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 scanCmd = &cobra.Command{ Use: "scan ", Aliases: []string{"search", "find"}, Short: "Perform a prefix scan for keys", Long: `This performa a prefix scan for keys starting with the given prefix. This uses a Trie to search for matching keys and returns all matched keys.`, Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { path := viper.GetString("path") prefix := args[0] os.Exit(scan(path, prefix)) }, } func init() { RootCmd.AddCommand(scanCmd) } func scan(path, prefix string) int { db, err := bitcask.Open(path) if err != nil { log.Error().Err(err).Msg("error opening database") return 1 } defer db.Close() err = db.Scan([]byte(prefix), func(key []byte) error { value, err := db.Get(key) if err != nil { log.Error().Err(err).Msg("error reading key") return err } fmt.Printf("%s\n", string(value)) log.Debug().Interface("key", key).Interface("value", value).Msg("key/value") return nil }) if err != nil { log.Error().Err(err).Msg("error scanning keys") return 1 } return 0 }