aboutsummaryrefslogtreecommitdiffstats
path: root/config.go
blob: 126b123a978f0c48aae8d691ef299bb7b46dde6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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(configFile(), &config)
	if err != nil {
		return err
	}

	return nil
}