2018-02-07 19:02:18 +01:00
|
|
|
package root
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/alecthomas/kingpin"
|
2018-02-12 16:45:13 +01:00
|
|
|
"github.com/apex/log"
|
2021-02-02 10:32:46 +01:00
|
|
|
"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/log/handlers/batch"
|
|
|
|
"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/log/handlers/cli"
|
|
|
|
"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/log/handlers/syslog"
|
|
|
|
"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/ooni"
|
|
|
|
"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/utils"
|
2021-02-04 11:00:27 +01:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/version"
|
2018-02-07 19:02:18 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Cmd is the root command
|
2019-10-03 09:43:25 +02:00
|
|
|
var Cmd = kingpin.New("ooniprobe", "")
|
2018-02-07 19:02:18 +01:00
|
|
|
|
|
|
|
// Command is syntax sugar for defining sub-commands
|
|
|
|
var Command = Cmd.Command
|
|
|
|
|
2018-05-03 18:40:52 +02:00
|
|
|
// Init should be called by all subcommand that care to have a ooni.Context instance
|
2020-11-13 19:01:06 +01:00
|
|
|
var Init func() (*ooni.Probe, error)
|
2018-02-12 16:45:13 +01:00
|
|
|
|
2020-11-13 21:16:43 +01:00
|
|
|
// NewProbeCLI is like Init but returns a ooni.ProbeCLI instead.
|
|
|
|
func NewProbeCLI() (ooni.ProbeCLI, error) {
|
|
|
|
probeCLI, err := Init()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return probeCLI, nil
|
|
|
|
}
|
|
|
|
|
2018-02-07 19:02:18 +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()
|
2018-02-21 16:06:30 +01:00
|
|
|
|
|
|
|
isVerbose := Cmd.Flag("verbose", "Enable verbose log output.").Short('v').Bool()
|
|
|
|
isBatch := Cmd.Flag("batch", "Enable batch command line usage.").Bool()
|
2020-12-01 13:31:15 +01:00
|
|
|
logHandler := Cmd.Flag(
|
|
|
|
"log-handler", "Set the desired log handler (one of: batch, cli, syslog)",
|
|
|
|
).String()
|
2018-02-12 16:45:13 +01:00
|
|
|
|
2019-12-29 14:07:57 +01:00
|
|
|
softwareName := Cmd.Flag(
|
|
|
|
"software-name", "Override application name",
|
2022-04-29 13:41:09 +02:00
|
|
|
).Default(ooni.DefaultSoftwareName).String()
|
2019-12-29 14:07:57 +01:00
|
|
|
softwareVersion := Cmd.Flag(
|
|
|
|
"software-version", "Override the application version",
|
|
|
|
).Default(version.Version).String()
|
2022-11-15 09:55:05 +01:00
|
|
|
proxy := Cmd.Flag(
|
|
|
|
"proxy", "specify a proxy address for speaking to the OONI Probe backend (use: --proxy=psiphon:/// for psiphon)",
|
|
|
|
).String()
|
2019-12-29 14:07:57 +01:00
|
|
|
|
2018-02-07 19:02:18 +01:00
|
|
|
Cmd.PreAction(func(ctx *kingpin.ParseContext) error {
|
2020-12-01 13:31:15 +01:00
|
|
|
// TODO(bassosimone): we need to properly deprecate --batch
|
|
|
|
// in favour of more granular command line flags.
|
|
|
|
if *isBatch && *logHandler != "" {
|
|
|
|
log.Fatal("cannot specify --batch and --log-handler together")
|
|
|
|
}
|
2018-02-21 16:06:30 +01:00
|
|
|
if *isBatch {
|
2020-12-01 13:31:15 +01:00
|
|
|
*logHandler = "batch"
|
|
|
|
}
|
|
|
|
switch *logHandler {
|
|
|
|
case "batch":
|
2018-02-21 16:06:30 +01:00
|
|
|
log.SetHandler(batch.Default)
|
2020-12-01 13:31:15 +01:00
|
|
|
case "cli", "":
|
2018-02-21 16:06:30 +01:00
|
|
|
log.SetHandler(cli.Default)
|
2020-12-01 13:31:15 +01:00
|
|
|
case "syslog":
|
|
|
|
log.SetHandler(syslog.Default)
|
|
|
|
default:
|
|
|
|
log.Fatalf("unknown --log-handler: %s", *logHandler)
|
2018-02-21 16:06:30 +01:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-11-13 19:01:06 +01:00
|
|
|
Init = func() (*ooni.Probe, error) {
|
2018-02-12 16:45:13 +01:00
|
|
|
var err error
|
|
|
|
|
2018-05-22 02:33:59 +02:00
|
|
|
homePath, err := utils.GetOONIHome()
|
2018-02-12 17:29:03 +01:00
|
|
|
if err != nil {
|
2018-03-23 12:10:14 +01:00
|
|
|
return nil, err
|
2018-02-12 17:29:03 +01:00
|
|
|
}
|
|
|
|
|
2020-11-13 19:01:06 +01:00
|
|
|
probe := ooni.NewProbe(*configPath, homePath)
|
2022-11-15 09:55:05 +01:00
|
|
|
err = probe.Init(*softwareName, *softwareVersion, *proxy)
|
2018-03-19 13:20:42 +01:00
|
|
|
if err != nil {
|
2018-03-23 12:10:14 +01:00
|
|
|
return nil, err
|
2018-03-19 13:20:42 +01:00
|
|
|
}
|
2018-09-17 16:16:56 +02:00
|
|
|
if *isBatch {
|
2020-11-13 20:07:30 +01:00
|
|
|
probe.SetIsBatch(true)
|
2018-09-17 16:16:56 +02:00
|
|
|
}
|
2018-03-19 13:20:42 +01:00
|
|
|
|
2020-11-13 19:01:06 +01:00
|
|
|
return probe, nil
|
2018-02-12 16:45:13 +01:00
|
|
|
}
|
|
|
|
|
2018-02-07 19:02:18 +01:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|