-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
181 lines (161 loc) · 4.38 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
package kong
import (
"bufio"
"errors"
"fmt"
"os"
"path"
"strconv"
"strings"
"gopkg.in/yaml.v2"
)
var errConfigComponentEmpty = errors.New("component cannot be empty")
// Config provides the configuration for the Jira client. The configuration is
// used to authenticate the Jira client and customize Jira queries.
type Config struct {
Endpoint string `yaml:"endpoint"`
Username string `yaml:"username"`
Password string `yaml:"password"`
Project string `yaml:"project"`
IssueType string `yaml:"issueType"`
Labels []string `yaml:"labels"`
Components []string `yaml:"components"`
CustomFields CustomFields `yaml:"customFields"`
SprintKeyword string `yaml:"sprintKeyword"`
SprintDuration int `yaml:"sprintDuration"`
CopyCommand string `yaml:"copyCommand"`
SprintStandupTemplate string `yaml:"sprintStandupTemplate"`
EpicStandupTemplate string `yaml:"epicStandupTemplate"`
}
// CustomFields provides configuration of custom fields to map fields like
// epics, sprints and story points to the Jira backend.
type CustomFields struct {
// custom fields for issue creation
Epics string `yaml:"epics"`
Sprints string `yaml:"sprints"`
StoryPoints string `yaml:"storyPoints"`
// custom fields for epic creation
EpicName string `yaml:"epicName"`
ParentLink string `yaml:"parentLink"`
}
// Validate ensures the configuration has a valid values.
func (c Config) Validate() error {
for _, component := range c.Components {
if component == "" {
return fmt.Errorf("Config.Validate: %w", errConfigComponentEmpty)
}
}
return nil
}
// Write ensures the configuration directory exists and writes the content of
// Config into a file for subsequent retrieval.
func (c Config) Write() (err error) {
err = os.MkdirAll(c.dir(), os.ModePerm)
if err != nil {
return err
}
f, err := os.Create(c.filepath())
if err != nil {
return err
}
defer func() {
err = f.Close()
}()
encoder := yaml.NewEncoder(f)
err = encoder.Encode(c)
if err != nil {
return err
}
return encoder.Close()
}
// LoadConfig reads the current configuration from disk or returns an empty one
// if the file does not exist yet.
func LoadConfig() (Config, error) {
var config Config
if config.isMissing() {
return config, ErrConfigMissing
}
b, err := os.ReadFile(config.filepath())
if err != nil {
return config, err
}
if err := yaml.Unmarshal(b, &config); err != nil {
return config, err
}
if err := config.Validate(); err != nil {
return config, fmt.Errorf("LoadConfig: %w", config.Validate())
}
return config, nil
}
func (c Config) dir() string {
return path.Join(os.Getenv("HOME"), ".config")
}
func (c Config) filepath() string {
return path.Join(c.dir(), "kong")
}
func (c Config) isMissing() bool {
_, err := os.Stat(c.filepath())
return os.IsNotExist(err)
}
// Configurer parses the user input to configure Kong for Jira.
type Configurer struct {
r *bufio.Reader
}
// NewConfigReader returns a new instance of Configurer used to parse user
// input for generating a Kong Jira configuration.
func NewConfigReader() Configurer {
return Configurer{
r: bufio.NewReader(os.Stdin),
}
}
// ReadString prompts the user to enter a value prefixed with a given label and
// writes the user input to target.
func (c Configurer) ReadString(label string, target *string) error {
var current string
if *target != "" {
current = " [" + *target + "]"
}
fmt.Print(label + current + ": ")
s, err := c.r.ReadString('\n')
if err != nil {
return err
}
s = strings.TrimSuffix(s, "\n")
// only write to target if user did not skip
if s != "" {
*target = s
}
return nil
}
// ReadInt prompts the user to enter a value prefixed with a given label and
// writes the user input to target.
func (c Configurer) ReadInt(label string, target *int) error {
var s string
if *target != 0 {
s = fmt.Sprint(*target)
}
err := c.ReadString(label, &s)
if err != nil {
return err
}
i, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return err
}
*target = int(i)
return nil
}
// ReadStringSlice prompts the user to enter a comma-separated value prefixed
// with a given label and writes the user input to target.
func (c Configurer) ReadStringSlice(label string, target *[]string) error {
var s string
if target != nil {
s = strings.Join(*target, ",")
}
err := c.ReadString(label, &s)
if err != nil {
return err
}
*target = strings.Split(s, ",")
return nil
}