bitcask-mirror/cmd/bitcask/root.go

61 lines
1.5 KiB
Go

package main
import (
"fmt"
"os"
"github.com/rs/zerolog"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"git.tcp.direct/Mirrors/bitcask-mirror/internal"
)
// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Use: "bitcask",
Version: internal.FullVersion(),
Short: "Command-line tools for bitcask",
Long: `This is the command-line tool to interact with a bitcask database.
This lets you get, set and delete key/value pairs as well as perform merge
(or compaction) operations. This tool serves as an example implementation
however is also intended to be useful in shell scripts.`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
// set logging level
if viper.GetBool("debug") {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
} else {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
}
},
}
// Execute adds all child commands to the root command
// and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := RootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func init() {
RootCmd.PersistentFlags().BoolP(
"debug", "d", false,
"Enable debug logging",
)
RootCmd.PersistentFlags().StringP(
"path", "p", "/tmp/bitcask",
"Path to Bitcask database",
)
viper.BindPFlag("path", RootCmd.PersistentFlags().Lookup("path"))
viper.SetDefault("path", "/tmp/bitcask")
viper.BindPFlag("debug", RootCmd.PersistentFlags().Lookup("debug"))
viper.SetDefault("debug", false)
}