bitcask-mirror/cmd/bitcask/get.go

52 lines
959 B
Go

package main
import (
"fmt"
"os"
log "github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"git.tcp.direct/Mirrors/bitcask-mirror"
)
var getCmd = &cobra.Command{
Use: "get <key>",
Aliases: []string{"view"},
Short: "Get a new Key and display its Value",
Long: `This retrieves a key and display its value`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
path := viper.GetString("path")
key := args[0]
os.Exit(get(path, key))
},
}
func init() {
RootCmd.AddCommand(getCmd)
}
func get(path, key string) int {
db, err := bitcask.Open(path)
if err != nil {
log.Error().Err(err).Msg("error opening database")
return 1
}
defer db.Close()
value, err := db.Get([]byte(key))
if err != nil {
log.Error().Err(err).Msg("error reading key")
return 1
}
fmt.Printf("%s\n", string(value))
log.Debug().Str("key", key).Bytes("value", value).Msg("key/value")
return 0
}