From a94bc2b1bc8374d2901f06adfb44ae769b6ab653 Mon Sep 17 00:00:00 2001 From: chen zechun Date: Tue, 5 Jul 2022 21:43:54 +0800 Subject: [PATCH] Modify join and unjoin validation Signed-off-by: chen zechun --- pkg/karmadactl/join.go | 16 ++++++++++------ pkg/karmadactl/unjoin.go | 16 ++++++++++------ 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/pkg/karmadactl/join.go b/pkg/karmadactl/join.go index 58b400461..9a540790d 100644 --- a/pkg/karmadactl/join.go +++ b/pkg/karmadactl/join.go @@ -1,7 +1,6 @@ package karmadactl import ( - "errors" "fmt" "strings" @@ -42,7 +41,7 @@ func NewCmdJoin(karmadaConfig KarmadaConfig, parentCommand string) *cobra.Comman if err := opts.Complete(args); err != nil { return err } - if err := opts.Validate(); err != nil { + if err := opts.Validate(args); err != nil { return err } if err := RunJoin(karmadaConfig, opts); err != nil { @@ -90,10 +89,9 @@ type CommandJoinOption struct { // Complete ensures that options are valid and marshals them if necessary. func (j *CommandJoinOption) Complete(args []string) error { // Get cluster name from the command args. - if len(args) == 0 { - return errors.New("cluster name is required") + if len(args) > 0 { + j.ClusterName = args[0] } - j.ClusterName = args[0] // If '--cluster-context' not specified, take the cluster name as the context. if len(j.ClusterContext) == 0 { @@ -104,7 +102,13 @@ func (j *CommandJoinOption) Complete(args []string) error { } // Validate checks option and return a slice of found errs. -func (j *CommandJoinOption) Validate() error { +func (j *CommandJoinOption) Validate(args []string) error { + if len(args) > 1 { + return fmt.Errorf("only the cluster name is allowed as an argument") + } + if len(j.ClusterName) == 0 { + return fmt.Errorf("cluster name is required") + } if errMsgs := validation.ValidateClusterName(j.ClusterName); len(errMsgs) != 0 { return fmt.Errorf("invalid cluster name(%s): %s", j.ClusterName, strings.Join(errMsgs, ";")) } diff --git a/pkg/karmadactl/unjoin.go b/pkg/karmadactl/unjoin.go index c92872383..f6f8ffb25 100644 --- a/pkg/karmadactl/unjoin.go +++ b/pkg/karmadactl/unjoin.go @@ -2,7 +2,6 @@ package karmadactl import ( "context" - "errors" "fmt" "time" @@ -50,7 +49,7 @@ func NewCmdUnjoin(karmadaConfig KarmadaConfig, parentCommand string) *cobra.Comm if err := opts.Complete(args); err != nil { return err } - if err := opts.Validate(); err != nil { + if err := opts.Validate(args); err != nil { return err } if err := RunUnjoin(karmadaConfig, opts); err != nil { @@ -94,10 +93,9 @@ type CommandUnjoinOption struct { // Complete ensures that options are valid and marshals them if necessary. func (j *CommandUnjoinOption) Complete(args []string) error { // Get cluster name from the command args. - if len(args) == 0 { - return errors.New("cluster name is required") + if len(args) > 0 { + j.ClusterName = args[0] } - j.ClusterName = args[0] // If '--cluster-context' not specified, take the cluster name as the context. if len(j.ClusterContext) == 0 { @@ -108,7 +106,13 @@ func (j *CommandUnjoinOption) Complete(args []string) error { } // Validate ensures that command unjoin options are valid. -func (j *CommandUnjoinOption) Validate() error { +func (j *CommandUnjoinOption) Validate(args []string) error { + if len(args) > 1 { + return fmt.Errorf("only the cluster name is allowed as an argument") + } + if len(j.ClusterName) == 0 { + return fmt.Errorf("cluster name is required") + } if j.Wait < 0 { return fmt.Errorf(" --wait %v must be a positive duration, e.g. 1m0s ", j.Wait) }