forked from git-chglog/git-chglog
/
questioner.go
192 lines (167 loc) · 4.04 KB
/
questioner.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
package main
import (
"errors"
"fmt"
"path/filepath"
"strings"
gitcmd "github.com/tsuyoshiwada/go-gitcmd"
survey "gopkg.in/AlecAivazis/survey.v1"
)
// Answer ...
type Answer struct {
RepositoryURL string `survey:"repository_url"`
Style string `survey:"style"`
CommitMessageFormat string `survey:"commit_message_format"`
Template string `survey:"template"`
IncludeMerges bool `survey:"include_merges"`
IncludeReverts bool `survey:"include_reverts"`
ConfigDir string `survey:"config_dir"`
}
// Questioner ...
type Questioner interface {
Ask() (*Answer, error)
}
type questionerImpl struct {
client gitcmd.Client
fs FileSystem
}
// NewQuestioner ...
func NewQuestioner(client gitcmd.Client, fs FileSystem) Questioner {
return &questionerImpl{
client: client,
fs: fs,
}
}
// Ask ...
func (q *questionerImpl) Ask() (*Answer, error) {
ans, err := q.ask()
if err != nil {
return nil, err
}
config := filepath.Join(ans.ConfigDir, defaultConfigFilename)
tpl := filepath.Join(ans.ConfigDir, defaultTemplateFilename)
c := q.fs.Exists(config)
t := q.fs.Exists(tpl)
msg := ""
if c && t {
msg = fmt.Sprintf("\"%s\" and \"%s\" already exists. Do you want to overwrite?", config, tpl)
} else if c {
msg = fmt.Sprintf("\"%s\" already exists. Do you want to overwrite?", config)
} else if t {
msg = fmt.Sprintf("\"%s\" already exists. Do you want to overwrite?", tpl)
}
if msg != "" {
overwrite := false
err = survey.AskOne(&survey.Confirm{
Message: msg,
Default: true,
}, &overwrite, nil)
if err != nil || !overwrite {
return nil, errors.New("creation of the file was interrupted")
}
}
return ans, nil
}
func (q *questionerImpl) ask() (*Answer, error) {
ans := &Answer{}
fmts := q.getPreviewableList(formats)
tpls := q.getPreviewableList(templates)
var previewableTransform = func(ans interface{}) (newAns interface{}) {
if s, ok := ans.(string); ok {
newAns = q.parsePreviewableList(s)
}
return
}
questions := []*survey.Question{
{
Name: "repository_url",
Prompt: &survey.Input{
Message: "What is the URL of your repository?",
Default: q.getRepositoryURL(),
},
},
{
Name: "style",
Prompt: &survey.Select{
Message: "What is your favorite style?",
Options: styles,
Default: styles[0],
},
},
{
Name: "commit_message_format",
Prompt: &survey.Select{
Message: "Choose the format of your favorite commit message",
Options: fmts,
Default: fmts[0],
},
Transform: previewableTransform,
},
{
Name: "template",
Prompt: &survey.Select{
Message: "What is your favorite template style?",
Options: tpls,
Default: tpls[0],
},
Transform: previewableTransform,
},
{
Name: "include_merges",
Prompt: &survey.Confirm{
Message: "Do you include Merge Commit in CHANGELOG?",
Default: true,
},
},
{
Name: "include_reverts",
Prompt: &survey.Confirm{
Message: "Do you include Revert Commit in CHANGELOG?",
Default: true,
},
},
{
Name: "config_dir",
Prompt: &survey.Input{
Message: "In which directory do you output configuration files and templates?",
Default: defaultConfigDir,
},
},
}
err := survey.Ask(questions, ans)
if err != nil {
return nil, err
}
return ans, nil
}
func (*questionerImpl) getPreviewableList(list []Previewable) []string {
arr := make([]string, len(list))
max := 0
for _, p := range list {
l := len(p.Display())
if max < l {
max = l
}
}
for i, p := range list {
arr[i] = fmt.Sprintf(
"%s -- %s",
p.Display()+strings.Repeat(" ", max-len(p.Display())),
p.Preview(),
)
}
return arr
}
func (*questionerImpl) parsePreviewableList(input string) string {
return strings.TrimSpace(strings.Split(input, "--")[0])
}
func (q *questionerImpl) getRepositoryURL() string {
if q.client.CanExec() != nil || q.client.InsideWorkTree() != nil {
return ""
}
rawurl, err := q.client.Exec("config", "--get", "remote.origin.url")
if err != nil {
return ""
}
return remoteOriginURLToHTTP(rawurl)
}