diff options
author | Matthew Lemon <lemon@matthewlemon.com> | 2020-07-31 16:57:47 +0100 |
---|---|---|
committer | Matthew Lemon <lemon@matthewlemon.com> | 2020-07-31 16:57:47 +0100 |
commit | a12c66e6c61303bfee84c8429e8b5946bfd8f158 (patch) | |
tree | 126eeff48e3acc9140dc2c58aa7a726721cf4397 | |
parent | a39e4a5fde7ea3947b9c93149fda6c2833837e32 (diff) |
starting writing the code for master writer
-rw-r--r-- | pkg/datamaps/config.go | 25 | ||||
-rw-r--r-- | pkg/datamaps/writer.go | 40 | ||||
-rw-r--r-- | pkg/datamaps/writer_test.go | 46 |
3 files changed, 103 insertions, 8 deletions
diff --git a/pkg/datamaps/config.go b/pkg/datamaps/config.go index fe7a45e..7311fba 100644 --- a/pkg/datamaps/config.go +++ b/pkg/datamaps/config.go @@ -143,6 +143,9 @@ type Options struct { // DMInitial is currently not used. DMInitial bool + + // MasterOutPutPath is where the master.xlsx file is to be saved + MasterOutPutPath string } func defaultOptions() *Options { @@ -158,15 +161,21 @@ func defaultOptions() *Options { log.Fatalf("Unable to get default datamaps directory %v", err) } + homeDir, err := os.UserHomeDir() + if err != nil { + log.Fatal("unable to get user home directory") + } + return &Options{ - Command: "help", - DBPath: filepath.Join(dbpath, dbName), - DMPath: dmPath, - DMName: "Unnamed Datamap", - XLSXPath: xlsxPath, - ReturnName: "Unnamed Return", - DMOverwrite: false, - DMInitial: false, + Command: "help", + DBPath: filepath.Join(dbpath, dbName), + DMPath: dmPath, + DMName: "Unnamed Datamap", + XLSXPath: xlsxPath, + ReturnName: "Unnamed Return", + DMOverwrite: false, + DMInitial: false, + MasterOutPutPath: filepath.Join(homeDir, "Desktop"), } } diff --git a/pkg/datamaps/writer.go b/pkg/datamaps/writer.go new file mode 100644 index 0000000..9a7015b --- /dev/null +++ b/pkg/datamaps/writer.go @@ -0,0 +1,40 @@ +package datamaps + +import ( + "log" + "path/filepath" + + "github.com/tealeg/xlsx/v3" +) + +func ExportMaster(opts *Options) error { + // A master represents a set of file data from the database. Actually, in terms of the database, + // it should represent a "return". + // + // The meat of the master is of the format: + // Key 1 | Key_1_Value_for_FD_1 | Key_1_Value_for_FD_2 | Key_1_Value_for_FD_3 | ... etc + // Key 2 | Key_2_Value_for_FD_1 | Key_2_Value_for_FD_2 | Key_2_Value_for_FD_3 | ... etc + // Key 3 | Key_3_Value_for_FD_1 | Key_3_Value_for_FD_2 | Key_3_Value_for_FD_3 | ... etc + // ... + // Could be represented as a slice or a map[string][]string + + // filename := filepath.Join(opts.MasterOutPutPath, "master.xlsx") + wb := xlsx.NewFile() + sh, err := wb.AddSheet("Master Data") + + testRow, err := sh.Row(1) + if err != nil { + log.Fatal(err) + } + if sl := testRow.WriteSlice([]string{"Hello", "Bollocks", "Knackers", "Bottyies"}, -1); sl == -1 { + log.Printf("not a slice type") + } + log.Printf("writing slice to row\n") + + log.Printf("saving master at %s", opts.MasterOutPutPath) + if err := wb.Save(filepath.Join(opts.MasterOutPutPath, "master.xlsx")); err != nil { + log.Fatalf("cannot save file to %s", opts.MasterOutPutPath) + } + sh.Close() + return nil +} diff --git a/pkg/datamaps/writer_test.go b/pkg/datamaps/writer_test.go new file mode 100644 index 0000000..da9dc9d --- /dev/null +++ b/pkg/datamaps/writer_test.go @@ -0,0 +1,46 @@ +package datamaps + +import ( + "os" + "path/filepath" + "testing" +) + +func TestWriteMaster(t *testing.T) { + + // setup - we need the datamap in the test database + db, err := setupDB("./testdata/test.db") + defer func() { + db.Close() + os.Remove("./testdata/test.db") + }() + + if err != nil { + t.Fatal("Expected to be able to set up the database.") + } + + opts := Options{ + DBPath: "./testdata/test.db", + DMName: "First Datamap", + DMPath: "./testdata/datamap.csv", + ReturnName: "Unnamed Return", + MasterOutPutPath: "./testdata/", + XLSXPath: "./testdata/", + } + + defer func() { + os.Remove(filepath.Join(opts.MasterOutPutPath, "master.xlsx")) + }() + + if err := DatamapToDB(&opts); err != nil { + t.Errorf("Unable to write datamap to database file because %v.", err) + } + + if err := ImportToDB(&opts); err != nil { + t.Fatalf("cannot read test XLSX files needed before exporting to master - %v", err) + } + + if err := ExportMaster(&opts); err != nil { + t.Error(err) + } +} |