Refactor how we create the context

This commit is contained in:
Arturo Filastò
2018-03-23 12:10:14 +01:00
parent 6ba779b156
commit 7fe1551951
6 changed files with 105 additions and 76 deletions
+7 -26
View File
@@ -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