Config file workflow management

This commit is contained in:
Arturo Filastò
2018-02-12 17:45:13 +02:00
parent 602a5dc952
commit 6008b5c7c5
6 changed files with 155 additions and 5 deletions
+33 -2
View File
@@ -2,7 +2,10 @@ package root
import (
"github.com/alecthomas/kingpin"
"github.com/openobservatory/gooni/internal/util"
"github.com/apex/log"
"github.com/apex/log/handlers/cli"
ooni "github.com/openobservatory/gooni"
"github.com/prometheus/common/version"
)
// Cmd is the root command
@@ -11,9 +14,37 @@ var Cmd = kingpin.New("ooni", "")
// Command is syntax sugar for defining sub-commands
var Command = Cmd.Command
// Init should be called by all subcommand that care to have a ooni.OONI instance
var Init func() (*ooni.Config, *ooni.OONI, error)
func init() {
configPath := Cmd.Flag("config", "Set a custom config file path").Short('c').String()
verbose := Cmd.Flag("verbose", "Enable verbose log output.").Short('v').Bool()
Cmd.PreAction(func(ctx *kingpin.ParseContext) error {
util.Log("Running pre-action")
log.SetHandler(cli.Default)
if *verbose {
log.SetLevel(log.DebugLevel)
log.Debugf("ooni version %s", version.Version)
}
Init = func() (*ooni.Config, *ooni.OONI, error) {
var c *ooni.Config
var err error
if *configPath != "" {
c, err = ooni.ReadConfig(*configPath)
} else {
c, err = ooni.ReadDefaultConfigPaths()
}
if err != nil {
return nil, nil, err
}
o := ooni.New(c)
o.Init()
return c, o, nil
}
return nil
})
}
+9 -1
View File
@@ -2,6 +2,7 @@ package run
import (
"github.com/alecthomas/kingpin"
"github.com/apex/log"
"github.com/openobservatory/gooni/internal/cli/root"
"github.com/openobservatory/gooni/internal/util"
)
@@ -12,7 +13,14 @@ func init() {
nettestGroup := cmd.Arg("name", "the nettest group to run").Required().String()
cmd.Action(func(_ *kingpin.ParseContext) error {
util.Log("Starting %s", nettestGroup)
util.Log("Starting %s", *nettestGroup)
config, ooni, err := root.Init()
if err != nil {
log.Errorf("%s", err)
return err
}
log.Infof("%s", config)
log.Infof("%s", ooni)
return nil
})
}
+1
View File
@@ -10,6 +10,7 @@ import (
"gopkg.in/AlecAivazis/survey.v1"
)
// HomePath returns the path to the OONI Home
func HomePath() (string, error) {
home, err := homedir.Dir()
if err != nil {