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
-13
View File
@@ -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
}
+4 -9
View File
@@ -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
}