diff --git a/cmd/ooniprobe/internal/cli/list/list.go b/cmd/ooniprobe/internal/cli/list/list.go index 779f1e2..4d847a0 100644 --- a/cmd/ooniprobe/internal/cli/list/list.go +++ b/cmd/ooniprobe/internal/cli/list/list.go @@ -6,8 +6,8 @@ import ( "github.com/alecthomas/kingpin" "github.com/apex/log" "github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/cli/root" - "github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/database" "github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/output" + "github.com/ooni/probe-cli/v3/internal/database" ) func init() { diff --git a/cmd/ooniprobe/internal/cli/rm/rm.go b/cmd/ooniprobe/internal/cli/rm/rm.go index d3a5795..4dafb54 100644 --- a/cmd/ooniprobe/internal/cli/rm/rm.go +++ b/cmd/ooniprobe/internal/cli/rm/rm.go @@ -8,7 +8,7 @@ import ( "github.com/alecthomas/kingpin" "github.com/apex/log" "github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/cli/root" - "github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/database" + "github.com/ooni/probe-cli/v3/internal/database" "github.com/upper/db/v4" ) diff --git a/cmd/ooniprobe/internal/cli/show/show.go b/cmd/ooniprobe/internal/cli/show/show.go index 4db420b..973c053 100644 --- a/cmd/ooniprobe/internal/cli/show/show.go +++ b/cmd/ooniprobe/internal/cli/show/show.go @@ -4,8 +4,8 @@ import ( "github.com/alecthomas/kingpin" "github.com/apex/log" "github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/cli/root" - "github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/database" "github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/output" + "github.com/ooni/probe-cli/v3/internal/database" ) func init() { diff --git a/cmd/ooniprobe/internal/log/handlers/cli/results.go b/cmd/ooniprobe/internal/log/handlers/cli/results.go index 9350bdd..ca6f0ed 100644 --- a/cmd/ooniprobe/internal/log/handlers/cli/results.go +++ b/cmd/ooniprobe/internal/log/handlers/cli/results.go @@ -8,8 +8,8 @@ import ( "time" "github.com/apex/log" - "github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/database" "github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/utils" + "github.com/ooni/probe-cli/v3/internal/database" ) func formatSpeed(speed float64) string { diff --git a/cmd/ooniprobe/internal/nettests/nettests.go b/cmd/ooniprobe/internal/nettests/nettests.go index 3809f8b..a13b168 100644 --- a/cmd/ooniprobe/internal/nettests/nettests.go +++ b/cmd/ooniprobe/internal/nettests/nettests.go @@ -8,9 +8,9 @@ import ( "github.com/apex/log" "github.com/fatih/color" - "github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/database" "github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/ooni" "github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/output" + "github.com/ooni/probe-cli/v3/internal/database" engine "github.com/ooni/probe-cli/v3/internal/engine" "github.com/ooni/probe-cli/v3/internal/model" "github.com/pkg/errors" diff --git a/cmd/ooniprobe/internal/nettests/nettests_test.go b/cmd/ooniprobe/internal/nettests/nettests_test.go index 99792c5..6d8d5bf 100644 --- a/cmd/ooniprobe/internal/nettests/nettests_test.go +++ b/cmd/ooniprobe/internal/nettests/nettests_test.go @@ -7,8 +7,8 @@ import ( "path" "testing" - "github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/database" "github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/ooni" + "github.com/ooni/probe-cli/v3/internal/database" "github.com/ooni/probe-cli/v3/internal/model" ) diff --git a/cmd/ooniprobe/internal/nettests/run.go b/cmd/ooniprobe/internal/nettests/run.go index 5b28acc..eef842c 100644 --- a/cmd/ooniprobe/internal/nettests/run.go +++ b/cmd/ooniprobe/internal/nettests/run.go @@ -7,8 +7,8 @@ import ( "time" "github.com/apex/log" - "github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/database" "github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/ooni" + "github.com/ooni/probe-cli/v3/internal/database" "github.com/ooni/probe-cli/v3/internal/model" "github.com/pkg/errors" ) diff --git a/cmd/ooniprobe/internal/ooni/ooni.go b/cmd/ooniprobe/internal/ooni/ooni.go index 7db8dd9..338863d 100644 --- a/cmd/ooniprobe/internal/ooni/ooni.go +++ b/cmd/ooniprobe/internal/ooni/ooni.go @@ -11,10 +11,9 @@ import ( "github.com/apex/log" "github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/config" - "github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/database" - "github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/enginex" "github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/utils" "github.com/ooni/probe-cli/v3/internal/atomicx" + "github.com/ooni/probe-cli/v3/internal/database" "github.com/ooni/probe-cli/v3/internal/engine" "github.com/ooni/probe-cli/v3/internal/kvstore" "github.com/ooni/probe-cli/v3/internal/legacy/assetsdir" @@ -26,6 +25,11 @@ import ( // DefaultSoftwareName is the default software name. const DefaultSoftwareName = "ooniprobe-cli" +// logger is the logger used by the engine. +var logger = log.WithFields(log.Fields{ + "type": "engine", +}) + // ProbeCLI is the OONI Probe CLI context. type ProbeCLI interface { Config() *config.Config @@ -231,7 +235,7 @@ func (p *Probe) NewSession(ctx context.Context, runType model.RunType) (*engine. } return engine.NewSession(ctx, engine.SessionConfig{ KVStore: kvstore, - Logger: enginex.Logger, + Logger: logger, SoftwareName: softwareName, SoftwareVersion: p.softwareVersion, TempDir: p.tempDir, diff --git a/cmd/ooniprobe/internal/output/output.go b/cmd/ooniprobe/internal/output/output.go index 3c21268..44bbfa1 100644 --- a/cmd/ooniprobe/internal/output/output.go +++ b/cmd/ooniprobe/internal/output/output.go @@ -8,7 +8,7 @@ import ( "github.com/apex/log" "github.com/mitchellh/go-wordwrap" - "github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/database" + "github.com/ooni/probe-cli/v3/internal/database" ) // MeasurementJSON prints the JSON of a measurement diff --git a/cmd/ooniprobe/internal/utils/paths.go b/cmd/ooniprobe/internal/utils/paths.go index a9aea3f..ca5f88e 100644 --- a/cmd/ooniprobe/internal/utils/paths.go +++ b/cmd/ooniprobe/internal/utils/paths.go @@ -1,11 +1,9 @@ package utils import ( - "errors" "fmt" "os" "path/filepath" - "time" "github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/utils/homedir" ) @@ -53,26 +51,6 @@ func FileExists(path string) bool { return err == nil && stat.Mode().IsRegular() } -// ResultTimestamp is a windows friendly timestamp -const ResultTimestamp = "2006-01-02T150405.999999999Z0700" - -// MakeResultsDir creates and returns a directory for the result -func MakeResultsDir(home string, name string, ts time.Time) (string, error) { - p := filepath.Join(home, "msmts", - fmt.Sprintf("%s-%s", name, ts.Format(ResultTimestamp))) - - // If the path already exists, this is a problem. It should not clash, because - // we are using nanosecond precision for the starttime. - if _, e := os.Stat(p); e == nil { - return "", errors.New("results path already exists") - } - err := os.MkdirAll(p, 0700) - if err != nil { - return "", err - } - return p, nil -} - // GetOONIHome returns the path to the OONI Home func GetOONIHome() (string, error) { if ooniHome := os.Getenv("OONI_HOME"); ooniHome != "" { @@ -96,8 +74,5 @@ func DidLegacyInformedConsent() bool { } path := filepath.Join(filepath.Join(home, ".ooni"), "initialized") - if FileExists(path) { - return true - } - return false + return FileExists(path) } diff --git a/cmd/ooniprobe/internal/database/actions.go b/internal/database/actions.go similarity index 96% rename from cmd/ooniprobe/internal/database/actions.go rename to internal/database/actions.go index 1aca48d..c94b9f1 100644 --- a/cmd/ooniprobe/internal/database/actions.go +++ b/internal/database/actions.go @@ -12,8 +12,7 @@ import ( "time" "github.com/apex/log" - "github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/enginex" - "github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/utils" + "github.com/ooni/probe-cli/v3/internal/engine" "github.com/pkg/errors" "github.com/upper/db/v4" ) @@ -65,7 +64,7 @@ func GetMeasurementJSON(sess db.Session, measurementID int64) (map[string]interf } query := url.Values{} query.Add("report_id", reportID) - if measurement.URL.URL.Valid == true { + if measurement.URL.URL.Valid { query.Add("input", measurement.URL.URL.String) } measurementURL.RawQuery = query.Encode() @@ -84,7 +83,7 @@ func GetMeasurementJSON(sess db.Session, measurementID int64) (map[string]interf } // MeasurementFilePath might be NULL because the measurement from a // 3.0.0-beta install - if measurement.Measurement.MeasurementFilePath.Valid == false { + if !measurement.Measurement.MeasurementFilePath.Valid { log.Error("invalid measurement_file_path") log.Error("backup your OONI_HOME and run `ooniprobe reset`") return nil, errors.New("cannot access measurement file") @@ -254,7 +253,7 @@ func CreateMeasurement(sess db.Session, reportID sql.NullString, testName string func CreateResult(sess db.Session, homePath string, testGroupName string, networkID int64) (*Result, error) { startTime := time.Now().UTC() - p, err := utils.MakeResultsDir(homePath, testGroupName, startTime) + p, err := makeResultsDir(homePath, testGroupName, startTime) if err != nil { return nil, err } @@ -276,7 +275,7 @@ func CreateResult(sess db.Session, homePath string, testGroupName string, networ } // CreateNetwork will create a new network in the network table -func CreateNetwork(sess db.Session, loc enginex.LocationProvider) (*Network, error) { +func CreateNetwork(sess db.Session, loc engine.LocationProvider) (*Network, error) { network := Network{ ASN: loc.ProbeASN(), CountryCode: loc.ProbeCC(), @@ -351,7 +350,7 @@ func AddTestKeys(sess db.Session, msmt *Measurement, tk interface{}) error { // the IsAnomaly field of bool type. // Maybe generics are not so bad after-all, heh golang? isAnomalyValue := reflect.ValueOf(tk).FieldByName("IsAnomaly") - if isAnomalyValue.IsValid() == true && isAnomalyValue.Kind() == reflect.Bool { + if isAnomalyValue.IsValid() && isAnomalyValue.Kind() == reflect.Bool { isAnomaly = isAnomalyValue.Bool() isAnomalyValid = true } diff --git a/cmd/ooniprobe/internal/database/actions_test.go b/internal/database/actions_test.go similarity index 100% rename from cmd/ooniprobe/internal/database/actions_test.go rename to internal/database/actions_test.go diff --git a/cmd/ooniprobe/internal/database/database.go b/internal/database/database.go similarity index 100% rename from cmd/ooniprobe/internal/database/database.go rename to internal/database/database.go diff --git a/cmd/ooniprobe/internal/database/database_test.go b/internal/database/database_test.go similarity index 100% rename from cmd/ooniprobe/internal/database/database_test.go rename to internal/database/database_test.go diff --git a/cmd/ooniprobe/internal/database/migrations/1_create_msmt_results.sql b/internal/database/migrations/1_create_msmt_results.sql similarity index 100% rename from cmd/ooniprobe/internal/database/migrations/1_create_msmt_results.sql rename to internal/database/migrations/1_create_msmt_results.sql diff --git a/cmd/ooniprobe/internal/database/migrations/2_single_msmt_file.sql b/internal/database/migrations/2_single_msmt_file.sql similarity index 100% rename from cmd/ooniprobe/internal/database/migrations/2_single_msmt_file.sql rename to internal/database/migrations/2_single_msmt_file.sql diff --git a/cmd/ooniprobe/internal/database/migrations/3_results_is_uploaded.sql b/internal/database/migrations/3_results_is_uploaded.sql similarity index 100% rename from cmd/ooniprobe/internal/database/migrations/3_results_is_uploaded.sql rename to internal/database/migrations/3_results_is_uploaded.sql diff --git a/cmd/ooniprobe/internal/database/models.go b/internal/database/models.go similarity index 99% rename from cmd/ooniprobe/internal/database/models.go rename to internal/database/models.go index e8df73f..4d45e18 100644 --- a/cmd/ooniprobe/internal/database/models.go +++ b/internal/database/models.go @@ -99,7 +99,7 @@ type PerformanceTestKeys struct { // Finished marks the result as done and sets the runtime func (r *Result) Finished(sess db.Session) error { - if r.IsDone == true || r.Runtime != 0 { + if r.IsDone || r.Runtime != 0 { return errors.New("Result is already finished") } r.Runtime = time.Now().UTC().Sub(r.StartTime).Seconds() diff --git a/internal/database/utils.go b/internal/database/utils.go new file mode 100644 index 0000000..8c6fda6 --- /dev/null +++ b/internal/database/utils.go @@ -0,0 +1,29 @@ +package database + +import ( + "errors" + "fmt" + "os" + "path/filepath" + "time" +) + +// resultTimestamp is a windows friendly timestamp +const resultTimestamp = "2006-01-02T150405.999999999Z0700" + +// makeResultsDir creates and returns a directory for the result +func makeResultsDir(home string, name string, ts time.Time) (string, error) { + p := filepath.Join(home, "msmts", + fmt.Sprintf("%s-%s", name, ts.Format(resultTimestamp))) + + // If the path already exists, this is a problem. It should not clash, because + // we are using nanosecond precision for the starttime. + if _, e := os.Stat(p); e == nil { + return "", errors.New("results path already exists") + } + err := os.MkdirAll(p, 0700) + if err != nil { + return "", err + } + return p, nil +} diff --git a/cmd/ooniprobe/internal/enginex/enginex.go b/internal/engine/location.go similarity index 59% rename from cmd/ooniprobe/internal/enginex/enginex.go rename to internal/engine/location.go index 8071334..40a2c72 100644 --- a/cmd/ooniprobe/internal/enginex/enginex.go +++ b/internal/engine/location.go @@ -1,14 +1,4 @@ -// Package enginex contains ooni/probe-engine extensions. -package enginex - -import ( - "github.com/apex/log" -) - -// Logger is the logger used by the engine. -var Logger = log.WithFields(log.Fields{ - "type": "engine", -}) +package engine // LocationProvider is an interface that returns the current location. The // github.com/ooni/probe-cli/v3/internal/engine/session.Session implements it.