-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
266 lines (205 loc) · 5.98 KB
/
utils.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package main
import (
"bytes"
"fmt"
"log"
"os"
"sort"
"strings"
ui "github.com/awesome-gocui/gocui"
color "github.com/gookit/color"
config "github.com/gookit/config/v2"
yaml "github.com/gookit/config/v2/yaml"
keyring "github.com/zalando/go-keyring"
)
func getPaths() string {
configHome := os.Getenv("XDG_CONFIG_HOME")
configDir := fmt.Sprintf("%s/%s", configHome, ProjectName)
configPath := fmt.Sprintf("%s/%s", configDir, "config.yaml")
return configPath
}
func initConfigSetup() {
red := color.FgRed.Render
configPath := getPaths()
config.WithOptions(config.ParseEnv)
config.AddDriver(yaml.Driver)
if err := config.LoadFiles(configPath); err != nil {
log.Fatalf("Missing config file, create one at %s", red(ConfigPathMsg))
}
}
func GetJiraCredentials() (string, string, string, error) {
server := config.String(ServerKey)
username := config.String(UsernameKey)
secret, err := keyring.Get(ProjectName, username)
if err != nil {
log.Panicln(err)
}
return server, username, secret, err
}
func GetSavedProjects() []string {
projectCodeMap := config.StringMap(ProjectsKey)
var projects []string
for key := range projectCodeMap {
projects = append(projects, strings.ToUpper(key))
}
return projects
}
func getStatusesPath(code string) string {
return fmt.Sprintf("%s.%s.statuses", ProjectsKey, strings.ToLower(code))
}
func getStatusesMap(code string) map[string]string {
statusesPath := getStatusesPath(code)
return config.StringMap(statusesPath)
}
// Currently we have problem:
// Despite we Set("projects.test") value: { statuses: { done: true, to do: false } }
// and despite we can see exactly correct value when Get("projects.test")
// Config.Get("projects.test.statuses.done") will return nil
func GetSavedStatusesByProjectCode(code string) []string {
statusesPath := getStatusesPath(code)
statusesMap := getStatusesMap(code)
keys := make([]string, 0, len(statusesMap))
for k := range statusesMap {
keys = append(keys, k)
}
sort.Strings(keys)
statuses := make([]string, len(keys))
for index, key := range keys {
keyPath := fmt.Sprintf("%s.%s", statusesPath, key)
richLabel := fmt.Sprintf("[ ] %s", strings.ToUpper(key))
if config.Bool(keyPath) {
richLabel = fmt.Sprintf("[v] %s", strings.ToUpper(key))
}
statuses[index] = richLabel
}
return statuses
}
func loadProjects() {
ProjectsList.SetTitle(makeTabNames(ProjectsView))
savedProjects := GetSavedProjects()
sort.Strings(savedProjects)
if len(savedProjects) == 0 {
ProjectsList.SetTitle("No projects (Press 'a' to add)")
ProjectsList.Reset()
IssuesList.Reset()
IssuesList.SetTitle("No issues")
}
ProjectsList.SetItems(savedProjects)
}
func makeTabNames(name string) string {
switch name {
case ProjectsView:
return " Projects "
case StatusesView:
return " Projects > Statuses "
}
return "Something went wrong in making name"
}
func FetchIssues(g *ui.Gui, code string) error {
IssuesList.Reset()
IssuesList.SetCode(code)
issues, err := SearchIssuesByProjectCode(code)
if err != nil {
IssuesList.Title = fmt.Sprintf(" Failed to load issues from: %s ", code)
IssuesList.Clear()
return err
}
if len(issues) == 0 {
IssuesList.SetTitle(fmt.Sprintf("No issues in %s", code))
return nil
}
parsedIssues := make([]string, len(issues))
for index, issue := range issues {
key := issue.Key
summary := issue.Fields.Summary
row := fmt.Sprintf("%-2s %s", key, summary)
parsedIssues[index] = row
}
IssuesList.SetItems(parsedIssues)
return nil
}
func FetchStatuses(g *ui.Gui, code string) error {
StatusesList.Reset()
StatusesList.SetCode(code)
oldParsedStatuses := GetSavedStatusesByProjectCode(code)
if len(oldParsedStatuses) > 0 {
StatusesList.SetItems(oldParsedStatuses)
return nil
}
statuses, _, err := SearchStatusesByProjectCode(code)
if err != nil {
StatusesList.Title = " Projects > Statuses | Fetched failed "
StatusesList.Clear()
return err
}
if len(statuses) == 0 {
return nil
}
newParsedStatuses := make([]string, len(statuses))
for index, status := range statuses {
newParsedStatuses[index] = fmt.Sprintf("[v] %s", strings.ToUpper(status.Name))
}
StatusesList.SetItems(newParsedStatuses)
return nil
}
/*
* This helper will set the config into memory and write it to the file
*/
func writeConfigToFile() {
buff := new(bytes.Buffer)
if _, err := config.DumpTo(buff, config.Yaml); err != nil {
log.Printf("Error while dumping config file: %s\n", err)
}
configPath := getPaths()
if err := os.WriteFile(configPath, buff.Bytes(), 0755); err != nil {
log.Printf("Error while writing config file: %s\n", err)
}
if err := config.ReloadFiles(); err != nil {
log.Println("---- error on reload config")
}
}
func spaces(n int) string {
var s bytes.Buffer
for i := 0; i < n; i++ {
s.WriteString(" ")
}
return s.String()
}
// func startSpinner(g *ui.Gui, v *ui.View) {
// spinnerInterval := 110 * time.Millisecond
// oldTitle := v.Title
// // Set the initial spinner state
// spinnerState := 0
// spinnerFrames := []string{"|", "/", "-", "\\"}
// // Create a function to update the view's title with the spinner
// updateTitle := func(v *ui.View) {
// v.Title = fmt.Sprintf(" %s %s ", oldTitle, spinnerFrames[spinnerState])
// spinnerState = (spinnerState + 1) % len(spinnerFrames)
// }
// go func() {
// for {
// g.Update(func(g *ui.Gui) error {
// if v, err := g.View(StatusesView); err == nil {
// updateTitle(v)
// }
// return nil
// })
// time.Sleep(spinnerInterval)
// }
// }()
// }
func isNewUsernameView(v *ui.View) bool {
return strings.Contains(v.Title, InsertUsernameTitle) // || strings.Contains(v.Title, "try again")
}
func isNewServerView(v *ui.View) bool {
return strings.Contains(v.Title, InsertServerTitle)
}
func isNewProjectView(v *ui.View) bool {
return strings.Contains(v.Title, InsertNewCodeTitle)
}
func isDeleteView(v *ui.View) bool {
return strings.Contains(v.Title, DeleteConfirmTitle)
}
func isCreatingBranchView(v *ui.View) bool {
return strings.Contains(v.Title, NewBranchTitle)
}