Merge pull request #10 from OpenObservatory/refactor/paths
Move all directory related functionality into paths utils
This commit is contained in:
commit
75c8094738
|
@ -1,13 +1,13 @@
|
||||||
package database
|
package database
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/apex/log"
|
"github.com/apex/log"
|
||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
|
"github.com/openobservatory/gooni/utils"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -247,29 +247,12 @@ func (r *Result) Finished(db *sqlx.DB, makeSummary ResultSummaryFunc) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// MakeResultsPath creates and returns a directory for the result
|
|
||||||
func MakeResultsPath(home string, r *Result) (string, error) {
|
|
||||||
p := filepath.Join(home, "msmts",
|
|
||||||
fmt.Sprintf("%s-%s", r.Name, r.StartTime.Format(time.RFC3339Nano)))
|
|
||||||
|
|
||||||
// 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
|
|
||||||
}
|
|
||||||
|
|
||||||
// CreateResult writes the Result to the database a returns a pointer
|
// CreateResult writes the Result to the database a returns a pointer
|
||||||
// to the Result
|
// to the Result
|
||||||
func CreateResult(db *sqlx.DB, homePath string, r Result) (*Result, error) {
|
func CreateResult(db *sqlx.DB, homePath string, r Result) (*Result, error) {
|
||||||
log.Debugf("Creating result %v", r)
|
log.Debugf("Creating result %v", r)
|
||||||
|
|
||||||
p, err := MakeResultsPath(homePath, &r)
|
p, err := utils.MakeResultsDir(homePath, r.Name, r.StartTime)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
13
ooni.go
13
ooni.go
|
@ -59,9 +59,9 @@ func (c *Context) MaybeLocationLookup() error {
|
||||||
func (c *Context) LocationLookup() error {
|
func (c *Context) LocationLookup() error {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
dbPath := filepath.Join(c.Home, "geoip")
|
geoipDir := utils.GeoIPDir(c.Home)
|
||||||
|
|
||||||
c.Location, err = utils.GeoIPLookup(dbPath)
|
c.Location, err = utils.GeoIPLookup(geoipDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -92,7 +92,7 @@ func (c *Context) Init() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
c.dbPath = filepath.Join(c.Home, "db", "main.sqlite3")
|
c.dbPath = utils.DBDir(c.Home, "main")
|
||||||
if c.Config.InformedConsent == false {
|
if c.Config.InformedConsent == false {
|
||||||
if err = Onboarding(c.Config); err != nil {
|
if err = Onboarding(c.Config); err != nil {
|
||||||
return errors.Wrap(err, "onboarding")
|
return errors.Wrap(err, "onboarding")
|
||||||
|
@ -215,11 +215,10 @@ func ParseConfig(b []byte) (*Config, error) {
|
||||||
// MaybeInitializeHome does the setup for a new OONI Home
|
// MaybeInitializeHome does the setup for a new OONI Home
|
||||||
func MaybeInitializeHome(home string) error {
|
func MaybeInitializeHome(home string) error {
|
||||||
firstRun := false
|
firstRun := false
|
||||||
requiredDirs := []string{"db", "msmts", "geoip"}
|
for _, d := range utils.RequiredDirs(home) {
|
||||||
for _, d := range requiredDirs {
|
if _, e := os.Stat(d); e != nil {
|
||||||
if _, e := os.Stat(filepath.Join(home, d)); e != nil {
|
|
||||||
firstRun = true
|
firstRun = true
|
||||||
if err := os.MkdirAll(filepath.Join(home, d), 0700); err != nil {
|
if err := os.MkdirAll(d, 0700); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,11 +36,6 @@ var legacyGeoipFiles = map[string]string{
|
||||||
"GeoIP.dat": "http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz",
|
"GeoIP.dat": "http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz",
|
||||||
}
|
}
|
||||||
|
|
||||||
// GeoIPDir returns the geoip data dir for the given OONI Home
|
|
||||||
func GeoIPDir(home string) string {
|
|
||||||
return filepath.Join(home, "geoip")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Download the file to a temporary location
|
// Download the file to a temporary location
|
||||||
func downloadToTemp(url string) (string, error) {
|
func downloadToTemp(url string) (string, error) {
|
||||||
out, err := ioutil.TempFile(os.TempDir(), "maxmind")
|
out, err := ioutil.TempFile(os.TempDir(), "maxmind")
|
||||||
|
|
46
utils/paths.go
Normal file
46
utils/paths.go
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// RequiredDirs returns the required ooni home directories
|
||||||
|
func RequiredDirs(home string) []string {
|
||||||
|
requiredDirs := []string{}
|
||||||
|
requiredSubdirs := []string{"db", "msmts", "geoip"}
|
||||||
|
for _, d := range requiredSubdirs {
|
||||||
|
requiredDirs = append(requiredDirs, filepath.Join(home, d))
|
||||||
|
}
|
||||||
|
return requiredDirs
|
||||||
|
}
|
||||||
|
|
||||||
|
// GeoIPDir returns the geoip data dir for the given OONI Home
|
||||||
|
func GeoIPDir(home string) string {
|
||||||
|
return filepath.Join(home, "geoip")
|
||||||
|
}
|
||||||
|
|
||||||
|
// DBDir returns the database dir for the given name
|
||||||
|
func DBDir(home string, name string) string {
|
||||||
|
return filepath.Join(home, "db", fmt.Sprintf("%s.sqlite3", name))
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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(time.RFC3339Nano)))
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user