Skip to content

Commit

Permalink
Refactor validation
Browse files Browse the repository at this point in the history
  • Loading branch information
jace-ys committed Feb 5, 2024
1 parent 3ac9017 commit 569d5bc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
26 changes: 14 additions & 12 deletions pkg/renew/renew.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,24 +96,26 @@ func NewCmdRenew(ctx context.Context, ioStreams genericclioptions.IOStreams) *co

// Validate validates the provided options
func (o *Options) Validate(cmd *cobra.Command, args []string) error {
if len(o.LabelSelector) > 0 && len(args) > 0 {
return errors.New("cannot specify Certificate names in conjunction with label selectors")
}

if len(o.LabelSelector) > 0 && o.All {
return errors.New("cannot specify label selectors in conjunction with --all flag")
}

if o.All && len(args) > 0 {
return errors.New("cannot specify Certificate names in conjunction with --all flag")
}
if len(args) == 0 {
if !o.All && len(o.LabelSelector) == 0 {
return errors.New("please either supply one or more Certificate resource names, label selectors, or use the --all flag to renew all Certificate resources")
}
} else {
if len(o.LabelSelector) > 0 {
return errors.New("cannot specify Certificate names in conjunction with label selectors")
}

if o.All && cmd.PersistentFlags().Changed("namespace") {
return errors.New("cannot specify --namespace flag in conjunction with --all flag")
}
if o.All {
return errors.New("cannot specify Certificate names in conjunction with --all flag")
}

if !o.All && len(args) == 0 && len(o.LabelSelector) == 0 {
return errors.New("please either supply one or more Certificate resource names, label selectors, or use the --all flag to renew all Certificate resources")
if o.AllNamespaces {
return errors.New("cannot specify Certificate names in conjunction with --all-namespaces flag")
}
}

return nil
Expand Down
11 changes: 9 additions & 2 deletions pkg/renew/renew_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ func TestValidate(t *testing.T) {
args: []string{"abc"},
expErr: true,
},
"If there are arguments, as well as --all-namespaces, error": {
options: &Options{
AllNamespaces: true,
},
args: []string{"abc"},
expErr: true,
},
"If there are all certificates selected, as well as label selector, error": {
options: &Options{
LabelSelector: "foo=bar",
Expand All @@ -63,14 +70,14 @@ func TestValidate(t *testing.T) {
},
expErr: false,
},
"If --namespace and --all namespace specified, error": {
"If --namespace and --all specified, don't error": {
options: &Options{
All: true,
},
setStringFlags: []stringFlag{
{name: "namespace", value: "foo"},
},
expErr: true,
expErr: false,
},
"If --namespace specified without arguments, error": {
options: &Options{},
Expand Down

0 comments on commit 569d5bc

Please sign in to comment.