Skip to content

Commit

Permalink
use hook to realise type check
Browse files Browse the repository at this point in the history
Signed-off-by: WhereAreBugs <[email protected]>
  • Loading branch information
WhereAreBugs committed Dec 1, 2023
1 parent a9eeb98 commit 3f6fc7e
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions cli/command/cluster/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ const (
ADD_EXAMPLE = `Examples:
$ curveadm add my-cluster # Add a cluster named 'my-cluster'
$ curveadm add my-cluster -m "deploy for test" # Add a cluster with description
$ curveadm add my-cluster -f /path/to/topology.yaml # Add a cluster with specified topology`
$ curveadm add my-cluster -f /path/to/topology.yaml # Add a cluster with specified topology
$ curveadm add my-cluster -t develop # Add a cluster with type 'develop'`
)

var (
Expand Down Expand Up @@ -69,6 +70,9 @@ func NewAddCommand(curveadm *cli.CurveAdm) *cobra.Command {
Short: "Add cluster",
Args: utils.ExactArgs(1),
Example: ADD_EXAMPLE,
PreRunE: func(cmd *cobra.Command, args []string) error {
return checkAddOptions(cmd)
},
RunE: func(cmd *cobra.Command, args []string) error {
options.name = args[0]
return runAdd(curveadm, options)
Expand Down Expand Up @@ -140,9 +144,13 @@ func checkTopology(curveadm *cli.CurveAdm, data string, options addOptions) erro
return pb.Run()
}

func checkDeployType(deployType string) error {
func checkAddOptions(cmd *cobra.Command) error {
deployType, err := cmd.Flags().GetString("deploy-type")
if err != nil {
return err
}
for _, t := range SUPPORTED_DEPLOY_TYPES {
if t == deployType {
if deployType == t {
return nil
}
}
Expand Down Expand Up @@ -176,20 +184,14 @@ func runAdd(curveadm *cli.CurveAdm, options addOptions) error {
return err
}

// 4) check deploy type
err = checkDeployType(options.deployType)
if err != nil {
return err
}

// 5) insert cluster (with topology) into database
// 4) insert cluster (with topology) into database
uuid := uuid.NewString()
err = storage.InsertCluster(name, uuid, options.description, data, options.deployType)
if err != nil {
return errno.ERR_INSERT_CLUSTER_FAILED.E(err)
}

// 6) print success prompt
// 5) print success prompt
curveadm.WriteOutln("Added cluster '%s'", name)
return nil
}

0 comments on commit 3f6fc7e

Please sign in to comment.