diff options
author | Matthew Lemon <lemon@matthewlemon.com> | 2020-07-23 17:02:27 +0100 |
---|---|---|
committer | Matthew Lemon <lemon@matthewlemon.com> | 2020-07-23 17:02:27 +0100 |
commit | 4c8294bcbeb45ce93a74ad963a346cf69ccbc398 (patch) | |
tree | a26a6527a7ac15a6d37ba42102a83faec4098bd7 | |
parent | 1cd5f295939e25e6af54bf69ae9f551f23c57f23 (diff) |
working basics now with no flags package - thanks fzf
-rw-r--r-- | cmd/datamaps/main.go | 102 | ||||
-rw-r--r-- | pkg/datamaps/config.go | 15 | ||||
-rw-r--r-- | pkg/datamaps/db.go | 12 | ||||
-rw-r--r-- | pkg/datamaps/db_test.go | 7 | ||||
-rw-r--r-- | pkg/datamaps/testdata/test.db | bin | 135168 -> 0 bytes |
5 files changed, 40 insertions, 96 deletions
diff --git a/cmd/datamaps/main.go b/cmd/datamaps/main.go index f37f537..9e87869 100644 --- a/cmd/datamaps/main.go +++ b/cmd/datamaps/main.go @@ -4,7 +4,9 @@ datamaps-go is a simple tool to extract from and send data to spreadsheets. package main import ( + "fmt" "log" + "net/http" "os" "path/filepath" @@ -12,8 +14,8 @@ import ( ) const ( - config_dir_name = "datamaps-go" - db_name = "datamaps.db" + configDirName = "datamaps-go" + dbName = "datamaps.db" ) func setUp() (string, error) { @@ -22,25 +24,25 @@ func setUp() (string, error) { return "", err } // check if config folder exists - 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) { + 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", config_path) + 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(db_path); os.IsNotExist(err) { + if _, err := os.Stat(dbPath); os.IsNotExist(err) { log.Println("Database does not exist.") - _, err = os.Create(db_path) + _, err = os.Create(dbPath) if err != nil { return "", err } - log.Printf("Creating database file at %s\n", db_path) - _, err := datamaps.SetupDB(db_path) + log.Printf("Creating database file at %s\n", dbPath) + _, err := datamaps.SetupDB(dbPath) if err != nil { return "", err } @@ -56,13 +58,7 @@ func main() { switch opts.Command { case "datamap": - data, err := datamaps.ReadDML(opts.DMPath) - if err != nil { - log.Fatal(err) - } - opts.DMData = data - - err = datamaps.DatamapToDB(opts) + err := datamaps.DatamapToDB(opts) if err != nil { log.Fatal(err) } @@ -71,64 +67,18 @@ func main() { if err != nil { log.Fatal(err) } + case "server": + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/" { + http.NotFound(w, r) + return + } + fmt.Fprintf(w, "Welcome to datamaps!") + // or you could write it thus + // w.Write([]byte("Hello from datamaps")) + }) + log.Println("Starting server on :8080") + err := http.ListenAndServe(":8080", nil) + log.Fatal(err) } - - // switch os.Args[1] { - - // case "server": - // opts.Command.Parse(os.Args[2:]) - // http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - // if r.URL.Path != "/" { - // http.NotFound(w, r) - // return - // } - // fmt.Fprintf(w, "Welcome to datamaps!") - // // or you could write it thus - // // w.Write([]byte("Hello from Snippetbox")) - // }) - // log.Println("Starting server on :8080") - // err := http.ListenAndServe(":8080", nil) - // log.Fatal(err) - - // case "datamap": - // datamapCmd.Parse(os.Args[2:]) - // fmt.Println("subcommand 'datamap'") - // fmt.Println(" import:", *dmPath) - // fmt.Println(" name:", *dmName) - // fmt.Println(" overwrite:", *dmOverwrite) - // fmt.Println(" initial:", *dmInitial) - - // 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) - // } - // // Here we actually read the data from the file - // data, err := datamaps.ReadDML(opts.ImportPath) - // if err != nil { - // log.Fatal(err) - // } - // opts.DMData = data - - // opts.DBPath = filepath.Join(config_path, db_name) - // err = datamaps.DatamapToDB(opts) - // if err != nil { - // log.Fatal(err) - // } - // case "setup": - // setupCmd.Parse(os.Args[2:]) - // _, err := setUp() - // if err != nil { - // log.Fatal(err) - // } - - // default: - // fmt.Println("Do not recognised that command. Expected 'datamap' subcommand.") - // os.Exit(1) - // } } diff --git a/pkg/datamaps/config.go b/pkg/datamaps/config.go index e64f1fd..94ce068 100644 --- a/pkg/datamaps/config.go +++ b/pkg/datamaps/config.go @@ -7,8 +7,8 @@ import ( ) const ( - config_dir_name = "datamaps-go" - db_name = "datamaps.db" + configDirName = "datamaps-go" + dbName = "datamaps.db" ) func getUserConfigDir() (string, error) { @@ -16,8 +16,8 @@ func getUserConfigDir() (string, error) { if err != nil { return "", err } - config_path := filepath.Join(dir, config_dir_name) - return config_path, nil + configPath := filepath.Join(dir, configDirName) + return configPath, nil } func defaultDMPath() (string, error) { @@ -28,9 +28,7 @@ func defaultDMPath() (string, error) { return filepath.Join(dir, "Documents", "datamaps"), nil } -// TODO - need a func to replace Options.Command with the one we pass -// Needs to use flag.NewFlagSet so we can Parse on it in main. - +// Options for the whole CLI application type Options struct { Command string DBPath string @@ -38,7 +36,6 @@ type Options struct { DMName string DMOverwrite bool DMInitial bool - DMData []DatamapLine } func defaultOptions() *Options { @@ -57,7 +54,6 @@ func defaultOptions() *Options { DMName: "Unnamed Datamap", DMOverwrite: false, DMInitial: false, - DMData: make([]DatamapLine, 0), } } @@ -98,6 +94,7 @@ func processOptions(opts *Options, allArgs []string) { } } +//ParseOptions for CLI. func ParseOptions() *Options { opts := defaultOptions() processOptions(opts, os.Args[1:]) diff --git a/pkg/datamaps/db.go b/pkg/datamaps/db.go index 0f6a930..527bbb6 100644 --- a/pkg/datamaps/db.go +++ b/pkg/datamaps/db.go @@ -55,10 +55,12 @@ func SetupDB(path string) (*sql.DB, error) { //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 - // } + + data, err := ReadDML(opts.DMPath) + if err != nil { + log.Fatal(err) + } + d, err := sql.Open("sqlite3", opts.DBPath) if err != nil { return errors.New("Cannot open that damn database file") @@ -85,7 +87,7 @@ func DatamapToDB(opts *Options) error { } defer stmt_dm.Close() defer stmt_dml.Close() - for _, dml := range opts.DMData { + for _, dml := range data { _, err = stmt_dml.Exec(1, dml.Key, dml.Sheet, dml.Cellref) if err != nil { return err diff --git a/pkg/datamaps/db_test.go b/pkg/datamaps/db_test.go index e9d5100..029121c 100644 --- a/pkg/datamaps/db_test.go +++ b/pkg/datamaps/db_test.go @@ -30,16 +30,11 @@ func TestOpenSQLiteFile(t *testing.T) { } func TestDatamapGoesIntoDB(t *testing.T) { - d, err := ReadDML("./testdata/datamap.csv") - if err != nil { - t.Fatal(err) - } opts := Options{ DBPath: "./testdata/test.db", DMName: "First Datamap", - DMData: d, } - err = DatamapToDB(opts) + err := DatamapToDB(&opts) if err != nil { t.Errorf("Unable to write datamap to database file because %v.", err) } diff --git a/pkg/datamaps/testdata/test.db b/pkg/datamaps/testdata/test.db Binary files differdeleted file mode 100644 index 764cbe0..0000000 --- a/pkg/datamaps/testdata/test.db +++ /dev/null |