1
0
mirror of https://github.com/taigrr/bitcask synced 2025-01-18 04:03:17 -08:00
bitcask/cmd/bitcask/keys.go
2019-03-14 21:50:41 +10:00

50 lines
868 B
Go

package main
import (
"fmt"
"os"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/prologic/bitcask"
)
var keysCmd = &cobra.Command{
Use: "keys",
Aliases: []string{"list", "ls"},
Short: "Display all keys in Database",
Long: `This displays all known keys in the Database"`,
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
path := viper.GetString("path")
os.Exit(keys(path))
},
}
func init() {
RootCmd.AddCommand(keysCmd)
}
func keys(path string) int {
db, err := bitcask.Open(path)
if err != nil {
log.WithError(err).Error("error opening database")
return 1
}
defer db.Close()
err = db.Fold(func(key string) error {
fmt.Printf("%s\n", key)
return nil
})
if err != nil {
log.WithError(err).Error("error listing keys")
return 1
}
return 0
}