Refactor the EnsureDefaultConfigDir() function
This commit is contained in:
parent
1d40171e04
commit
efb7e87d1b
14
ooni.go
14
ooni.go
|
@ -147,19 +147,20 @@ func ParseConfig(b []byte) (*Config, error) {
|
||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func EnsureDefaultConfigDir() error {
|
//EnsureDefaultOONIHomeDir makes sure the paths to the OONI Home exist
|
||||||
|
func EnsureDefaultOONIHomeDir() (string, error) {
|
||||||
home, err := GetOONIHome()
|
home, err := GetOONIHome()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, e := os.Stat(filepath.Join(home, "db")); e != nil {
|
if _, e := os.Stat(filepath.Join(home, "db")); e != nil {
|
||||||
err = os.MkdirAll(filepath.Join(home, "db"), 0700)
|
err = os.MkdirAll(filepath.Join(home, "db"), 0700)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return "", err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return home, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadConfig reads the configuration from the path
|
// ReadConfig reads the configuration from the path
|
||||||
|
@ -167,9 +168,6 @@ func ReadConfig(path string) (*Config, error) {
|
||||||
b, err := ioutil.ReadFile(path)
|
b, err := ioutil.ReadFile(path)
|
||||||
|
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
if err = EnsureDefaultConfigDir(); err != nil {
|
|
||||||
return nil, errors.Wrap(err, "Creating new config")
|
|
||||||
}
|
|
||||||
c := &Config{}
|
c := &Config{}
|
||||||
|
|
||||||
if err = c.Default(); err != nil {
|
if err = c.Default(); err != nil {
|
||||||
|
@ -197,7 +195,7 @@ func ReadConfig(path string) (*Config, error) {
|
||||||
|
|
||||||
// ReadDefaultConfigPaths from common locations.
|
// ReadDefaultConfigPaths from common locations.
|
||||||
func ReadDefaultConfigPaths() (*Config, error) {
|
func ReadDefaultConfigPaths() (*Config, error) {
|
||||||
home, err := GetOONIHome()
|
home, err := EnsureDefaultOONIHomeDir()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "reading default config paths")
|
return nil, errors.Wrap(err, "reading default config paths")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user