Refactor how we create the context
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
package geoip
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
|
||||
"github.com/alecthomas/kingpin"
|
||||
"github.com/apex/log"
|
||||
"github.com/openobservatory/gooni/internal/cli/root"
|
||||
"github.com/openobservatory/gooni/utils"
|
||||
)
|
||||
|
||||
func init() {
|
||||
cmd := root.Command("geoip", "Perform a geoip lookup")
|
||||
|
||||
shouldUpdate := cmd.Flag("update", "Update the geoip database").Bool()
|
||||
|
||||
cmd.Action(func(_ *kingpin.ParseContext) error {
|
||||
log.Info("geoip")
|
||||
ctx, err := root.Init()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
geoipPath := filepath.Join(ctx.Home, "geoip")
|
||||
|
||||
if *shouldUpdate {
|
||||
utils.DownloadGeoIPDatabaseFiles(geoipPath)
|
||||
}
|
||||
|
||||
loc, err := utils.GeoIPLookup(geoipPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.WithFields(log.Fields{
|
||||
"asn": loc.ASN,
|
||||
"network_name": loc.NetworkName,
|
||||
"country_code": loc.CountryCode,
|
||||
"ip": loc.IP,
|
||||
}).Info("Looked up your location")
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"github.com/alecthomas/kingpin"
|
||||
"github.com/apex/log"
|
||||
ooni "github.com/openobservatory/gooni"
|
||||
"github.com/openobservatory/gooni/internal/database"
|
||||
"github.com/openobservatory/gooni/internal/log/handlers/batch"
|
||||
"github.com/openobservatory/gooni/internal/log/handlers/cli"
|
||||
"github.com/prometheus/common/version"
|
||||
@@ -17,7 +16,7 @@ var Cmd = kingpin.New("ooni", "")
|
||||
var Command = Cmd.Command
|
||||
|
||||
// Init should be called by all subcommand that care to have a ooni.OONI instance
|
||||
var Init func() (*ooni.Config, *ooni.Context, error)
|
||||
var Init func() (*ooni.Context, error)
|
||||
|
||||
func init() {
|
||||
configPath := Cmd.Flag("config", "Set a custom config file path").Short('c').String()
|
||||
@@ -36,39 +35,21 @@ func init() {
|
||||
log.Debugf("ooni version %s", version.Version)
|
||||
}
|
||||
|
||||
Init = func() (*ooni.Config, *ooni.Context, error) {
|
||||
var config *ooni.Config
|
||||
Init = func() (*ooni.Context, error) {
|
||||
var err error
|
||||
|
||||
if *configPath != "" {
|
||||
log.Debugf("Reading config file from %s", *configPath)
|
||||
config, err = ooni.ReadConfig(*configPath)
|
||||
} else {
|
||||
log.Debug("Reading default config file")
|
||||
config, err = ooni.ReadDefaultConfigPaths()
|
||||
}
|
||||
homePath, err := ooni.GetOONIHome()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
dbPath, err := database.DefaultDatabasePath()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
log.Debugf("Connecting to database sqlite3://%s", dbPath)
|
||||
db, err := database.Connect(dbPath)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
ctx := ooni.New(config, db)
|
||||
ctx := ooni.NewContext(*configPath, homePath)
|
||||
err = ctx.Init()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return config, ctx, nil
|
||||
return ctx, nil
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
@@ -21,7 +21,7 @@ func init() {
|
||||
|
||||
cmd.Action(func(_ *kingpin.ParseContext) error {
|
||||
log.Infof("Starting %s", *nettestGroup)
|
||||
_, ctx, err := root.Init()
|
||||
ctx, err := root.Init()
|
||||
if err != nil {
|
||||
log.Errorf("%s", err)
|
||||
return err
|
||||
@@ -33,7 +33,7 @@ func init() {
|
||||
}
|
||||
log.Debugf("Running test group %s", group.Label)
|
||||
|
||||
result, err := database.CreateResult(ctx.DB, database.Result{
|
||||
result, err := database.CreateResult(ctx.DB, ctx.Home, database.Result{
|
||||
Name: *nettestGroup,
|
||||
StartTime: time.Now().UTC(),
|
||||
})
|
||||
|
||||
@@ -1,14 +1,10 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
|
||||
"github.com/apex/log"
|
||||
"github.com/jmoiron/sqlx"
|
||||
_ "github.com/mattn/go-sqlite3" // this is needed to load the sqlite3 driver
|
||||
ooni "github.com/openobservatory/gooni"
|
||||
"github.com/openobservatory/gooni/internal/bindata"
|
||||
"github.com/pkg/errors"
|
||||
migrate "github.com/rubenv/sql-migrate"
|
||||
)
|
||||
|
||||
@@ -41,12 +37,3 @@ func Connect(path string) (db *sqlx.DB, err error) {
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// DefaultDatabasePath for the main database
|
||||
func DefaultDatabasePath() (string, error) {
|
||||
home, err := ooni.GetOONIHome()
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "default database path")
|
||||
}
|
||||
return filepath.Join(home, "db", "main.sqlite3"), nil
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@ import (
|
||||
|
||||
"github.com/apex/log"
|
||||
"github.com/jmoiron/sqlx"
|
||||
ooni "github.com/openobservatory/gooni"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
@@ -238,11 +237,7 @@ func (r *Result) Finished(db *sqlx.DB, makeSummary ResultSummaryFunc) error {
|
||||
}
|
||||
|
||||
// MakeResultsPath creates and returns a directory for the result
|
||||
func MakeResultsPath(r *Result) (string, error) {
|
||||
home, err := ooni.GetOONIHome()
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "default measurements path")
|
||||
}
|
||||
func MakeResultsPath(home string, r *Result) (string, error) {
|
||||
p := filepath.Join(home, "msmts",
|
||||
fmt.Sprintf("%s-%s", r.Name, r.StartTime.Format(time.RFC3339Nano)))
|
||||
|
||||
@@ -251,7 +246,7 @@ func MakeResultsPath(r *Result) (string, error) {
|
||||
if _, e := os.Stat(p); e == nil {
|
||||
return "", errors.New("results path already exists")
|
||||
}
|
||||
err = os.MkdirAll(p, 0700)
|
||||
err := os.MkdirAll(p, 0700)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -260,10 +255,10 @@ func MakeResultsPath(r *Result) (string, error) {
|
||||
|
||||
// CreateResult writes the Result to the database a returns a pointer
|
||||
// to the Result
|
||||
func CreateResult(db *sqlx.DB, r Result) (*Result, error) {
|
||||
func CreateResult(db *sqlx.DB, homePath string, r Result) (*Result, error) {
|
||||
log.Debugf("Creating result %v", r)
|
||||
|
||||
p, err := MakeResultsPath(&r)
|
||||
p, err := MakeResultsPath(homePath, &r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user