Skip to content

Commit

Permalink
refactor for cleaner code
Browse files Browse the repository at this point in the history
  • Loading branch information
eranturgeman committed Sep 3, 2024
1 parent b73beff commit 4a880c0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
8 changes: 2 additions & 6 deletions cli/scancommands.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,8 @@ func EnrichCmd(c *components.Context) error {
func ScanCmd(c *components.Context) error {
if len(c.Arguments) == 0 && !c.IsFlagSet(flags.SpecFlag) {
return pluginsCommon.PrintHelpAndReturnError("providing either a <source pattern> argument or the 'spec' option is mandatory", c)
}

if len(c.Arguments) > 1 {
log.Warn(fmt.Sprintf("Too many arguments provided (%d in total).\n"+
"Some flags might be mistaken as arguments and ignored, which could prevent them from affecting the command as intended.\n"+
"Please ensure all provided flags are valid and provided correct order. For more details: 'jf %s --help'", len(c.Arguments), c.CommandName))
} else if len(c.Arguments) > 1 {
log.Warn(utils.GetCliTooManyArgsErrorMessage(len(c.Arguments), c.CommandName))
}

serverDetails, err := createServerDetailsWithConfigOffer(c)
Expand Down
9 changes: 9 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,12 @@ func splitEnvVar(envVar string) (key, value string) {
}
return split[0], strings.Join(split[1:], "=")
}

// This is a general error message for the CLI commands.
// Due to the way we parse the commands, in some cases flags might be mistaken as arguments and therefore they will not go though the command's flags verifications.
// When this happens it could prevent the flag from affecting the command as intended. This error message informes the user about this issue
func GetCliTooManyArgsErrorMessage(numberOfArguments int, commandName string) string {
return fmt.Sprintf("Too many arguments provided (%d in total).\n"+
"Some flags might be mistaken as arguments and ignored, which could prevent them from affecting the command as intended.\n"+
"Please ensure all provided flags are valid and provided correct order. For more details: 'jf %s --help'", numberOfArguments, commandName)
}

0 comments on commit 4a880c0

Please sign in to comment.