forked from isgasho/bitcask
/
root.go
60 lines (50 loc) 路 1.5 KB
/
root.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"fmt"
"os"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/MurilloVaz/bitcask/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") {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.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)
}