feat(cmd): added listcmd and refactored transactions to use TransactionArgs

This commit is contained in:
Lewis Wynne 2025-11-06 17:48:38 +00:00
parent cc206a7c8a
commit 2da42de7ab
5 changed files with 146 additions and 23 deletions

View file

@ -38,22 +38,31 @@ var setCmd = &cobra.Command{
func set(cmd *cobra.Command, args []string) error {
store := &Store{}
var value []byte
if len(args) == 2 {
return store.Transaction(args[0], false, func(tx *badger.Txn, k []byte) error {
return tx.Set(k, []byte(args[1]))
})
value = []byte(args[1])
} else {
bytes, err := io.ReadAll(cmd.InOrStdin())
if err != nil {
return err
}
value = bytes
}
bts, err := io.ReadAll(cmd.InOrStdin())
if err != nil {
return err
trans := TransactionArgs{
key: args[0],
readonly: false,
sync: false,
transact: func(tx *badger.Txn, k []byte) error {
return tx.Set(k, value)
},
}
return store.Transaction(args[0], false, func(tx *badger.Txn, k []byte) error {
return tx.Set(k, bts)
})
return store.Transaction(trans)
}
func init() {
rootCmd.AddCommand(setCmd)
}