This repository has been archived by the owner on Nov 22, 2022. It is now read-only.
/
cmdutils.go
174 lines (155 loc) · 4.34 KB
/
cmdutils.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
package cmdutils
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/profclems/glab/internal/glrepo"
"github.com/profclems/glab/pkg/api"
"github.com/xanzy/go-gitlab"
"github.com/AlecAivazis/survey/v2"
"github.com/profclems/glab/pkg/prompt"
"github.com/profclems/glab/pkg/surveyext"
"github.com/profclems/glab/internal/config"
"github.com/profclems/glab/internal/git"
)
const (
IssueTemplate = "issue_templates"
MergeRequestTemplate = "merge_request_templates"
)
// LoadGitLabTemplate finds and loads the GitLab template from the working git directory
// Follows the format officially supported by GitLab
// https://docs.gitlab.com/ee/user/project/description_templates.html#setting-a-default-template-for-issues-and-merge-requests.
//
// TODO: load from remote repository if repo is overriden by -R flag
func LoadGitLabTemplate(tmplType, tmplName string) (string, error) {
wdir, err := git.ToplevelDir()
if err != nil {
return "", err
}
if !strings.HasSuffix(tmplName, ".md") {
tmplName = tmplName + ".md"
}
tmplFile := filepath.Join(wdir, ".gitlab", tmplType, tmplName)
f, err := os.Open(tmplFile)
if os.IsNotExist(err) {
return "", nil
} else if err != nil {
return "", err
}
tmpl, err := ioutil.ReadAll(f)
if err != nil {
return "", err
}
return strings.TrimSpace(string(tmpl)), nil
}
// TODO: properly handle errors in this function.
// For now, it returns nil and empty slice if there's an error
func ListGitLabTemplates(tmplType string) ([]string, error) {
wdir, err := git.ToplevelDir()
tmplFolder := filepath.Join(wdir, ".gitlab", tmplType)
var files []string
f, err := os.Open(tmplFolder)
// if error return an empty slice since it only returns PathError
if err != nil {
return files, nil
}
fileNames, err := f.Readdirnames(-1)
defer f.Close()
if err != nil {
// return empty slice if error
return files, nil
}
for _, file := range fileNames {
files = append(files, strings.TrimSuffix(file, ".md"))
}
return files, nil
}
func GetEditor(cf func() (config.Config, error)) (string, error) {
cfg, err := cf()
if err != nil {
return "", fmt.Errorf("could not read config: %w", err)
}
// will search in the order glab_editor, visual, editor first from the env before the config file
editorCommand, _ := cfg.Get("", "editor")
return editorCommand, nil
}
func DescriptionPrompt(response *string, templateContent, editorCommand string) error {
defaultBody := *response
if templateContent != "" {
if defaultBody != "" {
// prevent excessive newlines between default body and template
defaultBody = strings.TrimRight(defaultBody, "\n")
defaultBody += "\n\n"
}
defaultBody += templateContent
}
qs := []*survey.Question{
{
Name: "Description",
Prompt: &surveyext.GLabEditor{
BlankAllowed: true,
EditorCommand: editorCommand,
Editor: &survey.Editor{
Message: "Description",
FileName: "*.md",
Default: defaultBody,
HideDefault: true,
AppendDefault: true,
},
},
},
}
err := prompt.Ask(qs, response)
if err != nil {
return err
}
if *response == "" {
*response = defaultBody
}
return nil
}
func LabelsPrompt(response *string, apiClient *gitlab.Client, repoRemote *glrepo.Remote) (err error) {
var addLabels bool
err = prompt.Confirm(&addLabels, "Do you want to add labels?", true)
if err != nil {
return
}
if addLabels {
labelOptions, _ := git.Config("remote." + repoRemote.Name + ".glab-cached-labels")
if labelOptions == "" {
lOpts := &gitlab.ListLabelsOptions{}
lOpts.PerPage = 100
labels, err := api.ListLabels(apiClient, repoRemote.FullName(), lOpts)
if err == nil && labels != nil {
for i, label := range labels {
if i > 0 {
labelOptions += ","
}
labelOptions += label.Name
}
if labelOptions != "" {
// silently fails if not a git repo
_ = git.SetConfig(repoRemote.Name, "glab-cached-labels", labelOptions)
}
}
}
if labelOptions != "" {
var selectedLabels []string
err = prompt.MultiSelect(&selectedLabels, "Select Labels", strings.Split(labelOptions, ","))
if err != nil {
return err
}
if len(selectedLabels) > 0 {
*response = strings.Join(selectedLabels, ",")
}
} else {
err = prompt.AskQuestionWithInput(response, "Label(s) [Comma Separated]", "", false)
if err != nil {
return err
}
}
}
return nil
}