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

69 lines
1.7 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/ooni/probe-cli"
"github.com/ooni/probe-cli/internal/log/handlers/batch"
"github.com/ooni/probe-cli/internal/log/handlers/cli"
"github.com/ooni/probe-cli/internal/utils"
2019-05-23 16:38:46 +02:00
"github.com/ooni/probe-cli/version"
)
// Cmd is the root command
var Cmd = kingpin.New("ooniprobe", "")
// 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.Context instance
2018-03-23 12:10:14 +01:00
var Init func() (*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
softwareName := Cmd.Flag(
"software-name", "Override application name",
).Default("ooniprobe-cli").String()
softwareVersion := Cmd.Flag(
"software-version", "Override the application version",
).Default(version.Version).String()
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)
2019-05-23 16:38:46 +02:00
log.Debugf("ooni version %s", version.Version)
2018-02-12 16:45:13 +01:00
}
2018-03-23 12:10:14 +01:00
Init = func() (*ooni.Context, error) {
2018-02-12 16:45:13 +01:00
var err error
homePath, err := utils.GetOONIHome()
if err != nil {
2018-03-23 12:10:14 +01:00
return nil, err
}
2018-03-23 12:10:14 +01:00
ctx := ooni.NewContext(*configPath, homePath)
err = ctx.Init(*softwareName, *softwareVersion)
if err != nil {
2018-03-23 12:10:14 +01:00
return nil, err
}
if *isBatch {
ctx.IsBatch = true
}
2018-03-23 12:10:14 +01:00
return ctx, nil
2018-02-12 16:45:13 +01:00
}
return nil
})
}