diff options
Diffstat (limited to '')
-rw-r--r-- | cmd/datamaps/main.go | 38 | ||||
-rw-r--r-- | pkg/datamaps/config.go | 50 | ||||
-rw-r--r-- | pkg/datamaps/config_test.go | 9 | ||||
-rw-r--r-- | pkg/datamaps/testdata/test.db | bin | 135168 -> 135168 bytes |
4 files changed, 53 insertions, 44 deletions
diff --git a/cmd/datamaps/main.go b/cmd/datamaps/main.go index c2a1d71..322bf8d 100644 --- a/cmd/datamaps/main.go +++ b/cmd/datamaps/main.go @@ -4,7 +4,6 @@ datamaps-go is a simple tool to extract from and send data to spreadsheets. package main import ( - "flag" "fmt" "log" "net/http" @@ -54,38 +53,13 @@ func setUp() (string, error) { } func main() { - // setup command - setupCmd := flag.NewFlagSet("setup", flag.ExitOnError) - setupCmd.Usage = func() { fmt.Println("No, you fucking idiot!") } - // datamap command and its flags - datamapCmd := flag.NewFlagSet("datamap", flag.ExitOnError) - importFlg := datamapCmd.String("import", "/home/lemon/Documents/datamaps/input/datamap.csv", "Path to datamap") - nameFlg := datamapCmd.String("name", "Unnamed datamap", "The name you want to give to the imported datamap.") - overwriteFlg := datamapCmd.Bool("overwrite", false, "Start fresh with this datamap (erases existing datamap data).") - initialFlg := datamapCmd.Bool("initial", false, "This option must be used where no datamap table yet exists.") - - // server command and its flags - serverCmd := flag.NewFlagSet("server", flag.ExitOnError) - - if len(os.Args) < 2 { - fmt.Println("Expected 'datamap' or 'setup' subcommand") - os.Exit(1) - } - - opts := datamaps.Options{ - DBPath: "", - DMPath: *importFlg, - DMName: *nameFlg, - DMOverwrite: *overwriteFlg, - DMInitial: *initialFlg, - DMData: nil, - } + opts := datamaps.ParseOptions() switch os.Args[1] { case "server": - serverCmd.Parse(os.Args[2:]) + opts.Command.Parse(os.Args[2:]) http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/" { http.NotFound(w, r) @@ -102,10 +76,10 @@ func main() { case "datamap": datamapCmd.Parse(os.Args[2:]) fmt.Println("subcommand 'datamap'") - fmt.Println(" import:", *importFlg) - fmt.Println(" name:", *nameFlg) - fmt.Println(" overwrite:", *overwriteFlg) - fmt.Println(" initial:", *initialFlg) + fmt.Println(" import:", *dmPath) + fmt.Println(" name:", *dmName) + fmt.Println(" overwrite:", *dmOverwrite) + fmt.Println(" initial:", *dmInitial) dir, err := os.UserConfigDir() if err != nil { diff --git a/pkg/datamaps/config.go b/pkg/datamaps/config.go index 61ef760..c182267 100644 --- a/pkg/datamaps/config.go +++ b/pkg/datamaps/config.go @@ -1,6 +1,9 @@ package datamaps import ( + "flag" + "fmt" + "log" "os" "path/filepath" ) @@ -12,14 +15,26 @@ const ( func getUserConfigDir() (string, error) { dir, err := os.UserConfigDir() - config_path := filepath.Join(dir, config_dir_name) if err != nil { return "", err } + config_path := filepath.Join(dir, config_dir_name) return config_path, nil } +func defaultDMPath() (string, error) { + dir, err := os.UserHomeDir() + if err != nil { + return "", err + } + 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. + type Options struct { + Command string DBPath string DMPath string DMName string @@ -29,9 +44,18 @@ type Options struct { } func defaultOptions() *Options { + dbpath, err := getUserConfigDir() + if err != nil { + log.Fatalf("Unable to get user config directory %v", err) + } + dmpath, err := defaultDMPath() + if err != nil { + log.Fatalf("Unable to get default datamaps directory %v", err) + } return &Options{ - DBPath: "PATH TO DB", - DMPath: "PATH TO DATAMAP", + Command: "help", + DBPath: filepath.Join(dbpath, "datamaps.db"), + DMPath: dmpath, DMName: "Unnamed Datamap", DMOverwrite: false, DMInitial: false, @@ -41,6 +65,26 @@ func defaultOptions() *Options { func ParseOptions() *Options { opts := defaultOptions() + + // setup command + setupCmd := flag.NewFlagSet("setup", flag.ExitOnError) + setupCmd.Usage = func() { fmt.Println("No, you fucking idiot!") } + + // datamap command and its flags + datamapCmd := flag.NewFlagSet("datamap", flag.ExitOnError) + dmPath := datamapCmd.String("import", opts.DMPath, "Path to datamap") + dmName := datamapCmd.String("name", opts.DMName, "The name you want to give to the imported datamap.") + dmOverwrite := datamapCmd.Bool("overwrite", opts.DMOverwrite, "Start fresh with this datamap (erases existing datamap data).") + dmInitial := datamapCmd.Bool("initial", opts.DMInitial, "This option must be used where no datamap table yet exists.") + + // server command and its flags + serverCmd := flag.NewFlagSet("server", flag.ExitOnError) + + if len(os.Args) < 2 { + fmt.Println("Expected 'datamap' or 'setup' subcommand") + os.Exit(1) + } + return opts } diff --git a/pkg/datamaps/config_test.go b/pkg/datamaps/config_test.go index 6fb8cc4..ffeaf5e 100644 --- a/pkg/datamaps/config_test.go +++ b/pkg/datamaps/config_test.go @@ -1,10 +1 @@ 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/datamaps/testdata/test.db b/pkg/datamaps/testdata/test.db Binary files differindex 2706a14..764cbe0 100644 --- a/pkg/datamaps/testdata/test.db +++ b/pkg/datamaps/testdata/test.db |