Skip to content

Commit 59df443

Browse files
authored
cscli: print command name along with errors (#3768)
1 parent aa1abce commit 59df443

File tree

16 files changed

+51
-41
lines changed

16 files changed

+51
-41
lines changed

cmd/crowdsec-cli/args/args.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,3 @@ func NoArgs(cmd *cobra.Command, args []string) error {
5151
}
5252
return nil
5353
}
54-

cmd/crowdsec-cli/clibouncer/delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (cli *cliBouncers) delete(ctx context.Context, bouncers []string, ignoreMis
5757
log.Warnf("bouncer '%s' is auto-created and cannot be deleted, delete parent bouncer %s instead", bouncerName, parentBouncer)
5858
continue
5959
}
60-
//Try to find all child bouncers and delete them
60+
// Try to find all child bouncers and delete them
6161
for _, childBouncer := range allBouncers {
6262
if strings.HasPrefix(childBouncer.Name, bouncerName+"@") && childBouncer.AutoCreated {
6363
if err := cli.db.DeleteBouncer(ctx, childBouncer.Name); err != nil {

cmd/crowdsec-cli/cliconfig/backup.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
func (cli *cliConfig) newBackupCmd() *cobra.Command {
1010
cmd := &cobra.Command{
1111
Use: "backup",
12-
Short: "This command has been removed. You can backup/restore the configuration by other means.",
12+
Short: "This command has been removed. You can backup/restore the configuration by other means.",
1313
DisableAutoGenTag: true,
1414
RunE: func(_ *cobra.Command, _ []string) error {
1515
configDir := cli.cfg().ConfigPaths.ConfigDir

cmd/crowdsec-cli/cliconfig/restore.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
func (cli *cliConfig) newRestoreCmd() *cobra.Command {
1010
cmd := &cobra.Command{
1111
Use: "restore",
12-
Short: "This command has been removed. You can backup/restore the configuration by other means.",
12+
Short: "This command has been removed. You can backup/restore the configuration by other means.",
1313
DisableAutoGenTag: true,
1414
RunE: func(_ *cobra.Command, _ []string) error {
1515
configDir := cli.cfg().ConfigPaths.ConfigDir

cmd/crowdsec-cli/clidecision/decisions.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ import (
1717
log "github.com/sirupsen/logrus"
1818
"github.com/spf13/cobra"
1919

20+
"github.com/crowdsecurity/go-cs-lib/cstime"
21+
2022
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/args"
2123
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/clialert"
2224
"github.com/crowdsecurity/crowdsec/pkg/apiclient"
2325
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
2426
"github.com/crowdsecurity/crowdsec/pkg/models"
2527
"github.com/crowdsecurity/crowdsec/pkg/types"
26-
27-
"github.com/crowdsecurity/go-cs-lib/cstime"
2828
)
2929

3030
type configGetter func() *csconfig.Config

cmd/crowdsec-cli/clidecision/import.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,6 @@ func (cli *cliDecisions) import_(ctx context.Context, input string, duration str
212212
}
213213

214214
allowlistResp, _, err := cli.client.Allowlists.CheckIfAllowlistedBulk(ctx, decisionsStr)
215-
216215
if err != nil {
217216
return err
218217
}

cmd/crowdsec-cli/main.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,7 @@ It is meant to allow you to manage bans, parsers/scenarios/etc, api and generall
309309
}
310310

311311
func fatal(err error) {
312-
red := color.New(color.FgRed).SprintFunc()
313-
fmt.Fprintln(os.Stderr, red("Error:"), err)
312+
fmt.Fprintln(os.Stderr, color.RedString("Error:"), err)
314313
os.Exit(1)
315314
}
316315

@@ -323,7 +322,19 @@ func mainWrap() error {
323322
return err
324323
}
325324

326-
return cmd.ExecuteContext(ctx)
325+
err = cmd.ExecuteContext(ctx)
326+
if err != nil {
327+
cmdName := cmd.Name()
328+
329+
subCmd, _, subErr := cmd.Find(os.Args[1:])
330+
if subErr == nil {
331+
cmdName = subCmd.CommandPath()
332+
}
333+
334+
return fmt.Errorf("%s: %w", cmdName, err)
335+
}
336+
337+
return nil
327338
}
328339

329340
func main() {

cmd/crowdsec-cli/reload/reload.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ package reload
33
import (
44
"os"
55

6-
"github.com/crowdsecurity/go-cs-lib/version"
76
isatty "github.com/mattn/go-isatty"
7+
8+
"github.com/crowdsecurity/go-cs-lib/version"
89
)
910

1011
func UserMessage() string {

test/bats/01_cscli_lapi.bats

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ teardown() {
113113
config_set "$LOCAL_API_CREDENTIALS" '.url="http://127.0.0.1:-80"'
114114

115115
rune -1 cscli lapi status
116-
assert_stderr 'Error: failed to authenticate to Local API (LAPI): parse "http://127.0.0.1:-80/": invalid port ":-80" after host'
116+
assert_stderr 'Error: cscli lapi status: failed to authenticate to Local API (LAPI): parse "http://127.0.0.1:-80/": invalid port ":-80" after host'
117117
}
118118

119119
@test "cscli - bad LAPI password" {
@@ -122,7 +122,7 @@ teardown() {
122122
config_set "$LOCAL_API_CREDENTIALS" '.password="meh"'
123123

124124
rune -1 cscli lapi status
125-
assert_stderr 'Error: failed to authenticate to Local API (LAPI): API error: incorrect Username or Password'
125+
assert_stderr 'Error: cscli lapi status: failed to authenticate to Local API (LAPI): API error: incorrect Username or Password'
126126
}
127127

128128
@test "cscli lapi register / machines validate" {
@@ -189,7 +189,7 @@ teardown() {
189189
rune -1 cscli machines inspect malicious
190190
# XXX: we may want to remove this warning
191191
assert_stderr --partial 'QueryMachineByID : ent: machine not found'
192-
assert_stderr --partial "Error: unable to read machine data 'malicious': user 'malicious': user doesn't exist"
192+
assert_stderr --partial "Error: cscli machines inspect: unable to read machine data 'malicious': user 'malicious': user doesn't exist"
193193

194194
rune -0 cscli lapi register --machine newmachine --token 12345678901234567890123456789012
195195
assert_stderr --partial "Successfully registered to Local API"

test/bats/10_bouncers.bats

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ teardown() {
118118
rune -0 cscli bouncers add ciTestBouncer
119119
rune -1 cscli bouncers add ciTestBouncer
120120

121-
assert_stderr 'Error: unable to create bouncer: bouncer ciTestBouncer already exists'
121+
assert_stderr 'Error: cscli bouncers add: unable to create bouncer: bouncer ciTestBouncer already exists'
122122

123123
rune -0 cscli bouncers list -o json
124124
rune -0 jq '. | length' <(output)
@@ -134,7 +134,7 @@ teardown() {
134134

135135
@test "cscli bouncers prune" {
136136
rune -1 cscli bouncers prune --duration foobar
137-
assert_stderr 'Error: invalid argument "foobar" for "-d, --duration" flag: time: invalid duration "foobar"'
137+
assert_stderr 'Error: cscli bouncers prune: invalid argument "foobar" for "-d, --duration" flag: time: invalid duration "foobar"'
138138

139139
# duration takes days as well
140140
rune -0 cscli bouncers prune --duration 1d30m

0 commit comments

Comments
 (0)