feat(cmd): added listcmd and refactored transactions to use TransactionArgs
This commit is contained in:
parent
cc206a7c8a
commit
2da42de7ab
5 changed files with 146 additions and 23 deletions
27
cmd/set.go
27
cmd/set.go
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue