Skip to content

Commit

Permalink
fix: add error checking and update description for InstallCommand
Browse files Browse the repository at this point in the history
- Add error checking for missing `app` or `ns` inputs in the `PreRunE` function
- Add error checking for missing tool name in the `PreRunE` function
- Update the short description of the `InstallCommand` to include new tools: `etcdctl, mc, dnsctl`

Signed-off-by: ysicing <[email protected]>
  • Loading branch information
ysicing committed May 22, 2024
1 parent de8fcfb commit ab0f578
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
7 changes: 7 additions & 0 deletions cmd/backup/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"
"time"

"github.com/cockroachdb/errors"
"github.com/spf13/cobra"

"github.com/easysoft/qcadmin/internal/api/cne"
Expand All @@ -24,6 +25,12 @@ func NewCmdBackupApp(f factory.Factory) *cobra.Command {
Use: "app",
Short: "backup app",
Long: "backup app",
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(app) == 0 || len(ns) == 0 {
return errors.New("missing app or ns")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
log.Infof("start backup app: %s", app)
cneClient := cne.NewCneAPI()
Expand Down
16 changes: 11 additions & 5 deletions cmd/experimental/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,30 @@ var (
installExample = templates.Examples(`
# install tools
z experimental install helm`)
installTools = map[string]any{
"helm": true,
"kubectl": true,
"mc": true,
"etcdctl": true,
"dnsctl": true,
}
)

// InstallCommand install some tools
func InstallCommand(f factory.Factory) *cobra.Command {
installCmd := &cobra.Command{
Use: "install [flags]",
Short: "install tools, like: helm, kubectl",
Short: "install tools, like: helm, kubectl,etcdctl,mc,dnsctl",
Example: installExample,
Args: cobra.MinimumNArgs(1),
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
f.GetLog().Fatalf("args error: %v", args)
return errors.New("missing args: helm or kubectl")
return errors.New("missing args: tool name")
}
tool := args[0]
if tool != "helm" && tool != "kubectl" && tool != "etcdctl" && tool != "mc" {
// TODO add more tools
return errors.Errorf("not support tool: %s, only suppor helm, kubectl, etcdctl", tool)
if _, exist := installTools[tool]; !exist {
return errors.Errorf("not support tool: %s", tool)
}
return nil
},
Expand Down

0 comments on commit ab0f578

Please sign in to comment.