diff options
author | Matthew Lemon <lemon@matthewlemon.com> | 2020-07-22 21:01:54 +0100 |
---|---|---|
committer | Matthew Lemon <lemon@matthewlemon.com> | 2020-07-22 21:01:54 +0100 |
commit | d7d4d0a0476f98d1b0b4c5c454b6d1048a82bdad (patch) | |
tree | 11aaba3bab132e3f7d13f73c61d8b14739794f50 | |
parent | c3d4c6bb51a3d7b89e0fcbb69230db423577ae17 (diff) |
more major surgery
-rw-r--r-- | cmd/datamaps/main.go (renamed from cmd/dm/main.go) | 22 | ||||
-rwxr-xr-x | datamaps | bin | 0 -> 12830216 bytes | |||
-rw-r--r-- | go.mod | 2 | ||||
-rw-r--r-- | pkg/datamaps/config.go | 46 | ||||
-rw-r--r-- | pkg/datamaps/config_test.go | 10 | ||||
-rw-r--r-- | pkg/datamaps/db.go (renamed from pkg/reader/db.go) | 16 | ||||
-rw-r--r-- | pkg/datamaps/db_test.go (renamed from pkg/reader/db_test.go) | 6 | ||||
-rw-r--r-- | pkg/datamaps/reader.go (renamed from pkg/reader/reader.go) | 2 | ||||
-rw-r--r-- | pkg/datamaps/reader_test.go (renamed from pkg/reader/reader_test.go) | 2 | ||||
-rw-r--r-- | pkg/datamaps/testdata/datamap.csv (renamed from pkg/reader/testdata/datamap.csv) | 0 | ||||
-rw-r--r-- | pkg/datamaps/testdata/test.db (renamed from pkg/reader/testdata/test.db) | bin | 135168 -> 135168 bytes | |||
-rw-r--r-- | pkg/datamaps/testdata/test_template.xlsx (renamed from pkg/reader/testdata/test_template.xlsx) | bin | 9193 -> 9193 bytes |
12 files changed, 79 insertions, 27 deletions
diff --git a/cmd/dm/main.go b/cmd/datamaps/main.go index de79441..c2a1d71 100644 --- a/cmd/dm/main.go +++ b/cmd/datamaps/main.go @@ -11,7 +11,7 @@ import ( "os" "path/filepath" - "github.com/yulqen/datamaps-go/pkg/reader" + "github.com/yulqen/datamaps-go/pkg/datamaps" ) const ( @@ -43,7 +43,7 @@ func setUp() (string, error) { return "", err } log.Printf("Creating database file at %s\n", db_path) - _, err := reader.SetupDB(db_path) + _, err := datamaps.SetupDB(db_path) if err != nil { return "", err } @@ -73,13 +73,13 @@ func main() { os.Exit(1) } - opts := reader.Opts{ - DBPath: "", - ImportPath: *importFlg, - Name: *nameFlg, - Overwrite: *overwriteFlg, - Initial: *initialFlg, - DMData: nil, + opts := datamaps.Options{ + DBPath: "", + DMPath: *importFlg, + DMName: *nameFlg, + DMOverwrite: *overwriteFlg, + DMInitial: *initialFlg, + DMData: nil, } switch os.Args[1] { @@ -118,14 +118,14 @@ func main() { os.Exit(1) } // Here we actually read the data from the file - data, err := reader.ReadDML(opts.ImportPath) + data, err := datamaps.ReadDML(opts.ImportPath) if err != nil { log.Fatal(err) } opts.DMData = data opts.DBPath = filepath.Join(config_path, db_name) - err = reader.DatamapToDB(opts) + err = datamaps.DatamapToDB(opts) if err != nil { log.Fatal(err) } diff --git a/datamaps b/datamaps Binary files differnew file mode 100755 index 0000000..50e12b7 --- /dev/null +++ b/datamaps @@ -3,8 +3,10 @@ module github.com/yulqen/datamaps-go go 1.14 require ( + github.com/cweill/gotests v1.5.3 // indirect github.com/mattn/go-sqlite3 v1.14.0 github.com/tealeg/xlsx v1.0.5 github.com/urfave/cli/v2 v2.2.0 github.com/yulqen/coords v0.1.0 + golang.org/x/tools v0.0.0-20200722181740-bd1e9de8d890 // indirect ) diff --git a/pkg/datamaps/config.go b/pkg/datamaps/config.go new file mode 100644 index 0000000..61ef760 --- /dev/null +++ b/pkg/datamaps/config.go @@ -0,0 +1,46 @@ +package datamaps + +import ( + "os" + "path/filepath" +) + +const ( + config_dir_name = "datamaps-go" + db_name = "datamaps.db" +) + +func getUserConfigDir() (string, error) { + dir, err := os.UserConfigDir() + config_path := filepath.Join(dir, config_dir_name) + if err != nil { + return "", err + } + return config_path, nil +} + +type Options struct { + DBPath string + DMPath string + DMName string + DMOverwrite bool + DMInitial bool + DMData []DatamapLine +} + +func defaultOptions() *Options { + return &Options{ + DBPath: "PATH TO DB", + DMPath: "PATH TO DATAMAP", + DMName: "Unnamed Datamap", + DMOverwrite: false, + DMInitial: false, + DMData: make([]DatamapLine, 0), + } +} + +func ParseOptions() *Options { + opts := defaultOptions() + return opts + +} diff --git a/pkg/datamaps/config_test.go b/pkg/datamaps/config_test.go new file mode 100644 index 0000000..6fb8cc4 --- /dev/null +++ b/pkg/datamaps/config_test.go @@ -0,0 +1,10 @@ +package datamaps + +import "testing" + +func Test_getUserConfigDir(t *testing.T) { + dir, _ := getUserConfigDir() + if dir != "/home/lemon/.config/datamaps-go" { + t.Errorf("Did not find the correct directory - found %s instead\n", dir) + } +} diff --git a/pkg/reader/db.go b/pkg/datamaps/db.go index 91b6b85..46d07bc 100644 --- a/pkg/reader/db.go +++ b/pkg/datamaps/db.go @@ -1,4 +1,4 @@ -package reader +package datamaps import ( "database/sql" @@ -11,12 +11,6 @@ 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); @@ -58,9 +52,9 @@ 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 { -func DatamapToDB(opts Opts) error { - fmt.Printf("Importing datamap file %s and naming it %s.\n", opts.ImportPath, opts.Name) +//func DatafmapToDB(d_path string, data []DatamapLine, dm_name string, dm_path string) error { +func DatamapToDB(opts Options) error { + fmt.Printf("Importing datamap file %s and naming it %s.\n", opts.DMPath, opts.DMName) // db, err := SetupDB("/home/lemon/.config/datamaps-go/datamaps.db") // if err != nil { // return err @@ -83,7 +77,7 @@ func DatamapToDB(opts Opts) error { if err != nil { return err } - _, err = stmt_dm.Exec(opts.Name, time.Now()) + _, err = stmt_dm.Exec(opts.DMName, time.Now()) stmt_dml, err := tx.Prepare("INSERT INTO datamap_line (dm_id, key, sheet, cellref) VALUES(?,?,?,?);") if err != nil { diff --git a/pkg/reader/db_test.go b/pkg/datamaps/db_test.go index 5a96485..e9d5100 100644 --- a/pkg/reader/db_test.go +++ b/pkg/datamaps/db_test.go @@ -1,4 +1,4 @@ -package reader +package datamaps import ( "testing" @@ -34,9 +34,9 @@ func TestDatamapGoesIntoDB(t *testing.T) { if err != nil { t.Fatal(err) } - opts := Opts{ + opts := Options{ DBPath: "./testdata/test.db", - Name: "First Datamap", + DMName: "First Datamap", DMData: d, } err = DatamapToDB(opts) diff --git a/pkg/reader/reader.go b/pkg/datamaps/reader.go index 7479f9f..b67ad3c 100644 --- a/pkg/reader/reader.go +++ b/pkg/datamaps/reader.go @@ -1,7 +1,7 @@ /* reader reads datamap files and populated spreadsheets */ -package reader +package datamaps import ( "encoding/csv" diff --git a/pkg/reader/reader_test.go b/pkg/datamaps/reader_test.go index ca886bc..e89ca79 100644 --- a/pkg/reader/reader_test.go +++ b/pkg/datamaps/reader_test.go @@ -1,4 +1,4 @@ -package reader +package datamaps import ( "testing" diff --git a/pkg/reader/testdata/datamap.csv b/pkg/datamaps/testdata/datamap.csv index d13bd27..d13bd27 100644 --- a/pkg/reader/testdata/datamap.csv +++ b/pkg/datamaps/testdata/datamap.csv diff --git a/pkg/reader/testdata/test.db b/pkg/datamaps/testdata/test.db Binary files differindex d497380..2706a14 100644 --- a/pkg/reader/testdata/test.db +++ b/pkg/datamaps/testdata/test.db diff --git a/pkg/reader/testdata/test_template.xlsx b/pkg/datamaps/testdata/test_template.xlsx Binary files differindex 77ac56f..77ac56f 100644 --- a/pkg/reader/testdata/test_template.xlsx +++ b/pkg/datamaps/testdata/test_template.xlsx |