aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--config.go76
-rw-r--r--main.go46
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
+}
diff --git a/main.go b/main.go
index eaef1fc..38cd527 100644
--- a/main.go
+++ b/main.go
@@ -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)
- }
-}