From 8d5da14c4bc8ffbaf310348e2110ec6d4f688716 Mon Sep 17 00:00:00 2001 From: Will Scott Date: Thu, 8 Mar 2018 02:25:40 -0800 Subject: [PATCH] Initial creation of the '.ooni/db' folders when not yet setup --- internal/database/database.go | 24 +++++++++++++++++++----- ooni.go | 6 +++++- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/internal/database/database.go b/internal/database/database.go index 84abc07..0421341 100644 --- a/internal/database/database.go +++ b/internal/database/database.go @@ -1,6 +1,7 @@ package database import ( + "os" "path/filepath" "github.com/apex/log" @@ -29,16 +30,29 @@ func RunMigrations(db *sqlx.DB) error { } // Connect to the database -func Connect(path string) (*sqlx.DB, error) { - db, err := sqlx.Connect("sqlite3", path) +func Connect(path string) (db *sqlx.DB, err error) { + home, err := ooni.GetOONIHome() if err != nil { - return nil, err + return } + + if _, e := os.Stat(filepath.Join(home, "db")); e != nil { + err = os.MkdirAll(filepath.Join(home, "db"), 0700) + if err != nil { + return + } + } + + db, err = sqlx.Connect("sqlite3", path) + if err != nil { + return + } + err = RunMigrations(db) if err != nil { - return nil, err + db = nil } - return db, nil + return } // DefaultDatabasePath for the main database diff --git a/ooni.go b/ooni.go index 1fd6b0e..e8b20c4 100644 --- a/ooni.go +++ b/ooni.go @@ -154,6 +154,8 @@ func ReadConfig(path string) (*Config, error) { if err = c.Validate(); err != nil { return nil, errors.Wrap(err, "validating") } + + return c, nil } if err != nil { @@ -186,5 +188,7 @@ func ReadDefaultConfigPaths() (*Config, error) { return c, nil } } - return nil, errors.New("failed to find a config") + + // Run from the default config + return ReadConfig(paths[0]) }