/
root.go
54 lines (48 loc) · 1.2 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
package cmd
import (
"fmt"
"os"
"github.com/spf13/cobra"
log "github.com/sirupsen/logrus"
logrus_syslog "github.com/sirupsen/logrus/hooks/syslog"
"log/syslog"
)
var verbose bool
var version string
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "zfs2glacier",
Short: "backup zfs filesystems to aws glacier",
Long: ``,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
hook, err := logrus_syslog.NewSyslogHook("", "", syslog.LOG_USER, "zfs2glacier")
if err != nil {
panic(err)
}
log.SetFormatter(&log.TextFormatter{DisableColors: true})
log.AddHook(hook)
if verbose {
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(v string) {
version = v
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func init() {
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose logging to stdout")
}
func check(err error) {
if err != nil {
log.Error(err)
log.Exit(1)
}
}