ooni-probe-cli/internal/cli/root/root.go

73 lines
1.8 KiB
Go
Raw Normal View History

package root
import (
"github.com/alecthomas/kingpin"
2018-02-12 16:45:13 +01:00
"github.com/apex/log"
ooni "github.com/openobservatory/gooni"
"github.com/openobservatory/gooni/internal/database"
"github.com/openobservatory/gooni/internal/log/handlers/batch"
"github.com/openobservatory/gooni/internal/log/handlers/cli"
2018-02-12 16:45:13 +01:00
"github.com/prometheus/common/version"
)
// Cmd is the root command
var Cmd = kingpin.New("ooni", "")
// Command is syntax sugar for defining sub-commands
var Command = Cmd.Command
2018-02-12 16:45:13 +01:00
// Init should be called by all subcommand that care to have a ooni.OONI instance
var Init func() (*ooni.Config, *ooni.Context, error)
2018-02-12 16:45:13 +01:00
func init() {
2018-02-12 16:45:13 +01:00
configPath := Cmd.Flag("config", "Set a custom config file path").Short('c').String()
isVerbose := Cmd.Flag("verbose", "Enable verbose log output.").Short('v').Bool()
isBatch := Cmd.Flag("batch", "Enable batch command line usage.").Bool()
2018-02-12 16:45:13 +01:00
Cmd.PreAction(func(ctx *kingpin.ParseContext) error {
if *isBatch {
log.SetHandler(batch.Default)
} else {
log.SetHandler(cli.Default)
}
if *isVerbose {
2018-02-12 16:45:13 +01:00
log.SetLevel(log.DebugLevel)
log.Debugf("ooni version %s", version.Version)
}
Init = func() (*ooni.Config, *ooni.Context, error) {
2018-02-12 16:45:13 +01:00
var c *ooni.Config
var err error
if *configPath != "" {
log.Debugf("Reading config file from %s", *configPath)
2018-02-12 16:45:13 +01:00
c, err = ooni.ReadConfig(*configPath)
} else {
log.Debug("Reading default config file")
2018-02-12 16:45:13 +01:00
c, err = ooni.ReadDefaultConfigPaths()
}
if err != nil {
return nil, nil, err
}
2018-02-13 10:48:46 +01:00
dbPath, err := database.DefaultDatabasePath()
if err != nil {
return nil, nil, err
}
log.Debugf("Connecting to database sqlite3://%s", dbPath)
db, err := database.Connect(dbPath)
if err != nil {
return nil, nil, err
}
o := ooni.New(c, db)
2018-02-12 16:45:13 +01:00
o.Init()
return c, o, nil
}
return nil
})
}