cli: add config command (#18535)
This commit is contained in:
parent
39e7d6d264
commit
1e21f9a589
3 changed files with 140 additions and 0 deletions
63
cmd/config.go
Normal file
63
cmd/config.go
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/evcc-io/evcc/util/config"
|
||||
"github.com/evcc-io/evcc/util/templates"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// configCmd represents the configure command
|
||||
var configCmd = &cobra.Command{
|
||||
Use: "config",
|
||||
Short: "Dump database configuration",
|
||||
Run: runConfig,
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(configCmd)
|
||||
configCmd.Flags().String("class", "", "Device class ("+strings.Join(templates.ClassStrings(), "|")+")")
|
||||
}
|
||||
|
||||
func runConfig(cmd *cobra.Command, args []string) {
|
||||
// load config
|
||||
if err := loadConfigFile(&conf, !cmd.Flag(flagIgnoreDatabase).Changed); err != nil {
|
||||
log.FATAL.Fatal(err)
|
||||
}
|
||||
|
||||
// setup environment
|
||||
if err := configureEnvironment(cmd, &conf); err != nil {
|
||||
log.FATAL.Fatal(err)
|
||||
}
|
||||
|
||||
cc := templates.ClassValues()
|
||||
if c := cmd.Flag("class").Value.String(); c != "" {
|
||||
class, err := templates.ClassString(c)
|
||||
if err != nil {
|
||||
log.FATAL.Fatal(err)
|
||||
}
|
||||
cc = []templates.Class{class}
|
||||
}
|
||||
|
||||
for _, class := range cc {
|
||||
configurable, err := config.ConfigurationsByClass(class)
|
||||
if err != nil {
|
||||
log.FATAL.Fatal(err)
|
||||
}
|
||||
|
||||
if len(configurable) > 0 {
|
||||
if len(cc) > 0 {
|
||||
fmt.Println(class)
|
||||
fmt.Println("---")
|
||||
}
|
||||
|
||||
for _, c := range configurable {
|
||||
fmt.Println(config.NameForID(c.ID), "type:"+c.Type, c.Value)
|
||||
}
|
||||
|
||||
fmt.Println("")
|
||||
}
|
||||
}
|
||||
}
|
||||
70
cmd/config_delete.go
Normal file
70
cmd/config_delete.go
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/evcc-io/evcc/api"
|
||||
"github.com/evcc-io/evcc/core/loadpoint"
|
||||
"github.com/evcc-io/evcc/util/config"
|
||||
"github.com/evcc-io/evcc/util/templates"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// configDeleteCmd represents the configure command
|
||||
var configDeleteCmd = &cobra.Command{
|
||||
Use: "delete <id>",
|
||||
Short: "Delete device",
|
||||
Run: runConfigDelete,
|
||||
Args: cobra.ExactArgs(1),
|
||||
}
|
||||
|
||||
func init() {
|
||||
configCmd.AddCommand(configDeleteCmd)
|
||||
}
|
||||
|
||||
func runConfigDelete(cmd *cobra.Command, args []string) {
|
||||
// load config
|
||||
if err := loadConfigFile(&conf, !cmd.Flag(flagIgnoreDatabase).Changed); err != nil {
|
||||
log.FATAL.Fatal(err)
|
||||
}
|
||||
|
||||
// setup environment
|
||||
if err := configureEnvironment(cmd, &conf); err != nil {
|
||||
log.FATAL.Fatal(err)
|
||||
}
|
||||
|
||||
id, err := config.IDForName(args[0])
|
||||
if err != nil {
|
||||
log.FATAL.Fatal(err)
|
||||
}
|
||||
|
||||
c, err := config.ConfigByID(id)
|
||||
if err != nil {
|
||||
log.FATAL.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Println(config.NameForID(c.ID), "type:"+c.Type, c.Value)
|
||||
|
||||
switch c.Class {
|
||||
case templates.Charger:
|
||||
deleteDevice[api.Charger](c)
|
||||
case templates.Meter:
|
||||
deleteDevice[api.Meter](c)
|
||||
case templates.Vehicle:
|
||||
deleteDevice[api.Vehicle](c)
|
||||
case templates.Tariff:
|
||||
deleteDevice[api.Tariff](c)
|
||||
case templates.Circuit:
|
||||
deleteDevice[api.Circuit](c)
|
||||
case templates.Loadpoint:
|
||||
deleteDevice[loadpoint.API](c)
|
||||
}
|
||||
}
|
||||
|
||||
func deleteDevice[T any](c config.Config) {
|
||||
var zero T
|
||||
dev := config.NewConfigurableDevice[T](&c, zero)
|
||||
if err := dev.Delete(); err != nil {
|
||||
log.FATAL.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
|
@ -3,6 +3,8 @@ package config
|
|||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"dario.cat/mergo"
|
||||
"github.com/evcc-io/evcc/util/templates"
|
||||
|
|
@ -175,6 +177,11 @@ func NameForID(id int) string {
|
|||
return fmt.Sprintf("db:%d", id)
|
||||
}
|
||||
|
||||
// IDForName returns a unique config name for the given id
|
||||
func IDForName(name string) (int, error) {
|
||||
return strconv.Atoi(strings.TrimPrefix(name, "db:"))
|
||||
}
|
||||
|
||||
// ConfigurationsByClass returns devices by class from the database
|
||||
func ConfigurationsByClass(class templates.Class) ([]Config, error) {
|
||||
var devices []Config
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue