diff --git a/cmd/set.go b/cmd/set.go index 4e066cb..5d21950 100644 --- a/cmd/set.go +++ b/cmd/set.go @@ -22,7 +22,10 @@ THE SOFTWARE. package cmd import ( + "fmt" "io" + "strings" + "time" "github.com/dgraph-io/badger/v4" "github.com/spf13/cobra" @@ -54,6 +57,20 @@ func set(cmd *cobra.Command, args []string) error { if err != nil { return err } + ttlRaw, err := cmd.Flags().GetString("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], @@ -64,6 +81,9 @@ func set(cmd *cobra.Command, args []string) error { if secret { entry = entry.WithMeta(metaSecret) } + if ttlRaw != "" { + entry = entry.WithTTL(ttl) + } return tx.SetEntry(entry) }, } @@ -74,4 +94,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)") }