From 62b515e2e0b6f9ed96284fed28300327ef604168 Mon Sep 17 00:00:00 2001 From: Will Scott Date: Thu, 8 Mar 2018 11:46:31 -0800 Subject: [PATCH] pull out directory creation to new function --- ooni.go | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/ooni.go b/ooni.go index 941f21a..25ce528 100644 --- a/ooni.go +++ b/ooni.go @@ -119,13 +119,6 @@ func (c *Config) Default() error { return err } - if _, e := os.Stat(filepath.Join(home, "db")); e != nil { - err = os.MkdirAll(filepath.Join(home, "db"), 0700) - if err != nil { - return err - } - } - c.path = filepath.Join(home, "config.json") return nil } @@ -154,11 +147,29 @@ func ParseConfig(b []byte) (*Config, error) { return c, nil } +func EnsureDefaultConfigDir() error { + home, err := GetOONIHome() + if err != nil { + return err + } + + if _, e := os.Stat(filepath.Join(home, "db")); e != nil { + err = os.MkdirAll(filepath.Join(home, "db"), 0700) + if err != nil { + return err + } + } + return nil +} + // ReadConfig reads the configuration from the path func ReadConfig(path string) (*Config, error) { b, err := ioutil.ReadFile(path) if os.IsNotExist(err) { + if err = EnsureDefaultConfigDir(); err != nil { + return nil, errors.Wrap(err, "Creating new config") + } c := &Config{} if err = c.Default(); err != nil {