aboutsummaryrefslogtreecommitdiffstats
path: root/pkg
diff options
context:
space:
mode:
authorMatthew Lemon <lemon@matthewlemon.com>2020-07-23 21:23:29 +0100
committerMatthew Lemon <lemon@matthewlemon.com>2020-07-23 21:23:29 +0100
commite68c3d4532816b54a7dce271ecbe345a685bd651 (patch)
tree6f7b67bb783064a80697dce62ccd6a2348504a5c /pkg
parent3b82b7463987ff637530cb403b6d294978bbd115 (diff)
main is getting smaller
Diffstat (limited to 'pkg')
-rw-r--r--pkg/datamaps/config.go35
1 files changed, 35 insertions, 0 deletions
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 {