diff options
author | Matthew Lemon <lemon@matthewlemon.com> | 2020-07-23 11:39:11 +0100 |
---|---|---|
committer | Matthew Lemon <lemon@matthewlemon.com> | 2020-07-23 11:39:11 +0100 |
commit | e910620988760b054bd669f6f6847ccf96ab717a (patch) | |
tree | 12c43d7132338ab7a2644358300f99c39eeb871a /pkg/datamaps | |
parent | d7d4d0a0476f98d1b0b4c5c454b6d1048a82bdad (diff) |
BROKEN - heavy work on config and removing flags dep
Diffstat (limited to 'pkg/datamaps')
-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 |
3 files changed, 47 insertions, 12 deletions
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 |