diff --git a/internal/cli/run/run.go b/internal/cli/run/run.go index b2d03b9..57d5820 100644 --- a/internal/cli/run/run.go +++ b/internal/cli/run/run.go @@ -12,16 +12,9 @@ import ( func init() { cmd := root.Command("run", "Run a test group or OONI Run link") - - var nettestGroupNamesBlue []string - var probe *ooni.Probe - - for name := range nettests.NettestGroups { - nettestGroupNamesBlue = append(nettestGroupNamesBlue, color.BlueString(name)) - } - noCollector := cmd.Flag("no-collector", "Disable uploading measurements to a collector").Bool() + var probe *ooni.Probe cmd.Action(func(_ *kingpin.ParseContext) error { var err error probe, err = root.Init() @@ -29,22 +22,43 @@ func init() { log.Errorf("%s", err) return err } - if err = onboard.MaybeOnboarding(probe); err != nil { log.WithError(err).Error("failed to perform onboarding") return err } - if *noCollector == true { probe.Config().Sharing.UploadResults = false } return nil }) + functionalRun := func(pred func(name string, gr nettests.Group) bool) error { + for name, group := range nettests.All { + if pred(name, group) != true { + continue + } + log.Infof("Running %s tests", color.BlueString(name)) + conf := nettests.RunGroupConfig{GroupName: name, Probe: probe} + if err := nettests.RunGroup(conf); err != nil { + log.WithError(err).Errorf("failed to run %s", name) + } + } + return nil + } + + genRunWithGroupName := func(targetName string) func(*kingpin.ParseContext) error { + return func(*kingpin.ParseContext) error { + return functionalRun(func(groupName string, gr nettests.Group) bool { + return groupName == targetName + }) + } + } + websitesCmd := cmd.Command("websites", "") inputFile := websitesCmd.Flag("input-file", "File containing input URLs").Strings() input := websitesCmd.Flag("input", "Test the specified URL").Strings() websitesCmd.Action(func(_ *kingpin.ParseContext) error { + log.Infof("Running %s tests", color.BlueString("websites")) return nettests.RunGroup(nettests.RunGroupConfig{ GroupName: "websites", Probe: probe, @@ -52,43 +66,23 @@ func init() { Inputs: *input, }) }) - imCmd := cmd.Command("im", "") - imCmd.Action(func(_ *kingpin.ParseContext) error { - return nettests.RunGroup(nettests.RunGroupConfig{ - GroupName: "im", - Probe: probe, - }) - }) - performanceCmd := cmd.Command("performance", "") - performanceCmd.Action(func(_ *kingpin.ParseContext) error { - return nettests.RunGroup(nettests.RunGroupConfig{ - GroupName: "performance", - Probe: probe, - }) - }) - middleboxCmd := cmd.Command("middlebox", "") - middleboxCmd.Action(func(_ *kingpin.ParseContext) error { - return nettests.RunGroup(nettests.RunGroupConfig{ - GroupName: "middlebox", - Probe: probe, - }) - }) - circumventionCmd := cmd.Command("circumvention", "") - circumventionCmd.Action(func(_ *kingpin.ParseContext) error { - return nettests.RunGroup(nettests.RunGroupConfig{ - GroupName: "circumvention", - Probe: probe, + + easyRuns := []string{"im", "performance", "circumvention", "middlebox"} + for _, name := range easyRuns { + cmd.Command(name, "").Action(genRunWithGroupName(name)) + } + + unattendedCmd := cmd.Command("unattended", "") + unattendedCmd.Action(func(_ *kingpin.ParseContext) error { + return functionalRun(func(name string, gr nettests.Group) bool { + return gr.UnattendedOK == true }) }) + allCmd := cmd.Command("all", "").Default() allCmd.Action(func(_ *kingpin.ParseContext) error { - log.Infof("Running %s tests", color.BlueString("all")) - for tg := range nettests.NettestGroups { - group := nettests.RunGroupConfig{GroupName: tg, Probe: probe} - if err := nettests.RunGroup(group); err != nil { - log.WithError(err).Errorf("failed to run %s", tg) - } - } - return nil + return functionalRun(func(name string, gr nettests.Group) bool { + return true + }) }) } diff --git a/internal/nettests/groups.go b/internal/nettests/groups.go index 640c8ed..16285e5 100644 --- a/internal/nettests/groups.go +++ b/internal/nettests/groups.go @@ -1,18 +1,20 @@ package nettests -// NettestGroup base structure -type NettestGroup struct { - Label string - Nettests []Nettest +// Group is a group of nettests +type Group struct { + Label string + Nettests []Nettest + UnattendedOK bool } -// NettestGroups that can be run by the user -var NettestGroups = map[string]NettestGroup{ +// All contains all the nettests that can be run by the user +var All = map[string]Group{ "websites": { Label: "Websites", Nettests: []Nettest{ WebConnectivity{}, }, + UnattendedOK: true, }, "performance": { Label: "Performance", @@ -27,6 +29,7 @@ var NettestGroups = map[string]NettestGroup{ HTTPInvalidRequestLine{}, HTTPHeaderFieldManipulation{}, }, + UnattendedOK: true, }, "im": { Label: "Instant Messaging", @@ -35,6 +38,7 @@ var NettestGroups = map[string]NettestGroup{ Telegram{}, WhatsApp{}, }, + UnattendedOK: true, }, "circumvention": { Label: "Circumvention Tools", @@ -42,5 +46,6 @@ var NettestGroups = map[string]NettestGroup{ Psiphon{}, Tor{}, }, + UnattendedOK: true, }, } diff --git a/internal/nettests/run.go b/internal/nettests/run.go index 15cdc30..885adb9 100644 --- a/internal/nettests/run.go +++ b/internal/nettests/run.go @@ -44,7 +44,7 @@ func RunGroup(config RunGroupConfig) error { return err } - group, ok := NettestGroups[config.GroupName] + group, ok := All[config.GroupName] if !ok { log.Errorf("No test group named %s", config.GroupName) return errors.New("invalid test group name")