From c620bc97267f753527ec9da373f1ae2050bd6b2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arturo=20Filast=C3=B2?= Date: Mon, 21 May 2018 17:33:59 -0700 Subject: [PATCH] Make path and homedir related logic more robust Add ability to pass OONI_HOME environment variable --- internal/cli/root/root.go | 3 ++- internal/legacy/legacy.go | 19 +++++++++++-------- nettests/nettests.go | 2 +- ooni.go | 14 +------------- utils/paths.go | 17 +++++++++++++++++ 5 files changed, 32 insertions(+), 23 deletions(-) diff --git a/internal/cli/root/root.go b/internal/cli/root/root.go index 124b0d2..5777f10 100644 --- a/internal/cli/root/root.go +++ b/internal/cli/root/root.go @@ -6,6 +6,7 @@ import ( ooni "github.com/ooni/probe-cli" "github.com/ooni/probe-cli/internal/log/handlers/batch" "github.com/ooni/probe-cli/internal/log/handlers/cli" + "github.com/ooni/probe-cli/utils" "github.com/prometheus/common/version" ) @@ -38,7 +39,7 @@ func init() { Init = func() (*ooni.Context, error) { var err error - homePath, err := ooni.GetOONIHome() + homePath, err := utils.GetOONIHome() if err != nil { return nil, err } diff --git a/internal/legacy/legacy.go b/internal/legacy/legacy.go index 1877ddc..27dac4c 100644 --- a/internal/legacy/legacy.go +++ b/internal/legacy/legacy.go @@ -5,13 +5,13 @@ import ( "os" "path/filepath" - homedir "github.com/mitchellh/go-homedir" + "github.com/ooni/probe-cli/utils/homedir" "github.com/pkg/errors" "gopkg.in/AlecAivazis/survey.v1" ) // HomePath returns the path to the OONI Home -func HomePath() (string, error) { +func homePath() (string, error) { home, err := homedir.Dir() if err != nil { return "", err @@ -20,8 +20,11 @@ func HomePath() (string, error) { } // HomeExists returns true if a legacy home exists -func HomeExists() (bool, error) { - home, err := HomePath() +func homeExists() (bool, error) { + home, err := homePath() + if err == homedir.ErrNoHomeDir { + return false, nil + } if err != nil { return false, err } @@ -33,7 +36,7 @@ func HomeExists() (bool, error) { } // BackupHome the legacy home directory -func BackupHome() error { +func backupHome() error { home, err := homedir.Dir() if err != nil { return errors.Wrap(err, "backing up home") @@ -48,14 +51,14 @@ func BackupHome() error { // MaybeMigrateHome prompts the user if we should backup the legacy home func MaybeMigrateHome() error { - exists, err := HomeExists() + exists, err := homeExists() if err != nil { return err } if !exists { return nil } - home, err := HomePath() + home, err := homePath() if err != nil { return err } @@ -72,7 +75,7 @@ func MaybeMigrateHome() error { } } else { logf("Backing up ~/.ooni to ~/.ooni-legacy") - if err := BackupHome(); err != nil { + if err := backupHome(); err != nil { return err } } diff --git a/nettests/nettests.go b/nettests/nettests.go index 70a63de..0d36fca 100644 --- a/nettests/nettests.go +++ b/nettests/nettests.go @@ -43,7 +43,7 @@ type Controller struct { msmtPath string // XXX maybe we can drop this and just use a temporary file } -func getCaBundlePath() { +func getCaBundlePath() string { path := os.Getenv("SSL_CERT_FILE") if path != "" { return path diff --git a/ooni.go b/ooni.go index a134a9d..051e051 100644 --- a/ooni.go +++ b/ooni.go @@ -9,7 +9,6 @@ import ( "github.com/apex/log" "github.com/jmoiron/sqlx" - homedir "github.com/mitchellh/go-homedir" "github.com/ooni/probe-cli/config" "github.com/ooni/probe-cli/internal/database" "github.com/ooni/probe-cli/internal/legacy" @@ -124,17 +123,6 @@ func NewContext(configPath string, homePath string) *Context { } } -// GetOONIHome returns the path to the OONI Home -func GetOONIHome() (string, error) { - home, err := homedir.Dir() - if err != nil { - return "", err - } - - path := filepath.Join(home, ".ooni") - return path, nil -} - // Config for the OONI Probe installation type Config struct { // Private settings @@ -179,7 +167,7 @@ func (c *Config) Unlock() { // Default config settings func (c *Config) Default() error { - home, err := GetOONIHome() + home, err := utils.GetOONIHome() if err != nil { return err } diff --git a/utils/paths.go b/utils/paths.go index 2699f88..cf37c86 100644 --- a/utils/paths.go +++ b/utils/paths.go @@ -6,6 +6,8 @@ import ( "os" "path/filepath" "time" + + "github.com/ooni/probe-cli/utils/homedir" ) // RequiredDirs returns the required ooni home directories @@ -46,3 +48,18 @@ func MakeResultsDir(home string, name string, ts time.Time) (string, error) { } return p, nil } + +// GetOONIHome returns the path to the OONI Home +func GetOONIHome() (string, error) { + if ooniHome := os.Getenv("OONI_HOME"); ooniHome != "" { + return ooniHome, nil + } + + home, err := homedir.Dir() + if err != nil { + return "", err + } + + path := filepath.Join(home, ".ooni") + return path, nil +}