Refactoring to reduce package count

* Consolidate util and utils into the same package
* Move internal/onboard into internal/cli/onboard
* Move maybeOnboard into the onboard package
This commit is contained in:
Arturo Filastò 2019-12-02 15:15:50 +02:00
parent 430a53e7d6
commit 58199a020e
12 changed files with 189 additions and 194 deletions

View File

@ -1,14 +1,164 @@
package onboard package onboard
import ( import (
"errors" "fmt"
"github.com/alecthomas/kingpin" "github.com/alecthomas/kingpin"
"github.com/apex/log" "github.com/apex/log"
"github.com/fatih/color"
ooni "github.com/ooni/probe-cli"
"github.com/ooni/probe-cli/config"
"github.com/ooni/probe-cli/internal/cli/root" "github.com/ooni/probe-cli/internal/cli/root"
"github.com/ooni/probe-cli/internal/onboard" "github.com/ooni/probe-cli/internal/output"
"github.com/pkg/errors"
"gopkg.in/AlecAivazis/survey.v1"
) )
// Onboarding start the interactive onboarding procedure
func Onboarding(config *config.Config) error {
output.SectionTitle("What is OONI Probe?")
fmt.Println()
output.Paragraph("Your tool for detecting internet censorship!")
fmt.Println()
output.Paragraph("OONI Probe checks whether your provider blocks access to sites and services. Run OONI Probe to collect evidence of internet censorship and to measure your network performance.")
fmt.Println()
output.PressEnterToContinue("Press 'Enter' to continue...")
output.SectionTitle("Heads Up")
fmt.Println()
output.Bullet("Anyone monitoring your internet activity (such as your government or ISP) may be able to see that you are running OONI Probe.")
fmt.Println()
output.Bullet("The network data you will collect will automatically be published (unless you opt-out in the settings).")
fmt.Println()
output.Bullet("You may test objectionable sites.")
fmt.Println()
output.Bullet("Read the documentation to learn more.")
fmt.Println()
output.PressEnterToContinue("Press 'Enter' to continue...")
output.SectionTitle("Pop Quiz!")
output.Paragraph("")
answer := ""
quiz1 := &survey.Select{
Message: "Anyone monitoring my internet activity may be able to see that I am running OONI Probe.",
Options: []string{"true", "false"},
Default: "true",
}
survey.AskOne(quiz1, &answer, nil)
if answer != "true" {
output.Paragraph(color.RedString("Actually..."))
output.Paragraph("OONI Probe is not a privacy tool. Therefore, anyone monitoring your internet activity may be able to see which software you are running.")
} else {
output.Paragraph(color.BlueString("Good job!"))
}
answer = ""
quiz2 := &survey.Select{
Message: "The network data I will collect will automatically be published (unless I opt-out in the settings).",
Options: []string{"true", "false"},
Default: "true",
}
survey.AskOne(quiz2, &answer, nil)
if answer != "true" {
output.Paragraph(color.RedString("Actually..."))
output.Paragraph("The network data you will collect will automatically be published to increase transparency of internet censorship (unless you opt-out in the settings).")
} else {
output.Paragraph(color.BlueString("Well done!"))
}
changeDefaults := false
prompt := &survey.Confirm{
Message: "Do you want to change the default settings?",
Default: false,
}
survey.AskOne(prompt, &changeDefaults, nil)
settings := struct {
IncludeIP bool
IncludeNetwork bool
IncludeCountry bool
UploadResults bool
SendCrashReports bool
}{}
settings.IncludeIP = false
settings.IncludeNetwork = true
settings.IncludeCountry = true
settings.UploadResults = true
settings.SendCrashReports = true
if changeDefaults == true {
var qs = []*survey.Question{
{
Name: "IncludeIP",
Prompt: &survey.Confirm{Message: "Should we include your IP?"},
},
{
Name: "IncludeNetwork",
Prompt: &survey.Confirm{
Message: "Can we include your network name?",
Default: true,
},
},
{
Name: "IncludeCountry",
Prompt: &survey.Confirm{
Message: "Can we include your country name?",
Default: true,
},
},
{
Name: "UploadResults",
Prompt: &survey.Confirm{
Message: "Can we upload your results?",
Default: true,
},
},
{
Name: "SendCrashReports",
Prompt: &survey.Confirm{
Message: "Can we send crash reports to OONI?",
Default: true,
},
},
}
err := survey.Ask(qs, &settings)
if err != nil {
log.WithError(err).Error("there was an error in parsing your responses")
return err
}
}
config.Lock()
config.InformedConsent = true
config.Sharing.IncludeCountry = settings.IncludeCountry
config.Advanced.SendCrashReports = settings.SendCrashReports
config.Sharing.IncludeIP = settings.IncludeIP
config.Sharing.IncludeASN = settings.IncludeNetwork
config.Sharing.UploadResults = settings.UploadResults
config.Unlock()
if err := config.Write(); err != nil {
log.WithError(err).Error("failed to write config file")
return err
}
return nil
}
// MaybeOnboarding will run the onboarding process only if the informed consent
// config option is set to false
func MaybeOnboarding(c *ooni.Context) error {
if c.Config.InformedConsent == false {
if c.IsBatch == true {
return errors.New("cannot run onboarding in batch mode")
}
if err := Onboarding(c.Config); err != nil {
return errors.Wrap(err, "onboarding")
}
}
return nil
}
func init() { func init() {
cmd := root.Command("onboard", "Starts the onboarding process") cmd := root.Command("onboard", "Starts the onboarding process")
@ -35,6 +185,6 @@ func init() {
return errors.New("cannot do onboarding in batch mode") return errors.New("cannot do onboarding in batch mode")
} }
return onboard.Onboarding(ctx.Config) return Onboarding(ctx.Config)
}) })
} }

View File

@ -7,6 +7,7 @@ import (
"github.com/apex/log" "github.com/apex/log"
"github.com/fatih/color" "github.com/fatih/color"
ooni "github.com/ooni/probe-cli" ooni "github.com/ooni/probe-cli"
"github.com/ooni/probe-cli/internal/cli/onboard"
"github.com/ooni/probe-cli/internal/cli/root" "github.com/ooni/probe-cli/internal/cli/root"
"github.com/ooni/probe-cli/internal/database" "github.com/ooni/probe-cli/internal/database"
"github.com/ooni/probe-cli/nettests" "github.com/ooni/probe-cli/nettests"
@ -66,7 +67,7 @@ func init() {
return err return err
} }
if err = ctx.MaybeOnboarding(); err != nil { if err = onboard.MaybeOnboarding(ctx); err != nil {
log.WithError(err).Error("failed to perform onboarding") log.WithError(err).Error("failed to perform onboarding")
return err return err
} }

View File

@ -6,7 +6,7 @@ import (
"path/filepath" "path/filepath"
"time" "time"
"github.com/ooni/probe-cli/internal/shutil" "github.com/ooni/probe-cli/utils/shutil"
"github.com/pkg/errors" "github.com/pkg/errors"
"upper.io/db.v3/lib/sqlbuilder" "upper.io/db.v3/lib/sqlbuilder"
) )

View File

@ -11,7 +11,7 @@ import (
"github.com/apex/log" "github.com/apex/log"
"github.com/fatih/color" "github.com/fatih/color"
colorable "github.com/mattn/go-colorable" colorable "github.com/mattn/go-colorable"
"github.com/ooni/probe-cli/internal/util" "github.com/ooni/probe-cli/utils"
) )
// Default handler outputting to stderr. // Default handler outputting to stderr.
@ -67,7 +67,7 @@ func logSectionTitle(w io.Writer, f log.Fields) error {
title := f.Get("title").(string) title := f.Get("title").(string)
fmt.Fprintf(w, "┏"+strings.Repeat("━", colWidth+2)+"┓\n") fmt.Fprintf(w, "┏"+strings.Repeat("━", colWidth+2)+"┓\n")
fmt.Fprintf(w, "┃ %s ┃\n", util.RightPad(title, colWidth)) fmt.Fprintf(w, "┃ %s ┃\n", utils.RightPad(title, colWidth))
fmt.Fprintf(w, "┗"+strings.Repeat("━", colWidth+2)+"┛\n") fmt.Fprintf(w, "┗"+strings.Repeat("━", colWidth+2)+"┛\n")
return nil return nil
} }
@ -84,7 +84,7 @@ func logTable(w io.Writer, f log.Fields) error {
continue continue
} }
line := fmt.Sprintf("%s: %s", color.Sprint(name), f.Get(name)) line := fmt.Sprintf("%s: %s", color.Sprint(name), f.Get(name))
lineLength := util.EscapeAwareRuneCountInString(line) lineLength := utils.EscapeAwareRuneCountInString(line)
lines = append(lines, line) lines = append(lines, line)
if colWidth < lineLength { if colWidth < lineLength {
colWidth = lineLength colWidth = lineLength
@ -94,7 +94,7 @@ func logTable(w io.Writer, f log.Fields) error {
fmt.Fprintf(w, "┏"+strings.Repeat("━", colWidth+2)+"┓\n") fmt.Fprintf(w, "┏"+strings.Repeat("━", colWidth+2)+"┓\n")
for _, line := range lines { for _, line := range lines {
fmt.Fprintf(w, "┃ %s ┃\n", fmt.Fprintf(w, "┃ %s ┃\n",
util.RightPad(line, colWidth), utils.RightPad(line, colWidth),
) )
} }
fmt.Fprintf(w, "┗"+strings.Repeat("━", colWidth+2)+"┛\n") fmt.Fprintf(w, "┗"+strings.Repeat("━", colWidth+2)+"┛\n")

View File

@ -9,7 +9,7 @@ import (
"time" "time"
"github.com/apex/log" "github.com/apex/log"
"github.com/ooni/probe-cli/internal/util" "github.com/ooni/probe-cli/utils"
) )
func statusIcon(ok bool) string { func statusIcon(ok bool) string {
@ -35,7 +35,7 @@ func logTestKeys(w io.Writer, testKeys string) error {
} }
for _, line := range testKeysLines { for _, line := range testKeysLines {
fmt.Fprintf(w, fmt.Sprintf("│ %s │\n", fmt.Fprintf(w, fmt.Sprintf("│ %s │\n",
util.RightPad(line, colWidth*2))) utils.RightPad(line, colWidth*2)))
} }
return nil return nil
} }
@ -71,22 +71,22 @@ func logMeasurementItem(w io.Writer, f log.Fields) error {
failureStr := fmt.Sprintf("success: %s", statusIcon(!isFailed)) failureStr := fmt.Sprintf("success: %s", statusIcon(!isFailed))
fmt.Fprintf(w, fmt.Sprintf("│ %s │\n", fmt.Fprintf(w, fmt.Sprintf("│ %s │\n",
util.RightPad( utils.RightPad(
fmt.Sprintf("#%d", rID), colWidth*2))) fmt.Sprintf("#%d", rID), colWidth*2)))
if url != "" { if url != "" {
fmt.Fprintf(w, fmt.Sprintf("│ %s │\n", fmt.Fprintf(w, fmt.Sprintf("│ %s │\n",
util.RightPad( utils.RightPad(
fmt.Sprintf("%s (%s)", url, urlCategoryCode), colWidth*2))) fmt.Sprintf("%s (%s)", url, urlCategoryCode), colWidth*2)))
} }
fmt.Fprintf(w, fmt.Sprintf("│ %s %s│\n", fmt.Fprintf(w, fmt.Sprintf("│ %s %s│\n",
util.RightPad(testName, colWidth), utils.RightPad(testName, colWidth),
util.RightPad(anomalyStr, colWidth))) utils.RightPad(anomalyStr, colWidth)))
fmt.Fprintf(w, fmt.Sprintf("│ %s %s│\n", fmt.Fprintf(w, fmt.Sprintf("│ %s %s│\n",
util.RightPad(failureStr, colWidth), utils.RightPad(failureStr, colWidth),
util.RightPad(uploadStr, colWidth))) utils.RightPad(uploadStr, colWidth)))
if testKeys != "" { if testKeys != "" {
if err := logTestKeys(w, testKeys); err != nil { if err := logTestKeys(w, testKeys); err != nil {
@ -116,15 +116,15 @@ func logMeasurementSummary(w io.Writer, f log.Fields) error {
networkName := f.Get("network_name").(string) networkName := f.Get("network_name").(string)
fmt.Fprintf(w, " │ %s │\n", fmt.Fprintf(w, " │ %s │\n",
util.RightPad(startTime.Format(time.RFC822), (colWidth+3)*3), utils.RightPad(startTime.Format(time.RFC822), (colWidth+3)*3),
) )
fmt.Fprintf(w, " │ %s │\n", fmt.Fprintf(w, " │ %s │\n",
util.RightPad(fmt.Sprintf("AS%d, %s (%s)", asn, networkName, countryCode), (colWidth+3)*3), utils.RightPad(fmt.Sprintf("AS%d, %s (%s)", asn, networkName, countryCode), (colWidth+3)*3),
) )
fmt.Fprintf(w, " │ %s %s %s │\n", fmt.Fprintf(w, " │ %s %s %s │\n",
util.RightPad(fmt.Sprintf("%.2fs", totalRuntime), colWidth), utils.RightPad(fmt.Sprintf("%.2fs", totalRuntime), colWidth),
util.RightPad(fmt.Sprintf("%d/%d anmls", anomalyCount, totalCount), colWidth), utils.RightPad(fmt.Sprintf("%d/%d anmls", anomalyCount, totalCount), colWidth),
util.RightPad(fmt.Sprintf("⬆ %s ⬇ %s", formatSize(dataUp), formatSize(dataDown)), colWidth+4)) utils.RightPad(fmt.Sprintf("⬆ %s ⬇ %s", formatSize(dataUp), formatSize(dataDown)), colWidth+4))
fmt.Fprintf(w, " └────────────────────────────────────────────────┘\n") fmt.Fprintf(w, " └────────────────────────────────────────────────┘\n")
return nil return nil

View File

@ -9,7 +9,7 @@ import (
"github.com/apex/log" "github.com/apex/log"
"github.com/ooni/probe-cli/internal/database" "github.com/ooni/probe-cli/internal/database"
"github.com/ooni/probe-cli/internal/util" "github.com/ooni/probe-cli/utils"
) )
func formatSpeed(speed float64) string { func formatSpeed(speed float64) string {
@ -95,7 +95,7 @@ func logResultItem(w io.Writer, f log.Fields) error {
fmt.Fprintf(w, "┢"+strings.Repeat("━", colWidth*2+2)+"┪\n") fmt.Fprintf(w, "┢"+strings.Repeat("━", colWidth*2+2)+"┪\n")
} }
firstRow := util.RightPad(fmt.Sprintf("#%d - %s", rID, startTime.Format(time.RFC822)), colWidth*2) firstRow := utils.RightPad(fmt.Sprintf("#%d - %s", rID, startTime.Format(time.RFC822)), colWidth*2)
fmt.Fprintf(w, "┃ "+firstRow+" ┃\n") fmt.Fprintf(w, "┃ "+firstRow+" ┃\n")
fmt.Fprintf(w, "┡"+strings.Repeat("━", colWidth*2+2)+"┩\n") fmt.Fprintf(w, "┡"+strings.Repeat("━", colWidth*2+2)+"┩\n")
@ -105,14 +105,14 @@ func logResultItem(w io.Writer, f log.Fields) error {
f.Get("test_keys").(string)) f.Get("test_keys").(string))
fmt.Fprintf(w, fmt.Sprintf("│ %s %s│\n", fmt.Fprintf(w, fmt.Sprintf("│ %s %s│\n",
util.RightPad(name, colWidth), utils.RightPad(name, colWidth),
util.RightPad(summary[0], colWidth))) utils.RightPad(summary[0], colWidth)))
fmt.Fprintf(w, fmt.Sprintf("│ %s %s│\n", fmt.Fprintf(w, fmt.Sprintf("│ %s %s│\n",
util.RightPad(networkName, colWidth), utils.RightPad(networkName, colWidth),
util.RightPad(summary[1], colWidth))) utils.RightPad(summary[1], colWidth)))
fmt.Fprintf(w, fmt.Sprintf("│ %s %s│\n", fmt.Fprintf(w, fmt.Sprintf("│ %s %s│\n",
util.RightPad(asn, colWidth), utils.RightPad(asn, colWidth),
util.RightPad(summary[2], colWidth))) utils.RightPad(summary[2], colWidth)))
if index == totalCount-1 { if index == totalCount-1 {
if isDone == true { if isDone == true {
@ -139,9 +139,9 @@ func logResultSummary(w io.Writer, f log.Fields) error {
} }
// └┬──────────────┬──────────────┬──────────────┬ // └┬──────────────┬──────────────┬──────────────┬
fmt.Fprintf(w, " │ %s │ %s │ %s │\n", fmt.Fprintf(w, " │ %s │ %s │ %s │\n",
util.RightPad(fmt.Sprintf("%d tests", tests), 12), utils.RightPad(fmt.Sprintf("%d tests", tests), 12),
util.RightPad(fmt.Sprintf("%d nets", networks), 12), utils.RightPad(fmt.Sprintf("%d nets", networks), 12),
util.RightPad(fmt.Sprintf("⬆ %s ⬇ %s", formatSize(dataUp), formatSize(dataDown)), 16)) utils.RightPad(fmt.Sprintf("⬆ %s ⬇ %s", formatSize(dataUp), formatSize(dataDown)), 16))
fmt.Fprintf(w, " └──────────────┴──────────────┴──────────────────┘\n") fmt.Fprintf(w, " └──────────────┴──────────────┴──────────────────┘\n")
return nil return nil

View File

@ -1,141 +0,0 @@
package onboard
import (
"fmt"
"github.com/apex/log"
"github.com/fatih/color"
"github.com/ooni/probe-cli/config"
"github.com/ooni/probe-cli/internal/output"
survey "gopkg.in/AlecAivazis/survey.v1"
)
func Onboarding(config *config.Config) error {
output.SectionTitle("What is OONI Probe?")
fmt.Println()
output.Paragraph("Your tool for detecting internet censorship!")
fmt.Println()
output.Paragraph("OONI Probe checks whether your provider blocks access to sites and services. Run OONI Probe to collect evidence of internet censorship and to measure your network performance.")
fmt.Println()
output.PressEnterToContinue("Press 'Enter' to continue...")
output.SectionTitle("Heads Up")
fmt.Println()
output.Bullet("Anyone monitoring your internet activity (such as your government or ISP) may be able to see that you are running OONI Probe.")
fmt.Println()
output.Bullet("The network data you will collect will automatically be published (unless you opt-out in the settings).")
fmt.Println()
output.Bullet("You may test objectionable sites.")
fmt.Println()
output.Bullet("Read the documentation to learn more.")
fmt.Println()
output.PressEnterToContinue("Press 'Enter' to continue...")
output.SectionTitle("Pop Quiz!")
output.Paragraph("")
answer := ""
quiz1 := &survey.Select{
Message: "Anyone monitoring my internet activity may be able to see that I am running OONI Probe.",
Options: []string{"true", "false"},
Default: "true",
}
survey.AskOne(quiz1, &answer, nil)
if answer != "true" {
output.Paragraph(color.RedString("Actually..."))
output.Paragraph("OONI Probe is not a privacy tool. Therefore, anyone monitoring your internet activity may be able to see which software you are running.")
} else {
output.Paragraph(color.BlueString("Good job!"))
}
answer = ""
quiz2 := &survey.Select{
Message: "The network data I will collect will automatically be published (unless I opt-out in the settings).",
Options: []string{"true", "false"},
Default: "true",
}
survey.AskOne(quiz2, &answer, nil)
if answer != "true" {
output.Paragraph(color.RedString("Actually..."))
output.Paragraph("The network data you will collect will automatically be published to increase transparency of internet censorship (unless you opt-out in the settings).")
} else {
output.Paragraph(color.BlueString("Well done!"))
}
changeDefaults := false
prompt := &survey.Confirm{
Message: "Do you want to change the default settings?",
Default: false,
}
survey.AskOne(prompt, &changeDefaults, nil)
settings := struct {
IncludeIP bool
IncludeNetwork bool
IncludeCountry bool
UploadResults bool
SendCrashReports bool
}{}
settings.IncludeIP = false
settings.IncludeNetwork = true
settings.IncludeCountry = true
settings.UploadResults = true
settings.SendCrashReports = true
if changeDefaults == true {
var qs = []*survey.Question{
{
Name: "IncludeIP",
Prompt: &survey.Confirm{Message: "Should we include your IP?"},
},
{
Name: "IncludeNetwork",
Prompt: &survey.Confirm{
Message: "Can we include your network name?",
Default: true,
},
},
{
Name: "IncludeCountry",
Prompt: &survey.Confirm{
Message: "Can we include your country name?",
Default: true,
},
},
{
Name: "UploadResults",
Prompt: &survey.Confirm{
Message: "Can we upload your results?",
Default: true,
},
},
{
Name: "SendCrashReports",
Prompt: &survey.Confirm{
Message: "Can we send crash reports to OONI?",
Default: true,
},
},
}
err := survey.Ask(qs, &settings)
if err != nil {
log.WithError(err).Error("there was an error in parsing your responses")
return err
}
}
config.Lock()
config.InformedConsent = true
config.Sharing.IncludeCountry = settings.IncludeCountry
config.Advanced.SendCrashReports = settings.SendCrashReports
config.Sharing.IncludeIP = settings.IncludeIP
config.Sharing.IncludeASN = settings.IncludeNetwork
config.Sharing.UploadResults = settings.UploadResults
config.Unlock()
if err := config.Write(); err != nil {
log.WithError(err).Error("failed to write config file")
return err
}
return nil
}

View File

@ -8,7 +8,7 @@ import (
"github.com/apex/log" "github.com/apex/log"
"github.com/ooni/probe-cli/internal/database" "github.com/ooni/probe-cli/internal/database"
"github.com/ooni/probe-cli/internal/util" "github.com/ooni/probe-cli/utils"
) )
// MeasurementJSON prints the JSON of a measurement // MeasurementJSON prints the JSON of a measurement
@ -154,12 +154,12 @@ func SectionTitle(text string) {
func Paragraph(text string) { func Paragraph(text string) {
const width = 80 const width = 80
fmt.Println(util.WrapString(text, width)) fmt.Println(utils.WrapString(text, width))
} }
func Bullet(text string) { func Bullet(text string) {
const width = 80 const width = 80
fmt.Printf("• %s\n", util.WrapString(text, width)) fmt.Printf("• %s\n", utils.WrapString(text, width))
} }
func PressEnterToContinue(text string) { func PressEnterToContinue(text string) {

15
ooni.go
View File

@ -10,7 +10,6 @@ import (
"github.com/ooni/probe-cli/internal/database" "github.com/ooni/probe-cli/internal/database"
"github.com/ooni/probe-cli/internal/enginex" "github.com/ooni/probe-cli/internal/enginex"
"github.com/ooni/probe-cli/internal/legacy" "github.com/ooni/probe-cli/internal/legacy"
"github.com/ooni/probe-cli/internal/onboard"
"github.com/ooni/probe-cli/utils" "github.com/ooni/probe-cli/utils"
"github.com/ooni/probe-cli/version" "github.com/ooni/probe-cli/version"
engine "github.com/ooni/probe-engine" engine "github.com/ooni/probe-engine"
@ -37,20 +36,6 @@ func (c *Context) MaybeLocationLookup() error {
return c.Session.MaybeLookupLocation() return c.Session.MaybeLookupLocation()
} }
// MaybeOnboarding will run the onboarding process only if the informed consent
// config option is set to false
func (c *Context) MaybeOnboarding() error {
if c.Config.InformedConsent == false {
if c.IsBatch == true {
return errors.New("cannot run onboarding in batch mode")
}
if err := onboard.Onboarding(c.Config); err != nil {
return errors.Wrap(err, "onboarding")
}
}
return nil
}
// Init the OONI manager // Init the OONI manager
func (c *Context) Init() error { func (c *Context) Init() error {
var err error var err error

View File

@ -1,4 +1,4 @@
package util package utils
import ( import (
"testing" "testing"

View File

@ -1,4 +1,4 @@
package util package utils
import ( import (
"bytes" "bytes"