Skip to content

Commit

Permalink
Cleanup handling of flags (evcc-io#4717)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig authored Oct 4, 2022
1 parent eca1cb5 commit 98a43ad
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 60 deletions.
25 changes: 9 additions & 16 deletions cmd/charger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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) {
Expand All @@ -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)
Expand All @@ -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 {
Expand All @@ -95,23 +88,23 @@ 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 {
log.ERROR.Println("enable:", err)
}
}

if cmd.PersistentFlags().Lookup(flagDisable).Changed {
if cmd.Flags().Lookup(flagDisable).Changed {
flagUsed = true

if err := v.Enable(false); err != nil {
log.ERROR.Println("disable:", err)
}
}

if cmd.PersistentFlags().Lookup(flagWakeup).Changed {
if cmd.Flags().Lookup(flagWakeup).Changed {
flagUsed = true

if vv, ok := v.(api.Resurrector); ok {
Expand Down
18 changes: 5 additions & 13 deletions cmd/charger_ramp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 1 addition & 8 deletions cmd/meter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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) {
Expand All @@ -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)
Expand Down
8 changes: 1 addition & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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.")
Expand Down Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion cmd/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
Expand Down
21 changes: 7 additions & 14 deletions cmd/vehicle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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) {
Expand All @@ -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)
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit 98a43ad

Please sign in to comment.