diff options
| author | Teddy Wing | 2017-06-04 02:07:50 +0200 | 
|---|---|---|
| committer | Teddy Wing | 2017-06-04 02:07:50 +0200 | 
| commit | 02e4fb5d0d95b8c5c5442ee0a97b960f1296c236 (patch) | |
| tree | 4cf323694692322036b80d2d921ff966096c6c36 /config.go | |
| parent | 055301ca09d57b759b290d897bbb7560460251ca (diff) | |
| parent | 9b6a6543e351308939bd420243507368b0669e63 (diff) | |
| download | timetasker-02e4fb5d0d95b8c5c5442ee0a97b960f1296c236.tar.bz2 | |
Merge branch 'timetasker-daily'
Diffstat (limited to 'config.go')
| -rw-r--r-- | config.go | 76 | 
1 files changed, 76 insertions, 0 deletions
| diff --git a/config.go b/config.go new file mode 100644 index 0000000..126b123 --- /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(configFile(), &config) +	if err != nil { +		return err +	} + +	return nil +} | 
