Make path and homedir related logic more robust

Add ability to pass OONI_HOME environment variable
This commit is contained in:
Arturo Filastò 2018-05-21 17:33:59 -07:00
parent e0ac7b337b
commit c620bc9726
5 changed files with 32 additions and 23 deletions

View File

@ -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
}

View File

@ -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
}
}

View File

@ -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

14
ooni.go
View File

@ -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
}

View File

@ -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
}