Modify join and unjoin validation
Signed-off-by: chen zechun <zechun.chen@daocloud.io>
This commit is contained in:
parent
ed9b838056
commit
a94bc2b1bc
|
@ -1,7 +1,6 @@
|
||||||
package karmadactl
|
package karmadactl
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -42,7 +41,7 @@ func NewCmdJoin(karmadaConfig KarmadaConfig, parentCommand string) *cobra.Comman
|
||||||
if err := opts.Complete(args); err != nil {
|
if err := opts.Complete(args); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := opts.Validate(); err != nil {
|
if err := opts.Validate(args); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := RunJoin(karmadaConfig, opts); err != nil {
|
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.
|
// Complete ensures that options are valid and marshals them if necessary.
|
||||||
func (j *CommandJoinOption) Complete(args []string) error {
|
func (j *CommandJoinOption) Complete(args []string) error {
|
||||||
// Get cluster name from the command args.
|
// Get cluster name from the command args.
|
||||||
if len(args) == 0 {
|
if len(args) > 0 {
|
||||||
return errors.New("cluster name is required")
|
|
||||||
}
|
|
||||||
j.ClusterName = args[0]
|
j.ClusterName = args[0]
|
||||||
|
}
|
||||||
|
|
||||||
// If '--cluster-context' not specified, take the cluster name as the context.
|
// If '--cluster-context' not specified, take the cluster name as the context.
|
||||||
if len(j.ClusterContext) == 0 {
|
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.
|
// 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 {
|
if errMsgs := validation.ValidateClusterName(j.ClusterName); len(errMsgs) != 0 {
|
||||||
return fmt.Errorf("invalid cluster name(%s): %s", j.ClusterName, strings.Join(errMsgs, ";"))
|
return fmt.Errorf("invalid cluster name(%s): %s", j.ClusterName, strings.Join(errMsgs, ";"))
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,6 @@ package karmadactl
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -50,7 +49,7 @@ func NewCmdUnjoin(karmadaConfig KarmadaConfig, parentCommand string) *cobra.Comm
|
||||||
if err := opts.Complete(args); err != nil {
|
if err := opts.Complete(args); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := opts.Validate(); err != nil {
|
if err := opts.Validate(args); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := RunUnjoin(karmadaConfig, opts); err != nil {
|
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.
|
// Complete ensures that options are valid and marshals them if necessary.
|
||||||
func (j *CommandUnjoinOption) Complete(args []string) error {
|
func (j *CommandUnjoinOption) Complete(args []string) error {
|
||||||
// Get cluster name from the command args.
|
// Get cluster name from the command args.
|
||||||
if len(args) == 0 {
|
if len(args) > 0 {
|
||||||
return errors.New("cluster name is required")
|
|
||||||
}
|
|
||||||
j.ClusterName = args[0]
|
j.ClusterName = args[0]
|
||||||
|
}
|
||||||
|
|
||||||
// If '--cluster-context' not specified, take the cluster name as the context.
|
// If '--cluster-context' not specified, take the cluster name as the context.
|
||||||
if len(j.ClusterContext) == 0 {
|
if len(j.ClusterContext) == 0 {
|
||||||
|
@ -108,7 +106,13 @@ func (j *CommandUnjoinOption) Complete(args []string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate ensures that command unjoin options are valid.
|
// 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 {
|
if j.Wait < 0 {
|
||||||
return fmt.Errorf(" --wait %v must be a positive duration, e.g. 1m0s ", j.Wait)
|
return fmt.Errorf(" --wait %v must be a positive duration, e.g. 1m0s ", j.Wait)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue