chore: refactor selection by name
This commit is contained in:
parent
7a7f787f11
commit
e1902c12fa
4 changed files with 31 additions and 22 deletions
24
cmd/flags.go
24
cmd/flags.go
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue