diff options
author | Matthew Lemon <lemon@matthewlemon.com> | 2020-07-22 17:02:41 +0100 |
---|---|---|
committer | Matthew Lemon <lemon@matthewlemon.com> | 2020-07-22 17:02:41 +0100 |
commit | c3d4c6bb51a3d7b89e0fcbb69230db423577ae17 (patch) | |
tree | 4ee56ca481c865634cf810db3fa0ec0eb78187d0 | |
parent | d2d94cfb6e7c676ad9f709641b86417c17093657 (diff) |
reconfigured opts
-rw-r--r-- | cmd/dm/main.go | 16 | ||||
-rw-r--r-- | pkg/reader/db.go | 17 | ||||
-rw-r--r-- | pkg/reader/db_test.go | 7 | ||||
-rw-r--r-- | pkg/reader/testdata/test.db | bin | 135168 -> 135168 bytes |
4 files changed, 31 insertions, 9 deletions
diff --git a/cmd/dm/main.go b/cmd/dm/main.go index 9a93fa4..de79441 100644 --- a/cmd/dm/main.go +++ b/cmd/dm/main.go @@ -73,6 +73,15 @@ func main() { os.Exit(1) } + opts := reader.Opts{ + DBPath: "", + ImportPath: *importFlg, + Name: *nameFlg, + Overwrite: *overwriteFlg, + Initial: *initialFlg, + DMData: nil, + } + switch os.Args[1] { case "server": @@ -109,13 +118,14 @@ func main() { os.Exit(1) } // Here we actually read the data from the file - data, err := reader.ReadDML(*importFlg) + data, err := reader.ReadDML(opts.ImportPath) if err != nil { log.Fatal(err) } + opts.DMData = data - db_path := filepath.Join(config_path, db_name) - err = reader.DatamapToDB(db_path, data, *nameFlg, *importFlg) + opts.DBPath = filepath.Join(config_path, db_name) + err = reader.DatamapToDB(opts) if err != nil { log.Fatal(err) } diff --git a/pkg/reader/db.go b/pkg/reader/db.go index 6f62de1..91b6b85 100644 --- a/pkg/reader/db.go +++ b/pkg/reader/db.go @@ -11,6 +11,12 @@ import ( _ "github.com/mattn/go-sqlite3" ) +type Opts struct { + DBPath, ImportPath, Name string + Overwrite, Initial bool + DMData []DatamapLine +} + func SetupDB(path string) (*sql.DB, error) { stmt_base := `DROP TABLE IF EXISTS datamap; CREATE TABLE datamap(id INTEGER PRIMARY KEY, name TEXT, date_created TEXT); @@ -52,13 +58,14 @@ func SetupDB(path string) (*sql.DB, error) { // TODO - how do we avoid passing in all these params!??! //DatamapToDB takes a slice of DatamapLine and writes it to a sqlite3 db file. -func DatamapToDB(d_path string, data []DatamapLine, dm_name string, dm_path string) error { - fmt.Printf("Importing datamap file %s and naming it %s.\n", dm_path, dm_name) +//func DatamapToDB(d_path string, data []DatamapLine, dm_name string, dm_path string) error { +func DatamapToDB(opts Opts) error { + fmt.Printf("Importing datamap file %s and naming it %s.\n", opts.ImportPath, opts.Name) // db, err := SetupDB("/home/lemon/.config/datamaps-go/datamaps.db") // if err != nil { // return err // } - d, err := sql.Open("sqlite3", d_path) + d, err := sql.Open("sqlite3", opts.DBPath) if err != nil { return errors.New("Cannot open that damn database file") } @@ -76,7 +83,7 @@ func DatamapToDB(d_path string, data []DatamapLine, dm_name string, dm_path stri if err != nil { return err } - _, err = stmt_dm.Exec(dm_name, time.Now()) + _, err = stmt_dm.Exec(opts.Name, time.Now()) stmt_dml, err := tx.Prepare("INSERT INTO datamap_line (dm_id, key, sheet, cellref) VALUES(?,?,?,?);") if err != nil { @@ -84,7 +91,7 @@ func DatamapToDB(d_path string, data []DatamapLine, dm_name string, dm_path stri } defer stmt_dm.Close() defer stmt_dml.Close() - for _, dml := range data { + for _, dml := range opts.DMData { _, err = stmt_dml.Exec(1, dml.Key, dml.Sheet, dml.Cellref) if err != nil { return err diff --git a/pkg/reader/db_test.go b/pkg/reader/db_test.go index 5c33343..5a96485 100644 --- a/pkg/reader/db_test.go +++ b/pkg/reader/db_test.go @@ -34,7 +34,12 @@ func TestDatamapGoesIntoDB(t *testing.T) { if err != nil { t.Fatal(err) } - err = DatamapToDB("./testdata/test.db", d, "First Datamap", "./testdata/test.db") + opts := Opts{ + DBPath: "./testdata/test.db", + Name: "First Datamap", + DMData: d, + } + err = DatamapToDB(opts) if err != nil { t.Errorf("Unable to write datamap to database file because %v.", err) } diff --git a/pkg/reader/testdata/test.db b/pkg/reader/testdata/test.db Binary files differindex bbdc08f..d497380 100644 --- a/pkg/reader/testdata/test.db +++ b/pkg/reader/testdata/test.db |