diff options
author | Teddy Wing | 2017-06-03 21:02:13 +0200 |
---|---|---|
committer | Teddy Wing | 2017-06-03 21:02:13 +0200 |
commit | 3a78f9dd28a89acc75b781222ede663b3f46fa15 (patch) | |
tree | 134032a50a34250587f0a8a21d6e40806c935240 | |
parent | b0858ffc7b79f3baf6d1f15962ab90a35275707e (diff) | |
parent | bcb86d1e025c6928018f94c5634eed88e50c0a58 (diff) | |
download | timetasker-3a78f9dd28a89acc75b781222ede663b3f46fa15.tar.bz2 |
Merge branch 'config-initialiser' into timetasker-daily
-rw-r--r-- | config.go | 76 | ||||
-rw-r--r-- | main.go | 46 |
2 files changed, 102 insertions, 20 deletions
diff --git a/config.go b/config.go new file mode 100644 index 0000000..43f7689 --- /dev/null +++ b/config.go @@ -0,0 +1,76 @@ +package main + +import ( + "io/ioutil" + "os" + "path/filepath" + + "github.com/teddywing/timetasker/timetask" + + "github.com/BurntSushi/toml" + "github.com/goulash/xdg" +) + +type Config struct { + Auth struct { + Username string + PasswordCmd string `toml:"password_cmd"` + } + Profile timetask.Profile + Projects map[string]timetask.Project +} + +const emptyConfig = `[auth] +username = "" +password_cmd = "" + + +[profile] +person_id = # ADD PERSON ID + + +[projects.example] +client = # ADD CLIENT ID +project = # ADD PROJECT ID +module = # ADD MODULE ID +task = 0 +work_type = # ADD WORK TYPE ID +billable = true +` + +func configDir() string { + return filepath.Join(xdg.ConfigHome, "timetasker") +} + +func configFile() string { + return filepath.Join(configDir(), "config.toml") +} + +func maybeWriteConfig() error { + path := xdg.FindConfig("timetasker/config.toml") + + if path == "" { + path = configDir() + if _, err := os.Stat(path); os.IsNotExist(err) { + os.Mkdir(path, 0700) + } + + config_path := configFile() + err := ioutil.WriteFile(config_path, []byte(emptyConfig), 0644) + if err != nil { + return err + } + } + + return nil +} + +func loadConfig() error { + config = Config{} + _, err := toml.DecodeFile("config2.toml", &config) + if err != nil { + return err + } + + return nil +} @@ -9,27 +9,22 @@ import ( "github.com/teddywing/timetasker/timetask" - "github.com/BurntSushi/toml" "gopkg.in/alecthomas/kingpin.v2" ) var VERSION string = "0.1.0" -type Config struct { - Auth struct { - Username string - PasswordCmd string `toml:"password_cmd"` - } - Profile timetask.Profile - Projects map[string]timetask.Project -} - var config Config func main() { var err error - loadConfig() + err = loadConfig() + if err != nil { + fmt.Println("Could not load config file") + fmt.Println(err) + os.Exit(1) + } // Parse command line arguments project_alias := kingpin.Flag( @@ -37,7 +32,6 @@ func main() { "Project alias defined in config.toml.", ). Short('p'). - Required(). String() time_spent := kingpin.Flag("time", "Time spent working on project."). Short('t'). @@ -49,9 +43,29 @@ func main() { description := kingpin.Flag("description", "Description of work."). Short('m'). String() + write_config_description := fmt.Sprintf( + "Initialise a new config file template at %s", + configFile(), + ) + write_config := kingpin.Flag("write-config", write_config_description). + Bool() kingpin.Version(VERSION) kingpin.Parse() + if *project_alias == "" && !*write_config { + kingpin.Fatalf("required flag --project not provided, try --help") + } + + if *write_config { + err = maybeWriteConfig() + if err != nil { + fmt.Println("Could not write config file") + fmt.Println(err) + os.Exit(1) + } + + os.Exit(0) + } // Submit time entry project, ok := config.Projects[*project_alias] if !ok { @@ -103,11 +117,3 @@ func main() { body, err = ioutil.ReadAll(resp.Body) log.Println(string(body)) } - -func loadConfig() { - config = Config{} - _, err := toml.DecodeFile("config2.toml", &config) - if err != nil { - log.Println(err) - } -} |