feat(ttl): added --ttl to setCmd, parsed with Go time.ParseDuration
This commit is contained in:
parent
98793b334a
commit
cf8a19cba0
1 changed files with 21 additions and 0 deletions
21
cmd/set.go
21
cmd/set.go
|
|
@ -22,7 +22,10 @@ THE SOFTWARE.
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/dgraph-io/badger/v4"
|
"github.com/dgraph-io/badger/v4"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
@ -54,6 +57,20 @@ func set(cmd *cobra.Command, args []string) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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{
|
trans := TransactionArgs{
|
||||||
key: args[0],
|
key: args[0],
|
||||||
|
|
@ -64,6 +81,9 @@ func set(cmd *cobra.Command, args []string) error {
|
||||||
if secret {
|
if secret {
|
||||||
entry = entry.WithMeta(metaSecret)
|
entry = entry.WithMeta(metaSecret)
|
||||||
}
|
}
|
||||||
|
if ttlRaw != "" {
|
||||||
|
entry = entry.WithTTL(ttl)
|
||||||
|
}
|
||||||
return tx.SetEntry(entry)
|
return tx.SetEntry(entry)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
@ -74,4 +94,5 @@ func set(cmd *cobra.Command, args []string) error {
|
||||||
func init() {
|
func init() {
|
||||||
rootCmd.AddCommand(setCmd)
|
rootCmd.AddCommand(setCmd)
|
||||||
setCmd.Flags().Bool("secret", false, "Mark the stored value as a secret")
|
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)")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue