-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
165 lines (129 loc) · 4.41 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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"time"
"github.com/BurntSushi/toml"
"github.com/pkg/errors"
)
//TODO (cbergoon): Add ability to round to nearest x duration.
const defaultConfigFileLocation = "/.config/pace/"
const defaultConfigFileName = "config.toml"
const defaultConfigFileContent = `# Pace Configuration File
# Jira Instance Information
# Jira URL of Atlassian instance to operate on. Must begin with "https://".
JiraInstanceUrl = "https://mycompany.atlassian.net"
# Jira username of to operate as. User must have ability to read and update issues.
JiraUsername = "john.doe"
# Password of above user account.
JiraPassword = "password"
##############################################################################################################
# Appearance/Behavior
# Prompt prefix display text.
Prompt = ">>> "
##############################################################################################################
# Queries
# Jira username to be used to in JQL queries to retrieve issues. Only time for specified user will be
# included in time log.
QueryUsername = "john.doe"
SuggestionProjects = ["PR1", "PR2"]
##############################################################################################################
# Fill Option Behavior
# FillOptionEnabled allows the log to be filled without entering date/time with each entry. Depending on
# network speeds, Jira instance speeds, number of projects, and project size this may cause performance
# issues and can be disabled with the option below.
FillOptionEnabled = false
# LogFillStartTime option sets the time that should be considered the start of the work day. The fill option
# will use this time when entering creating the first entry of a day. Format: #### (e.g. 0800 for 8:00 AM,
# 1630 for 4:30 PM)
LogFillStartTime = "0800"`
type Config struct {
JiraInstanceUrl string
JiraUsername string
JiraPassword string
Prompt string
QueryUsername string
SuggestionProjects []string
ClockStartTime time.Time
LogFillStartTime string //format 1625 or 0425; HHMM
FillOptionEnabled bool
}
type ProjectShortcut struct {
Name string
Shortcut string
}
func NewConfig() *Config {
return &Config{}
}
func (config *Config) InitializeConfig() error {
//TODO (cbergoon): use file path joiner to remove os specific file path separator; add function to get dir; add function to get file
if _, err := os.Stat(os.Getenv("HOME") + defaultConfigFileLocation + defaultConfigFileName); err != nil {
if os.IsNotExist(err) {
err := os.MkdirAll(os.Getenv("HOME")+defaultConfigFileLocation, os.ModePerm)
if err != nil {
fmt.Println(err)
return errors.New("failed to create pace config directory")
}
file, err := os.Create(os.Getenv("HOME") + defaultConfigFileLocation + defaultConfigFileName)
if err != nil {
return errors.New("failed to create config file")
}
defer file.Close()
_, err = file.WriteString(defaultConfigFileContent)
if err != nil {
return errors.New("failed to write config file")
}
err = file.Sync()
if err != nil {
return errors.New("failed to sync config file")
}
return nil
} else {
return errors.New("failed to check for configuration file")
}
}
cbuf, err := ioutil.ReadFile(os.Getenv("HOME") + defaultConfigFileLocation + defaultConfigFileName)
if err != nil {
return errors.New("failed to read configuration file")
}
if _, err := toml.Decode(string(cbuf), config); err != nil {
return errors.New("failed to parse configuration file")
}
valid, err := validateConfig(config)
if err != nil || !valid {
return errors.New("invalid configuration file")
}
return nil
}
func (config *Config) ApplyDefaults() {
return
}
func validateConfig(config *Config) (bool, error) {
//TODO (cbergoon): Implement validation.
return true, nil
}
func (config *Config) PersistConfig() error {
buf := new(bytes.Buffer)
if err := toml.NewEncoder(buf).Encode(config); err != nil {
return err
}
file, err := os.Create(os.Getenv("HOME") + defaultConfigFileLocation + defaultConfigFileName)
if err != nil {
return errors.New("failed to create config file")
}
defer file.Close()
_, err = file.Write(buf.Bytes())
if err != nil {
return errors.New("failed to write config file")
}
err = file.Sync()
if err != nil {
return errors.New("failed to sync config file")
}
return nil
}
func (config *Config) ConfigUpdated() error {
return config.PersistConfig()
}