chore: refactor selection by name

This commit is contained in:
andig 2022-03-15 11:28:13 +00:00
parent 7a7f787f11
commit e1902c12fa
4 changed files with 31 additions and 22 deletions

View file

@ -1,5 +1,11 @@
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
const (
flagHeaders = "log-headers"
flagHeadersDescription = "Log headers"
@ -22,3 +28,21 @@ const (
flagStop = "stop"
flagStopDescription = "Stop charging"
)
func selectByName(cmd *cobra.Command, conf *[]qualifiedConfig) error {
flag := cmd.PersistentFlags().Lookup(flagName)
if !flag.Changed {
return nil
}
name := flag.Value.String()
for _, cfg := range *conf {
if cfg.Name == name {
*conf = []qualifiedConfig{cfg}
return nil
}
}
return fmt.Errorf("%s not found", name)
}