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
+2 -1
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
}
+11 -8
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
}
}