Skip to content

Commit

Permalink
fix(load-balancer): show message if none of --server, --label-selecto…
Browse files Browse the repository at this point in the history
…r or --ip is set (#849)

Previously, if none of these flags were provided, only the error
`--server, --label-selector, and --ip are mutually exclusive` would
show, which is not wrong but doesn't include that exactly one is
required.

We could also use `cmd.MarkFlagsOneRequired()` and
`cmd.MarkFlagsMutuallyExclusive()` but the errors are not very pretty.
  • Loading branch information
phm07 authored Aug 26, 2024
1 parent 2cf34e9 commit 2df45e0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
3 changes: 3 additions & 0 deletions internal/cmd/loadbalancer/add_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ var AddTargetCmd = base.Cmd{
labelSelector, _ := cmd.Flags().GetString("label-selector")
ipAddr, _ := cmd.Flags().GetString("ip")

if !util.AnySet(serverIDOrName, labelSelector, ipAddr) {
return fmt.Errorf("specify one of --server, --label-selector, or --ip")
}
if !util.ExactlyOneSet(serverIDOrName, labelSelector, ipAddr) {
return fmt.Errorf("--server, --label-selector, and --ip are mutually exclusive")
}
Expand Down
3 changes: 3 additions & 0 deletions internal/cmd/loadbalancer/remove_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ var RemoveTargetCmd = base.Cmd{
return fmt.Errorf("Load Balancer not found: %s", idOrName)
}

if !util.AnySet(serverIDOrName, labelSelector, ipAddr) {
return fmt.Errorf("specify one of --server, --label-selector, or --ip")
}
if !util.ExactlyOneSet(serverIDOrName, labelSelector, ipAddr) {
return fmt.Errorf("--server, --label-selector, and --ip are mutually exclusive")
}
Expand Down
9 changes: 9 additions & 0 deletions internal/cmd/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ func ChainRunE(fns ...func(cmd *cobra.Command, args []string) error) func(cmd *c
}
}

func AnySet(ss ...string) bool {
for _, s := range ss {
if s != "" {
return true
}
}
return false
}

func ExactlyOneSet(s string, ss ...string) bool {
set := s != ""
for _, s := range ss {
Expand Down

0 comments on commit 2df45e0

Please sign in to comment.