/
config.go
182 lines (159 loc) · 4.43 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package config
import (
"fmt"
"os"
"strconv"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
const (
p1 = 4
p2 = 3
p3 = 2
p4 = 1
)
type Config struct {
LogLevel string `yaml:"logLevel"`
UpdateInterval int `yaml:"updateInterval"`
Todoist struct {
Token string `yaml:"token"`
NextActionLabel string `yaml:"nextActionLabel"`
AssignProjectLabel bool `yaml:"assignProjectLabel"`
AssignNextActionLabel bool `yaml:"assignNextActionLabel"`
ParentProjectName string `yaml:"parentProjectName"`
ProjectsLabelPrefix string `yaml:"projectsLabelPrefix"`
} `yaml:"todoist"`
Jira []JiraConfig `yaml:"jira"`
loaded bool
}
type JiraConfig struct {
Site string `yaml:"site"`
Username string `yaml:"username"`
Token string `yaml:"token"`
JQL string `yaml:"jql"`
Labels []string `yaml:"labels"`
CompletionStatuses []string `yaml:"completionStatuses"`
Project string `yaml:"project"`
SyncJiraLabels bool `yaml:"syncJiraLabels"`
SyncJiraComponents bool `yaml:"syncJiraComponents"`
PriorityMap map[string][]string `yaml:"priorityMap"`
}
var (
configuration Config
log = logrus.New()
)
func (cfg *Config) IsTest() bool {
return cfg.Todoist.Token == "TEST"
}
func (cfg *Config) validate() {
if cfg.Todoist.Token == "" {
log.Fatal("Todoist API token is not set")
}
if cfg.UpdateInterval <= 0 {
log.Fatal("Update interval must be greater than 0")
}
for _, jiraCfg := range cfg.Jira {
for key := range jiraCfg.PriorityMap {
_, err := cfg.ToAPIPriority(key)
if err != nil {
log.Fatalf("Invalid key found in PriorityMap: %s. Only p1-p4 are allowed.", key)
}
}
}
}
// ToAPIPriority converts a configuration priority to a Todoist API priority
func (cfg *Config) ToAPIPriority(configPriority string) (int, error) {
switch configPriority {
case "p1":
return p1, nil
case "p2":
return p2, nil
case "p3":
return p3, nil
case "p4":
return p4, nil
default:
return 0, fmt.Errorf("unknown priority %s", configPriority)
}
}
func GetConfiguration() Config {
if !configuration.loaded {
loadConfiguration()
}
return configuration
}
func GetLogger() *logrus.Logger {
return log
}
func loadConfiguration() {
configFile := "config.yaml"
if _, err := os.Stat(configFile); err == nil {
configuration, err = readConfig(configFile)
if err != nil {
log.Fatalf("Error reading config: %v", err)
}
}
overrideConfigFromEnv(&configuration)
setDefaults(&configuration)
configuration.validate()
if configuration.LogLevel == "" {
configuration.LogLevel = "error"
}
switch configuration.LogLevel {
case "info":
log.SetLevel(logrus.InfoLevel)
case "debug":
log.SetLevel(logrus.DebugLevel)
default:
log.SetLevel(logrus.ErrorLevel)
}
if configuration.Todoist.NextActionLabel == "" {
configuration.Todoist.NextActionLabel = "Next Action"
}
configuration.loaded = true
}
func readConfig(path string) (Config, error) {
var cfg Config
file, err := os.ReadFile(path)
if err != nil {
return cfg, err
}
err = yaml.Unmarshal(file, &cfg)
return cfg, err
}
func setDefaults(cfg *Config) {
if cfg.UpdateInterval <= 0 {
cfg.UpdateInterval = 5
}
if cfg.Todoist.ProjectsLabelPrefix == "" {
cfg.Todoist.ProjectsLabelPrefix = "Projects"
}
}
func overrideConfigFromEnv(cfg *Config) {
if logLevel := os.Getenv("LOG_LEVEL"); logLevel != "" {
cfg.LogLevel = logLevel
}
if interval := os.Getenv("UPDATE_INTERVAL"); interval != "" {
if val, err := strconv.Atoi(interval); err == nil {
cfg.UpdateInterval = val
}
}
if token := os.Getenv("TODOIST__TOKEN"); token != "" {
cfg.Todoist.Token = token
}
if label := os.Getenv("TODOIST__NEXT_ACTION_LABEL"); label != "" {
cfg.Todoist.NextActionLabel = label
}
if parentProjectName := os.Getenv("TODOIST__PARENT_PROJECT_NAME"); parentProjectName != "" {
cfg.Todoist.ParentProjectName = parentProjectName
}
if prefix := os.Getenv("TODOIST__PROJECTS_LABEL_PREFIX"); prefix != "" {
cfg.Todoist.ProjectsLabelPrefix = prefix
}
if assignNextActionLabel := os.Getenv("TODOIST__ASSIGN_NEXT_ACTION_LABEL"); assignNextActionLabel == "true" {
cfg.Todoist.AssignNextActionLabel = true
}
if assignProjectLabel := os.Getenv("TODOIST__ASSIGN_PROJECT_LABEL"); assignProjectLabel == "true" {
cfg.Todoist.AssignProjectLabel = true
}
}