cmd: remove global state in main.go

* refactoring
* Now supporting default config locations
This commit is contained in:
Christian Schwarz
2017-09-17 18:20:05 +02:00
parent 4ac7e78e2b
commit 9cd83399d3
9 changed files with 148 additions and 77 deletions

View File

@ -12,23 +12,14 @@ package cmd
import (
"github.com/spf13/cobra"
golog "log"
"net/http"
_ "net/http/pprof"
"os"
)
type Logger interface {
Printf(format string, v ...interface{})
}
// global state / facilities
var (
conf *Config
logFlags int = golog.LUTC | golog.Ldate | golog.Ltime
log Logger
)
var RootCmd = &cobra.Command{
Use: "zrepl",
Short: "ZFS dataset replication",
@ -46,15 +37,13 @@ var rootArgs struct {
}
func init() {
cobra.OnInitialize(initConfig)
//cobra.OnInitialize(initConfig)
RootCmd.PersistentFlags().StringVar(&rootArgs.configFile, "config", "", "config file path")
RootCmd.PersistentFlags().StringVar(&rootArgs.httpPprof, "debug.pprof.http", "", "run pprof http server on given port")
}
func initConfig() {
log = golog.New(os.Stderr, "", logFlags)
// CPU profiling
if rootArgs.httpPprof != "" {
go func() {
@ -62,17 +51,6 @@ func initConfig() {
}()
}
// Config
if rootArgs.configFile == "" {
log.Printf("config file not set")
os.Exit(1)
}
var err error
if conf, err = ParseConfig(rootArgs.configFile); err != nil {
log.Printf("error parsing config: %s", err)
os.Exit(1)
}
return
}