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 /cmd/datamaps/main.go | |
parent | 1cd5f295939e25e6af54bf69ae9f551f23c57f23 (diff) |
working basics now with no flags package - thanks fzf
Diffstat (limited to 'cmd/datamaps/main.go')
-rw-r--r-- | cmd/datamaps/main.go | 102 |
1 files changed, 26 insertions, 76 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) - // } } |