From 11a13d64b7f4f3a5317362c07c70908526ccff6e Mon Sep 17 00:00:00 2001 From: marco Date: Wed, 30 Apr 2025 13:13:36 +0200 Subject: [PATCH] custom duration type for "cscli allowlist add" --- cmd/crowdsec-cli/cliallowlists/allowlists.go | 17 ++++------------- test/bats/cscli-allowlists.bats | 2 +- 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/cmd/crowdsec-cli/cliallowlists/allowlists.go b/cmd/crowdsec-cli/cliallowlists/allowlists.go index be4b966be..488f8ffba 100644 --- a/cmd/crowdsec-cli/cliallowlists/allowlists.go +++ b/cmd/crowdsec-cli/cliallowlists/allowlists.go @@ -399,8 +399,8 @@ func (cli *cliAllowLists) delete(ctx context.Context, db *database.Client, name func (cli *cliAllowLists) newAddCmd() *cobra.Command { var ( - expirationStr string - comment string + expiration cstime.Duration + comment string ) cmd := &cobra.Command{ @@ -424,25 +424,16 @@ func (cli *cliAllowLists) newAddCmd() *cobra.Command { return err } - var expiration time.Duration - - if expirationStr != "" { - expiration, err = cstime.ParseDuration(expirationStr) - if err != nil { - return err - } - } - name := args[0] values := args[1:] - return cli.add(ctx, db, name, values, expiration, comment) + return cli.add(ctx, db, name, values, time.Duration(expiration), comment) }, } flags := cmd.Flags() - flags.StringVarP(&expirationStr, "expiration", "e", "", "expiration duration") + flags.VarP(&expiration, "expiration", "e", "expiration duration") flags.StringVarP(&comment, "comment", "d", "", "comment for the value") return cmd diff --git a/test/bats/cscli-allowlists.bats b/test/bats/cscli-allowlists.bats index 975dfe3cb..cf2706796 100644 --- a/test/bats/cscli-allowlists.bats +++ b/test/bats/cscli-allowlists.bats @@ -126,7 +126,7 @@ teardown() { refute_output assert_stderr 'Error: invalid day value in duration "1 day"' - rune -0 cscli allowlist add foo 10.10.10.10 -d comment -e '1d' + rune -0 cscli allowlist add foo 10.10.10.10 -d comment -e '1d12h' assert_output 'added 1 values to allowlist foo' refute_stderr