Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display custom command usage when it has no step #264

Merged
merged 2 commits into from Nov 28, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 16 additions & 2 deletions cmd/cmd_utils.go
Expand Up @@ -51,6 +51,9 @@ func processCustomCommands(commands []cfg.Command, parentCommand *cobra.Command,
Use: commandConfig.Name,
Short: commandConfig.Description,
Long: commandConfig.Description,
PreRun: func(cmd *cobra.Command, args []string) {
preCustomCommand(cmd, args, parentCommand, commandConfig)
},
Run: func(cmd *cobra.Command, args []string) {
executeCustomCommand(cmd, args, parentCommand, commandConfig)
},
Expand Down Expand Up @@ -94,15 +97,26 @@ func processCustomCommands(commands []cfg.Command, parentCommand *cobra.Command,
return nil
}

// executeCustomCommand executes a custom command
func executeCustomCommand(cmd *cobra.Command, args []string, parentCommand *cobra.Command, commandConfig *cfg.Command) {
// preCustomCommand is run before a custom command is executed
func preCustomCommand(cmd *cobra.Command, args []string, parentCommand *cobra.Command, commandConfig *cfg.Command) {
var err error

if len(args) != len(commandConfig.Arguments) {
err = fmt.Errorf("invalid number of arguments, %d argument(s) required", len(commandConfig.Arguments))
u.PrintErrorToStdErrorAndExit(err)
}

// no steps means a sub command should be specified
if len(commandConfig.Steps) == 0 {
cmd.Help()
os.Exit(0)
}
}

// executeCustomCommand executes a custom command
func executeCustomCommand(cmd *cobra.Command, args []string, parentCommand *cobra.Command, commandConfig *cfg.Command) {
var err error

// Execute custom command's steps
for i, step := range commandConfig.Steps {
// Prepare template data for arguments
Expand Down