Cleanup handling of flags (#4717)
This commit is contained in:
parent
eca1cb533b
commit
98a43ad599
7 changed files with 32 additions and 60 deletions
|
|
@ -9,7 +9,6 @@ import (
|
|||
"github.com/evcc-io/evcc/cmd/shutdown"
|
||||
"github.com/evcc-io/evcc/server"
|
||||
"github.com/evcc-io/evcc/util"
|
||||
"github.com/evcc-io/evcc/util/request"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
|
@ -26,13 +25,12 @@ const noCurrent = -1
|
|||
func init() {
|
||||
rootCmd.AddCommand(chargerCmd)
|
||||
chargerCmd.PersistentFlags().StringP(flagName, "n", "", fmt.Sprintf(flagNameDescription, "charger"))
|
||||
chargerCmd.PersistentFlags().IntP(flagCurrent, "I", noCurrent, flagCurrentDescription)
|
||||
chargerCmd.Flags().IntP(flagCurrent, "I", noCurrent, flagCurrentDescription)
|
||||
//lint:ignore SA1019 as Title is safe on ascii
|
||||
chargerCmd.PersistentFlags().BoolP(flagEnable, "e", false, strings.Title(flagEnable))
|
||||
chargerCmd.Flags().BoolP(flagEnable, "e", false, strings.Title(flagEnable))
|
||||
//lint:ignore SA1019 as Title is safe on ascii
|
||||
chargerCmd.PersistentFlags().BoolP(flagDisable, "d", false, strings.Title(flagDisable))
|
||||
chargerCmd.PersistentFlags().BoolP(flagWakeup, "w", false, flagWakeupDescription)
|
||||
chargerCmd.PersistentFlags().Bool(flagHeaders, false, flagHeadersDescription)
|
||||
chargerCmd.Flags().BoolP(flagDisable, "d", false, strings.Title(flagDisable))
|
||||
chargerCmd.Flags().BoolP(flagWakeup, "w", false, flagWakeupDescription)
|
||||
}
|
||||
|
||||
func runCharger(cmd *cobra.Command, args []string) {
|
||||
|
|
@ -45,15 +43,10 @@ func runCharger(cmd *cobra.Command, args []string) {
|
|||
}
|
||||
|
||||
// setup environment
|
||||
if err := configureEnvironment(conf); err != nil {
|
||||
if err := configureEnvironment(cmd, conf); err != nil {
|
||||
log.FATAL.Fatal(err)
|
||||
}
|
||||
|
||||
// full http request log
|
||||
if cmd.PersistentFlags().Lookup(flagHeaders).Changed {
|
||||
request.LogHeaders = true
|
||||
}
|
||||
|
||||
// select single charger
|
||||
if err := selectByName(cmd, &conf.Chargers); err != nil {
|
||||
log.FATAL.Fatal(err)
|
||||
|
|
@ -77,7 +70,7 @@ func runCharger(cmd *cobra.Command, args []string) {
|
|||
}
|
||||
|
||||
current := int64(noCurrent)
|
||||
if flag := cmd.PersistentFlags().Lookup(flagCurrent); flag.Changed {
|
||||
if flag := cmd.Flags().Lookup(flagCurrent); flag.Changed {
|
||||
var err error
|
||||
current, err = strconv.ParseInt(flag.Value.String(), 10, 64)
|
||||
if err != nil {
|
||||
|
|
@ -95,7 +88,7 @@ func runCharger(cmd *cobra.Command, args []string) {
|
|||
}
|
||||
}
|
||||
|
||||
if cmd.PersistentFlags().Lookup(flagEnable).Changed {
|
||||
if cmd.Flags().Lookup(flagEnable).Changed {
|
||||
flagUsed = true
|
||||
|
||||
if err := v.Enable(true); err != nil {
|
||||
|
|
@ -103,7 +96,7 @@ func runCharger(cmd *cobra.Command, args []string) {
|
|||
}
|
||||
}
|
||||
|
||||
if cmd.PersistentFlags().Lookup(flagDisable).Changed {
|
||||
if cmd.Flags().Lookup(flagDisable).Changed {
|
||||
flagUsed = true
|
||||
|
||||
if err := v.Enable(false); err != nil {
|
||||
|
|
@ -111,7 +104,7 @@ func runCharger(cmd *cobra.Command, args []string) {
|
|||
}
|
||||
}
|
||||
|
||||
if cmd.PersistentFlags().Lookup(flagWakeup).Changed {
|
||||
if cmd.Flags().Lookup(flagWakeup).Changed {
|
||||
flagUsed = true
|
||||
|
||||
if vv, ok := v.(api.Resurrector); ok {
|
||||
|
|
|
|||
|
|
@ -10,7 +10,6 @@ import (
|
|||
"github.com/evcc-io/evcc/cmd/shutdown"
|
||||
"github.com/evcc-io/evcc/server"
|
||||
"github.com/evcc-io/evcc/util"
|
||||
"github.com/evcc-io/evcc/util/request"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
|
@ -27,10 +26,8 @@ var chargerRampCmd = &cobra.Command{
|
|||
func init() {
|
||||
chargerCmd.AddCommand(chargerRampCmd)
|
||||
|
||||
chargerRampCmd.PersistentFlags().StringP(flagName, "n", "", fmt.Sprintf(flagNameDescription, "charger"))
|
||||
chargerRampCmd.PersistentFlags().Bool(flagHeaders, false, flagHeadersDescription)
|
||||
chargerRampCmd.PersistentFlags().StringP(flagDigits, "", "0", "fractional digits (0..2)")
|
||||
chargerRampCmd.PersistentFlags().StringP(flagDelay, "", "1s", "ramp delay")
|
||||
chargerRampCmd.Flags().StringP(flagDigits, "", "0", "fractional digits (0..2)")
|
||||
chargerRampCmd.Flags().StringP(flagDelay, "", "1s", "ramp delay")
|
||||
}
|
||||
|
||||
func ramp(c api.Charger, digits int, delay time.Duration) {
|
||||
|
|
@ -81,15 +78,10 @@ func runChargerRamp(cmd *cobra.Command, args []string) {
|
|||
}
|
||||
|
||||
// setup environment
|
||||
if err := configureEnvironment(conf); err != nil {
|
||||
if err := configureEnvironment(cmd, conf); err != nil {
|
||||
log.FATAL.Fatal(err)
|
||||
}
|
||||
|
||||
// full http request log
|
||||
if cmd.PersistentFlags().Lookup(flagHeaders).Changed {
|
||||
request.LogHeaders = true
|
||||
}
|
||||
|
||||
// select single charger
|
||||
if err := selectByName(cmd, &conf.Chargers); err != nil {
|
||||
log.FATAL.Fatal(err)
|
||||
|
|
@ -112,12 +104,12 @@ func runChargerRamp(cmd *cobra.Command, args []string) {
|
|||
chargers = map[string]api.Charger{name: charger}
|
||||
}
|
||||
|
||||
digits, err := strconv.Atoi(cmd.PersistentFlags().Lookup(flagDigits).Value.String())
|
||||
digits, err := strconv.Atoi(cmd.Flags().Lookup(flagDigits).Value.String())
|
||||
if err != nil {
|
||||
log.ERROR.Fatalln(err)
|
||||
}
|
||||
|
||||
delay, err := time.ParseDuration(cmd.PersistentFlags().Lookup(flagDelay).Value.String())
|
||||
delay, err := time.ParseDuration(cmd.Flags().Lookup(flagDelay).Value.String())
|
||||
if err != nil {
|
||||
log.ERROR.Fatalln(err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -52,7 +52,7 @@ func runDump(cmd *cobra.Command, args []string) {
|
|||
|
||||
// setup environment
|
||||
if err == nil {
|
||||
err = configureEnvironment(conf)
|
||||
err = configureEnvironment(cmd, conf)
|
||||
}
|
||||
|
||||
var site *core.Site
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ import (
|
|||
"github.com/evcc-io/evcc/api"
|
||||
"github.com/evcc-io/evcc/server"
|
||||
"github.com/evcc-io/evcc/util"
|
||||
"github.com/evcc-io/evcc/util/request"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
|
@ -21,7 +20,6 @@ var meterCmd = &cobra.Command{
|
|||
func init() {
|
||||
rootCmd.AddCommand(meterCmd)
|
||||
meterCmd.PersistentFlags().StringP(flagName, "n", "", fmt.Sprintf(flagNameDescription, "meter"))
|
||||
meterCmd.PersistentFlags().Bool(flagHeaders, false, flagHeadersDescription)
|
||||
}
|
||||
|
||||
func runMeter(cmd *cobra.Command, args []string) {
|
||||
|
|
@ -34,15 +32,10 @@ func runMeter(cmd *cobra.Command, args []string) {
|
|||
}
|
||||
|
||||
// setup environment
|
||||
if err := configureEnvironment(conf); err != nil {
|
||||
if err := configureEnvironment(cmd, conf); err != nil {
|
||||
log.FATAL.Fatal(err)
|
||||
}
|
||||
|
||||
// full http request log
|
||||
if cmd.PersistentFlags().Lookup(flagHeaders).Changed {
|
||||
request.LogHeaders = true
|
||||
}
|
||||
|
||||
// select single meter
|
||||
if err := selectByName(cmd, &conf.Meters); err != nil {
|
||||
log.FATAL.Fatal(err)
|
||||
|
|
|
|||
|
|
@ -22,7 +22,6 @@ import (
|
|||
"github.com/evcc-io/evcc/server/updater"
|
||||
"github.com/evcc-io/evcc/util"
|
||||
"github.com/evcc-io/evcc/util/pipe"
|
||||
"github.com/evcc-io/evcc/util/request"
|
||||
"github.com/evcc-io/evcc/util/sponsor"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
|
||||
|
|
@ -178,11 +177,6 @@ func runRoot(cmd *cobra.Command, args []string) {
|
|||
|
||||
util.LogLevel(viper.GetString("log"), viper.GetStringMapString("levels"))
|
||||
|
||||
// full http request log
|
||||
if cmd.PersistentFlags().Lookup(flagHeaders).Changed {
|
||||
request.LogHeaders = true
|
||||
}
|
||||
|
||||
// network config
|
||||
if viper.GetString("uri") != "" {
|
||||
log.WARN.Println("`uri` is deprecated and will be ignored. Use `network` instead.")
|
||||
|
|
@ -220,7 +214,7 @@ func runRoot(cmd *cobra.Command, args []string) {
|
|||
|
||||
// setup environment
|
||||
if err == nil {
|
||||
err = configureEnvironment(conf)
|
||||
err = configureEnvironment(cmd, conf)
|
||||
}
|
||||
|
||||
// setup site and loadpoints
|
||||
|
|
|
|||
|
|
@ -22,10 +22,12 @@ import (
|
|||
"github.com/evcc-io/evcc/util"
|
||||
"github.com/evcc-io/evcc/util/machine"
|
||||
"github.com/evcc-io/evcc/util/pipe"
|
||||
"github.com/evcc-io/evcc/util/request"
|
||||
"github.com/evcc-io/evcc/util/sponsor"
|
||||
"github.com/evcc-io/evcc/util/telemetry"
|
||||
"github.com/libp2p/zeroconf/v2"
|
||||
"github.com/samber/lo"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"golang.org/x/text/currency"
|
||||
)
|
||||
|
|
@ -54,7 +56,12 @@ func loadConfigFile(conf *config) error {
|
|||
return err
|
||||
}
|
||||
|
||||
func configureEnvironment(conf config) (err error) {
|
||||
func configureEnvironment(cmd *cobra.Command, conf config) (err error) {
|
||||
// full http request log
|
||||
if cmd.PersistentFlags().Lookup(flagHeaders).Changed {
|
||||
request.LogHeaders = true
|
||||
}
|
||||
|
||||
// setup machine id
|
||||
if conf.Plant != "" {
|
||||
err = machine.CustomID(conf.Plant)
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ import (
|
|||
"github.com/evcc-io/evcc/api"
|
||||
"github.com/evcc-io/evcc/server"
|
||||
"github.com/evcc-io/evcc/util"
|
||||
"github.com/evcc-io/evcc/util/request"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
|
@ -21,10 +20,9 @@ var vehicleCmd = &cobra.Command{
|
|||
func init() {
|
||||
rootCmd.AddCommand(vehicleCmd)
|
||||
vehicleCmd.PersistentFlags().StringP(flagName, "n", "", fmt.Sprintf(flagNameDescription, "vehicle"))
|
||||
vehicleCmd.PersistentFlags().BoolP(flagStart, "a", false, flagStartDescription)
|
||||
vehicleCmd.PersistentFlags().BoolP(flagStop, "o", false, flagStopDescription)
|
||||
vehicleCmd.PersistentFlags().BoolP(flagWakeup, "w", false, flagWakeupDescription)
|
||||
vehicleCmd.PersistentFlags().Bool(flagHeaders, false, flagHeadersDescription)
|
||||
vehicleCmd.Flags().BoolP(flagStart, "a", false, flagStartDescription)
|
||||
vehicleCmd.Flags().BoolP(flagStop, "o", false, flagStopDescription)
|
||||
vehicleCmd.Flags().BoolP(flagWakeup, "w", false, flagWakeupDescription)
|
||||
}
|
||||
|
||||
func runVehicle(cmd *cobra.Command, args []string) {
|
||||
|
|
@ -37,15 +35,10 @@ func runVehicle(cmd *cobra.Command, args []string) {
|
|||
}
|
||||
|
||||
// setup environment
|
||||
if err := configureEnvironment(conf); err != nil {
|
||||
if err := configureEnvironment(cmd, conf); err != nil {
|
||||
log.FATAL.Fatal(err)
|
||||
}
|
||||
|
||||
// full http request log
|
||||
if cmd.PersistentFlags().Lookup(flagHeaders).Changed {
|
||||
request.LogHeaders = true
|
||||
}
|
||||
|
||||
// select single vehicle
|
||||
if err := selectByName(cmd, &conf.Vehicles); err != nil {
|
||||
log.FATAL.Fatal(err)
|
||||
|
|
@ -69,7 +62,7 @@ func runVehicle(cmd *cobra.Command, args []string) {
|
|||
|
||||
var flagUsed bool
|
||||
for _, v := range vehicles {
|
||||
if cmd.PersistentFlags().Lookup(flagWakeup).Changed {
|
||||
if cmd.Flags().Lookup(flagWakeup).Changed {
|
||||
flagUsed = true
|
||||
|
||||
if vv, ok := v.(api.Resurrector); ok {
|
||||
|
|
@ -81,7 +74,7 @@ func runVehicle(cmd *cobra.Command, args []string) {
|
|||
}
|
||||
}
|
||||
|
||||
if cmd.PersistentFlags().Lookup(flagStart).Changed {
|
||||
if cmd.Flags().Lookup(flagStart).Changed {
|
||||
flagUsed = true
|
||||
|
||||
if vv, ok := v.(api.VehicleChargeController); ok {
|
||||
|
|
@ -93,7 +86,7 @@ func runVehicle(cmd *cobra.Command, args []string) {
|
|||
}
|
||||
}
|
||||
|
||||
if cmd.PersistentFlags().Lookup(flagStop).Changed {
|
||||
if cmd.Flags().Lookup(flagStop).Changed {
|
||||
flagUsed = true
|
||||
|
||||
if vv, ok := v.(api.VehicleChargeController); ok {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue