diff options
author | Matthew Lemon <lemon@matthewlemon.com> | 2020-07-23 21:23:29 +0100 |
---|---|---|
committer | Matthew Lemon <lemon@matthewlemon.com> | 2020-07-23 21:23:29 +0100 |
commit | e68c3d4532816b54a7dce271ecbe345a685bd651 (patch) | |
tree | 6f7b67bb783064a80697dce62ccd6a2348504a5c | |
parent | 3b82b7463987ff637530cb403b6d294978bbd115 (diff) |
main is getting smaller
-rw-r--r-- | cmd/datamaps/main.go | 43 | ||||
-rw-r--r-- | pkg/datamaps/config.go | 35 |
2 files changed, 36 insertions, 42 deletions
diff --git a/cmd/datamaps/main.go b/cmd/datamaps/main.go index 32b7ce8..15d635a 100644 --- a/cmd/datamaps/main.go +++ b/cmd/datamaps/main.go @@ -7,51 +7,10 @@ import ( "fmt" "log" "net/http" - "os" - "path/filepath" "github.com/yulqen/datamaps-go/pkg/datamaps" ) -const ( - configDirName = "datamaps-go" - dbName = "datamaps.db" -) - -func setUp() (string, error) { - dir, err := os.UserConfigDir() - if err != nil { - return "", err - } - // check if config folder exists - configPath := filepath.Join(dir, configDirName) - dbPath := filepath.Join(configPath, dbName) - if _, err := os.Stat(configPath); os.IsNotExist(err) { - log.Println("Config directory does not exist.") - log.Printf("Creating config directory %s\n", configPath) - if err := os.Mkdir(filepath.Join(dir, "datamaps-go"), 0700); err != nil { - return "", err - } - } else { - log.Println("Config directory found.") - } - if _, err := os.Stat(dbPath); os.IsNotExist(err) { - log.Println("Database does not exist.") - _, err = os.Create(dbPath) - if err != nil { - return "", err - } - log.Printf("Creating database file at %s\n", dbPath) - _, err := datamaps.SetupDB(dbPath) - if err != nil { - return "", err - } - } else { - log.Println("Database file found.") - } - return dir, nil -} - func main() { opts := datamaps.ParseOptions() @@ -62,7 +21,7 @@ func main() { log.Fatal(err) } case "setup": - _, err := setUp() + _, err := datamaps.SetUp() if err != nil { log.Fatal(err) } diff --git a/pkg/datamaps/config.go b/pkg/datamaps/config.go index 94ce068..344d1fb 100644 --- a/pkg/datamaps/config.go +++ b/pkg/datamaps/config.go @@ -11,6 +11,41 @@ const ( dbName = "datamaps.db" ) +// SetUp creates the config directory and requisite files +func SetUp() (string, error) { + dir, err := os.UserConfigDir() + if err != nil { + return "", err + } + // check if config folder exists + configPath := filepath.Join(dir, configDirName) + dbPath := filepath.Join(configPath, dbName) + if _, err := os.Stat(configPath); os.IsNotExist(err) { + log.Println("Config directory does not exist.") + log.Printf("Creating config directory %s\n", configPath) + if err := os.Mkdir(filepath.Join(dir, "datamaps-go"), 0700); err != nil { + return "", err + } + } else { + log.Println("Config directory found.") + } + if _, err := os.Stat(dbPath); os.IsNotExist(err) { + log.Println("Database does not exist.") + _, err = os.Create(dbPath) + if err != nil { + return "", err + } + log.Printf("Creating database file at %s\n", dbPath) + _, err := SetupDB(dbPath) + if err != nil { + return "", err + } + } else { + log.Println("Database file found.") + } + return dir, nil +} + func getUserConfigDir() (string, error) { dir, err := os.UserConfigDir() if err != nil { |