/
config.go
153 lines (133 loc) · 3 KB
/
config.go
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package config
import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/BurntSushi/toml"
)
const (
PomoDir = "~/.pomo"
PomoConfig = "config.toml"
Template = "config.template.toml"
)
var DefaultPath = fmt.Sprintf("%s/%s", PomoDir, PomoConfig)
type Config struct {
Database Database `toml:"database"`
Timers TimerConfig `toml:"timers"`
}
type Database struct {
Task string `toml:"task"`
Session string `toml:"session"`
}
func ExpandPath(path string) (string, error) {
if strings.HasPrefix(path, "~/") {
dirname, err := os.UserHomeDir()
if err != nil {
return "", err
}
return filepath.Join(dirname, path[2:]), nil
}
return path, nil
}
type TimerConfig struct {
Focus string `toml:"focus"`
Short string `toml:"short"`
Long string `toml:"long"`
Interval int `toml:"interval"`
}
func (tc *TimerConfig) FocusDuration() time.Duration {
d, err := time.ParseDuration(tc.Focus)
if err != nil {
panic(err)
}
return d
}
func (tc *TimerConfig) ShortBreakDuration() time.Duration {
d, err := time.ParseDuration(tc.Short)
if err != nil {
panic(err)
}
return d
}
func (tc *TimerConfig) LongBreakDuration() time.Duration {
d, err := time.ParseDuration(tc.Long)
if err != nil {
panic(err)
}
return d
}
type duration struct {
time.Duration
}
func (d *duration) UnmarshalText(text []byte) (err error) {
d.Duration, err = time.ParseDuration(string(text))
return err
}
func (d *duration) MarshalText() ([]byte, error) {
text := d.Duration.String()
return []byte(text), nil
}
func Parse(path string) (*Config, error) {
if _, err := os.Stat(path); err != nil {
path = "config.toml"
}
var config Config
_, err := toml.DecodeFile(path, &config)
if err != nil {
return nil, err
}
return &config, nil
}
func Update(key, value string, config *Config) error {
if strings.HasPrefix(key, "database.") {
switch {
case strings.HasPrefix(key, "task"):
config.Database.Task = value
case strings.HasPrefix(key, "session"):
config.Database.Session = value
default:
return fmt.Errorf("unknown database %s", key)
}
} else if strings.HasPrefix(key, "timers") {
switch {
case strings.HasSuffix(key, "focus"):
d, err := time.ParseDuration(value)
if err != nil {
return err
}
fmt.Println(d, value)
config.Timers.Focus = d.String()
case strings.HasSuffix(key, "short"):
d, err := time.ParseDuration(value)
if err != nil {
return err
}
config.Timers.Short = d.String()
case strings.HasSuffix(key, "long"):
d, err := time.ParseDuration(value)
if err != nil {
return err
}
config.Timers.Long = d.String()
case strings.HasSuffix(key, "intervals"):
intervals, err := strconv.Atoi(value)
if err != nil {
return err
}
config.Timers.Interval = intervals
default:
return fmt.Errorf("unknown key %s", key)
}
} else {
return fmt.Errorf("unknown key %s", key)
}
f, err := os.Create(DefaultPath)
if err != nil {
return err
}
defer f.Close()
return toml.NewEncoder(f).Encode(config)
}