forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdump.go
138 lines (109 loc) Β· 2.81 KB
/
dump.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package cmd
import (
"bytes"
_ "embed"
"fmt"
"os"
"path/filepath"
"text/template"
"github.com/Masterminds/sprig/v3"
"github.com/evcc-io/evcc/core"
"github.com/evcc-io/evcc/server"
"github.com/evcc-io/evcc/util/config"
"github.com/spf13/cobra"
)
// dumpCmd represents the meter command
var dumpCmd = &cobra.Command{
Use: "dump",
Short: "Dump configuration",
Run: runDump,
}
var (
//go:embed dump.tpl
dumpTmpl string
dumpConfig *bool
)
func init() {
rootCmd.AddCommand(dumpCmd)
dumpConfig = dumpCmd.Flags().Bool("cfg", false, "Dump config file")
}
func handle[T any](name string, h config.Handler[T]) config.Device[T] {
dev, err := h.ByName(name)
if err != nil {
log.FATAL.Fatal(err)
}
return dev
}
func runDump(cmd *cobra.Command, args []string) {
// load config
err := loadConfigFile(&conf, !cmd.Flag(flagIgnoreDatabase).Changed)
// setup environment
if err == nil {
err = configureEnvironment(cmd, &conf)
}
var site *core.Site
if err == nil {
site, err = configureSiteAndLoadpoints(&conf)
}
if *dumpConfig {
file, pathErr := filepath.Abs(cfgFile)
if pathErr != nil {
file = cfgFile
}
var redacted string
if src, err := os.ReadFile(cfgFile); err == nil {
redacted = redact(string(src))
}
tmpl := template.Must(
template.New("dump").
Funcs(sprig.FuncMap()).
Parse(dumpTmpl))
out := new(bytes.Buffer)
_ = tmpl.Execute(out, map[string]any{
"CfgFile": file,
"CfgError": errorString(err),
"CfgContent": redacted,
"Version": server.FormattedVersion(),
})
fmt.Println(out.String())
os.Exit(0)
}
if err != nil {
log.FATAL.Fatal(err)
}
d := dumper{len: 2}
d.Header("config", "=")
fmt.Println("")
if name := site.Meters.GridMeterRef; name != "" {
d.DumpWithHeader(fmt.Sprintf("grid: %s", name), handle(name, config.Meters()))
}
for id, name := range site.Meters.PVMetersRef {
if name != "" {
d.DumpWithHeader(fmt.Sprintf("pv %d: %s", id+1, name), handle(name, config.Meters()))
}
}
for id, name := range site.Meters.BatteryMetersRef {
if name != "" {
d.DumpWithHeader(fmt.Sprintf("battery %d: %s", id+1, name), handle(name, config.Meters()))
}
}
for id, name := range site.Meters.AuxMetersRef {
if name != "" {
d.DumpWithHeader(fmt.Sprintf("aux %d: %s", id+1, name), handle(name, config.Meters()))
}
}
for _, v := range site.Vehicles().Instances() {
d.DumpWithHeader(fmt.Sprintf("vehicle: %s", v.Title()), v)
}
for id, lpI := range site.Loadpoints() {
lp := lpI.(*core.Loadpoint)
d.Header(fmt.Sprintf("loadpoint %d", id+1), "=")
fmt.Println("")
if name := lp.MeterRef; name != "" {
d.DumpWithHeader(fmt.Sprintf("charge: %s", name), handle(name, config.Meters()))
}
if name := lp.ChargerRef; name != "" {
d.DumpWithHeader(fmt.Sprintf("charger: %s", name), handle(name, config.Chargers()))
}
}
}