aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthew Lemon <lemon@matthewlemon.com>2020-07-20 21:14:53 +0100
committerMatthew Lemon <lemon@matthewlemon.com>2020-07-20 21:14:53 +0100
commit9f5398b1aa338404403c8de4f48d343a90fe2428 (patch)
tree2d9470edb6faebfed2421a67d4a2708692e4dd01
parent341f0bd5dfd640a0adf7cceb61fabeab7e2cebb1 (diff)
better handling of setup but by no means perfect
Diffstat (limited to '')
-rw-r--r--cmd/main.go21
-rw-r--r--pkg/db/setup.go9
-rw-r--r--pkg/db/testdata/test.dbbin135168 -> 12288 bytes
3 files changed, 25 insertions, 5 deletions
diff --git a/cmd/main.go b/cmd/main.go
index 508d9a1..1f6e4ab 100644
--- a/cmd/main.go
+++ b/cmd/main.go
@@ -14,14 +14,19 @@ import (
"github.com/yulqen/datamaps-go/pkg/reader"
)
+const (
+ config_dir_name = "datamaps-go"
+ db_name = "datamaps.db"
+)
+
func setUp() (string, error) {
dir, err := os.UserConfigDir()
if err != nil {
return "", err
}
// check if config folder exists
- config_path := filepath.Join(dir, "datamaps-go")
- db_path := filepath.Join(config_path, "datamaps.db")
+ config_path := filepath.Join(dir, config_dir_name)
+ db_path := filepath.Join(config_path, db_name)
if _, err := os.Stat(config_path); os.IsNotExist(err) {
log.Println("Config directory does not exist.")
log.Printf("Creating config directory %s\n", config_path)
@@ -81,10 +86,22 @@ func main() {
fmt.Println(" overwrite:", *overwriteFlg)
fmt.Println(" initial:", *initialFlg)
+ dir, err := os.UserConfigDir()
+ if err != nil {
+ os.Exit(1)
+ }
+ // check if config folder exists
+ config_path := filepath.Join(dir, config_dir_name)
+
+ if _, err := os.Stat(config_path); os.IsNotExist(err) {
+ fmt.Println("Config directory and database does not exist. Run datamaps setup to fix.")
+ os.Exit(1)
+ }
data, err := reader.ReadDML(*importFlg)
if err != nil {
log.Fatal(err)
}
+
err = db.DatamapToDB(data, *nameFlg, *importFlg)
if err != nil {
log.Fatal(err)
diff --git a/pkg/db/setup.go b/pkg/db/setup.go
index d0c0064..e5179ed 100644
--- a/pkg/db/setup.go
+++ b/pkg/db/setup.go
@@ -3,6 +3,7 @@ package db
import (
"database/sql"
"errors"
+ "fmt"
"log"
"os"
"time"
@@ -37,12 +38,14 @@ func SetupDB(path string) (*sql.DB, error) {
pragma := "PRAGMA foreign_keys = ON;"
_, err = db.Exec(pragma)
if err != nil {
- log.Printf("%q: %s\n", err, pragma)
+ // log.Printf("%q: %s\n", err, pragma)
+ return nil, err
}
_, err = db.Exec(stmt_base)
if err != nil {
- log.Printf("%q: %s\n", err, stmt_base)
+ // log.Printf("%q: %s\n", err, stmt_base)
+ return nil, err
}
return db, nil
@@ -50,7 +53,7 @@ func SetupDB(path string) (*sql.DB, error) {
//DatamapToDB takes a slice of DatamapLine and writes it to a sqlite3 db file.
func DatamapToDB(data []reader.DatamapLine, dm_name string, dm_path string) error {
- log.Printf("Importing Datamap")
+ fmt.Printf("Importing datamap file %s and naming it %s.\n", dm_path, dm_name)
db, err := SetupDB("/home/lemon/.config/datamaps-go/datamaps.db")
if err != nil {
return err
diff --git a/pkg/db/testdata/test.db b/pkg/db/testdata/test.db
index a85f6ec..bb59dde 100644
--- a/pkg/db/testdata/test.db
+++ b/pkg/db/testdata/test.db
Binary files differ