From a86100e045d85ee2aacae851cf465740c4651c3f Mon Sep 17 00:00:00 2001 From: lew Date: Fri, 7 Nov 2025 19:52:06 +0000 Subject: [PATCH] fix(set): removed manual ttl parsing in favour of the .Duration() flag i didn't know existed --- cmd/set.go | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/cmd/set.go b/cmd/set.go index 5d21950..0fa1f6c 100644 --- a/cmd/set.go +++ b/cmd/set.go @@ -22,10 +22,7 @@ THE SOFTWARE. package cmd import ( - "fmt" "io" - "strings" - "time" "github.com/dgraph-io/badger/v4" "github.com/spf13/cobra" @@ -57,20 +54,10 @@ func set(cmd *cobra.Command, args []string) error { if err != nil { return err } - ttlRaw, err := cmd.Flags().GetString("ttl") + ttl, err := cmd.Flags().GetDuration("ttl") if err != nil { return err } - var ttl time.Duration - if strings.TrimSpace(ttlRaw) != "" { - ttl, err = time.ParseDuration(ttlRaw) - if err != nil { - return fmt.Errorf("invalid ttl %q: %w", ttlRaw, err) - } - if ttl <= 0 { - return fmt.Errorf("ttl must be greater than zero") - } - } trans := TransactionArgs{ key: args[0], @@ -81,7 +68,7 @@ func set(cmd *cobra.Command, args []string) error { if secret { entry = entry.WithMeta(metaSecret) } - if ttlRaw != "" { + if ttl != 0 { entry = entry.WithTTL(ttl) } return tx.SetEntry(entry) @@ -94,5 +81,5 @@ func set(cmd *cobra.Command, args []string) error { func init() { rootCmd.AddCommand(setCmd) setCmd.Flags().Bool("secret", false, "Mark the stored value as a secret") - setCmd.Flags().String("ttl", "", "Expire the key after the provided duration (e.g. 24h, 30m)") + setCmd.Flags().DurationP("ttl", "t", 0, "Expire the key after the provided duration (e.g. 24h, 30m)") }