mirror of
https://github.com/zrepl/zrepl.git
synced 2025-08-12 08:19:06 +02:00
cmd: remove global state in main.go
* refactoring * Now supporting default config locations
This commit is contained in:
24
cmd/main.go
24
cmd/main.go
@ -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
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user