return 1 with incomplete command line; always check error when calling cmd.Help (#1335)
This commit is contained in:
parent
4e6b9597f8
commit
aca37a38e7
|
@ -252,7 +252,7 @@ cscli alerts list --type ban`,
|
||||||
|
|
||||||
if err := manageCliDecisionAlerts(alertListFilter.IPEquals, alertListFilter.RangeEquals,
|
if err := manageCliDecisionAlerts(alertListFilter.IPEquals, alertListFilter.RangeEquals,
|
||||||
alertListFilter.ScopeEquals, alertListFilter.ValueEquals); err != nil {
|
alertListFilter.ScopeEquals, alertListFilter.ValueEquals); err != nil {
|
||||||
_ = cmd.Help()
|
printHelp(cmd)
|
||||||
log.Fatalf("%s", err)
|
log.Fatalf("%s", err)
|
||||||
}
|
}
|
||||||
if limit != nil {
|
if limit != nil {
|
||||||
|
@ -267,7 +267,7 @@ cscli alerts list --type ban`,
|
||||||
realDuration := strings.TrimSuffix(*alertListFilter.Until, "d")
|
realDuration := strings.TrimSuffix(*alertListFilter.Until, "d")
|
||||||
days, err := strconv.Atoi(realDuration)
|
days, err := strconv.Atoi(realDuration)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cmd.Help()
|
printHelp(cmd)
|
||||||
log.Fatalf("Can't parse duration %s, valid durations format: 1d, 4h, 4h15m", *alertListFilter.Until)
|
log.Fatalf("Can't parse duration %s, valid durations format: 1d, 4h, 4h15m", *alertListFilter.Until)
|
||||||
}
|
}
|
||||||
*alertListFilter.Until = fmt.Sprintf("%d%s", days*24, "h")
|
*alertListFilter.Until = fmt.Sprintf("%d%s", days*24, "h")
|
||||||
|
@ -281,7 +281,7 @@ cscli alerts list --type ban`,
|
||||||
realDuration := strings.TrimSuffix(*alertListFilter.Since, "d")
|
realDuration := strings.TrimSuffix(*alertListFilter.Since, "d")
|
||||||
days, err := strconv.Atoi(realDuration)
|
days, err := strconv.Atoi(realDuration)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cmd.Help()
|
printHelp(cmd)
|
||||||
log.Fatalf("Can't parse duration %s, valid durations format: 1d, 4h, 4h15m", *alertListFilter.Since)
|
log.Fatalf("Can't parse duration %s, valid durations format: 1d, 4h, 4h15m", *alertListFilter.Since)
|
||||||
}
|
}
|
||||||
*alertListFilter.Since = fmt.Sprintf("%d%s", days*24, "h")
|
*alertListFilter.Since = fmt.Sprintf("%d%s", days*24, "h")
|
||||||
|
@ -368,7 +368,7 @@ cscli alerts delete -s crowdsecurity/ssh-bf"`,
|
||||||
if !AlertDeleteAll {
|
if !AlertDeleteAll {
|
||||||
if err := manageCliDecisionAlerts(alertDeleteFilter.IPEquals, alertDeleteFilter.RangeEquals,
|
if err := manageCliDecisionAlerts(alertDeleteFilter.IPEquals, alertDeleteFilter.RangeEquals,
|
||||||
alertDeleteFilter.ScopeEquals, alertDeleteFilter.ValueEquals); err != nil {
|
alertDeleteFilter.ScopeEquals, alertDeleteFilter.ValueEquals); err != nil {
|
||||||
_ = cmd.Help()
|
printHelp(cmd)
|
||||||
log.Fatalf("%s", err)
|
log.Fatalf("%s", err)
|
||||||
}
|
}
|
||||||
if ActiveDecision != nil {
|
if ActiveDecision != nil {
|
||||||
|
@ -424,7 +424,7 @@ cscli alerts delete -s crowdsecurity/ssh-bf"`,
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
if len(args) == 0 {
|
if len(args) == 0 {
|
||||||
_ = cmd.Help()
|
printHelp(cmd)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for _, alertID := range args {
|
for _, alertID := range args {
|
||||||
|
|
|
@ -200,7 +200,7 @@ cscli decisions list -t ban
|
||||||
realDuration := strings.TrimSuffix(*filter.Until, "d")
|
realDuration := strings.TrimSuffix(*filter.Until, "d")
|
||||||
days, err := strconv.Atoi(realDuration)
|
days, err := strconv.Atoi(realDuration)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cmd.Help()
|
printHelp(cmd)
|
||||||
log.Fatalf("Can't parse duration %s, valid durations format: 1d, 4h, 4h15m", *filter.Until)
|
log.Fatalf("Can't parse duration %s, valid durations format: 1d, 4h, 4h15m", *filter.Until)
|
||||||
}
|
}
|
||||||
*filter.Until = fmt.Sprintf("%d%s", days*24, "h")
|
*filter.Until = fmt.Sprintf("%d%s", days*24, "h")
|
||||||
|
@ -214,7 +214,7 @@ cscli decisions list -t ban
|
||||||
realDuration := strings.TrimSuffix(*filter.Since, "d")
|
realDuration := strings.TrimSuffix(*filter.Since, "d")
|
||||||
days, err := strconv.Atoi(realDuration)
|
days, err := strconv.Atoi(realDuration)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cmd.Help()
|
printHelp(cmd)
|
||||||
log.Fatalf("Can't parse duration %s, valid durations format: 1d, 4h, 4h15m", *filter.Until)
|
log.Fatalf("Can't parse duration %s, valid durations format: 1d, 4h, 4h15m", *filter.Until)
|
||||||
}
|
}
|
||||||
*filter.Since = fmt.Sprintf("%d%s", days*24, "h")
|
*filter.Since = fmt.Sprintf("%d%s", days*24, "h")
|
||||||
|
@ -325,9 +325,8 @@ cscli decisions add --scope username --value foobar
|
||||||
addValue = addRange
|
addValue = addRange
|
||||||
addScope = types.Range
|
addScope = types.Range
|
||||||
} else if addValue == "" {
|
} else if addValue == "" {
|
||||||
cmd.Help()
|
printHelp(cmd)
|
||||||
log.Errorf("Missing arguments, a value is required (--ip, --range or --scope and --value)")
|
log.Fatalf("Missing arguments, a value is required (--ip, --range or --scope and --value)")
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if addReason == "" {
|
if addReason == "" {
|
||||||
|
|
|
@ -36,7 +36,7 @@ cscli explain --dsn "file://myfile.log" --type nginx
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
|
||||||
if logType == "" || (logLine == "" && logFile == "" && dsn == "") {
|
if logType == "" || (logLine == "" && logFile == "" && dsn == "") {
|
||||||
cmd.Help()
|
printHelp(cmd)
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
fmt.Printf("Please provide --type flag\n")
|
fmt.Printf("Please provide --type flag\n")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|
|
@ -165,7 +165,7 @@ cscli hubtest create my-scenario-test --parsers crowdsecurity/nginx --scenarios
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
if !runAll && len(args) == 0 {
|
if !runAll && len(args) == 0 {
|
||||||
cmd.Help()
|
printHelp(cmd)
|
||||||
fmt.Println("Please provide test to run or --all flag")
|
fmt.Println("Please provide test to run or --all flag")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
|
@ -194,10 +194,7 @@ cscli machines add MyTestMachine --password MyPassword
|
||||||
// create machineID if not specified by user
|
// create machineID if not specified by user
|
||||||
if len(args) == 0 {
|
if len(args) == 0 {
|
||||||
if !autoAdd {
|
if !autoAdd {
|
||||||
err = cmd.Help()
|
printHelp(cmd)
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("unable to print help(): %s", err)
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
machineID, err = generateID()
|
machineID, err = generateID()
|
||||||
|
@ -218,10 +215,7 @@ cscli machines add MyTestMachine --password MyPassword
|
||||||
// create password if doesn't specified by user
|
// create password if doesn't specified by user
|
||||||
if machinePassword == "" && !interactive {
|
if machinePassword == "" && !interactive {
|
||||||
if !autoAdd {
|
if !autoAdd {
|
||||||
err = cmd.Help()
|
printHelp(cmd)
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("unable to print help(): %s", err)
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
machinePassword = generatePassword(passwordLength)
|
machinePassword = generatePassword(passwordLength)
|
||||||
|
|
|
@ -180,7 +180,7 @@ cscli simulation disable crowdsecurity/ssh-bf`,
|
||||||
log.Fatalf("unable to enable global simulation mode : %s", err.Error())
|
log.Fatalf("unable to enable global simulation mode : %s", err.Error())
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
cmd.Help()
|
printHelp(cmd)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -224,7 +224,7 @@ cscli simulation disable crowdsecurity/ssh-bf`,
|
||||||
log.Fatalf("unable to disable global simulation mode : %s", err.Error())
|
log.Fatalf("unable to disable global simulation mode : %s", err.Error())
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
cmd.Help()
|
printHelp(cmd)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,10 +20,18 @@ import (
|
||||||
dto "github.com/prometheus/client_model/go"
|
dto "github.com/prometheus/client_model/go"
|
||||||
"github.com/prometheus/prom2json"
|
"github.com/prometheus/prom2json"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
"golang.org/x/mod/semver"
|
"golang.org/x/mod/semver"
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func printHelp(cmd *cobra.Command) {
|
||||||
|
err := cmd.Help()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("uname to print help(): %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func inSlice(s string, slice []string) bool {
|
func inSlice(s string, slice []string) bool {
|
||||||
for _, str := range slice {
|
for _, str := range slice {
|
||||||
if s == str {
|
if s == str {
|
||||||
|
|
39
tests/bats/90_decisions.bats
Normal file
39
tests/bats/90_decisions.bats
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
#!/usr/bin/env bats
|
||||||
|
# vim: ft=bats:list:ts=8:sts=4:sw=4:et:ai:si:
|
||||||
|
|
||||||
|
set -u
|
||||||
|
|
||||||
|
setup_file() {
|
||||||
|
load "../lib/setup_file.sh" >&3 2>&1
|
||||||
|
}
|
||||||
|
|
||||||
|
teardown_file() {
|
||||||
|
load "../lib/teardown_file.sh" >&3 2>&1
|
||||||
|
}
|
||||||
|
|
||||||
|
setup() {
|
||||||
|
load "../lib/setup.sh"
|
||||||
|
./instance-data load
|
||||||
|
./instance-crowdsec start
|
||||||
|
}
|
||||||
|
|
||||||
|
teardown() {
|
||||||
|
./instance-crowdsec stop
|
||||||
|
}
|
||||||
|
|
||||||
|
declare stderr
|
||||||
|
|
||||||
|
#----------
|
||||||
|
|
||||||
|
@test "$FILE 'decisions add' requires parameters" {
|
||||||
|
run -1 --separate-stderr cscli decisions add
|
||||||
|
assert_line "Usage:"
|
||||||
|
run echo "$stderr"
|
||||||
|
assert_output --partial "Missing arguments, a value is required (--ip, --range or --scope and --value)"
|
||||||
|
|
||||||
|
run -1 --separate-stderr cscli decisions add -o json
|
||||||
|
run echo "$stderr"
|
||||||
|
run -0 jq -c '[ .level, .msg]' <(output)
|
||||||
|
assert_output '["fatal","Missing arguments, a value is required (--ip, --range or --scope and --value)"]'
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue