-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.go
372 lines (324 loc) · 9.95 KB
/
main.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package main
import (
"bytes"
"fmt"
"os"
"text/template"
"time"
"github.com/charmbracelet/log"
"github.com/fatih/color"
"github.com/atotto/clipboard"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/huh"
"github.com/spf13/afero"
flag "github.com/spf13/pflag"
)
type Commit struct {
Board string
TicketNumber string
Type string
Scope string
Message string
Body string
Coauthors []string
IsBreakingChange bool
}
// isFlagPassed checks if a flag has been passed
func isFlagPassed(name string) bool {
found := false
flag.Visit(func(f *flag.Flag) {
if f.Name == name {
found = true
}
})
return found
}
var (
version = "dev"
debugMode bool
skipIntro bool
FS afero.Fs = afero.NewOsFs()
AFS *afero.Afero = &afero.Afero{Fs: FS}
)
const (
AsGitEditor = "as-git-editor"
ErrorString = "Error: %s"
ShiftTab = "shift+tab"
)
func init() {
flag.BoolP("version", "v", false, "show version")
flag.BoolP(AsGitEditor, "e", false, "used as GIT_EDITOR")
flag.BoolVarP(&skipIntro, "skip-intro", "s", false, "skip intro splash")
flag.BoolVarP(&debugMode, "debug", "D", false, "enable debug mode")
flag.Parse()
if isFlagPassed("version") {
fmt.Printf("meteor version %s\n", version)
os.Exit(0)
}
programLevel := log.InfoLevel
if debugMode {
programLevel = log.DebugLevel
}
logger := log.NewWithOptions(os.Stderr, log.Options{
ReportCaller: false,
ReportTimestamp: true,
TimeFormat: time.DateTime,
})
logger.SetLevel(programLevel)
log.SetDefault(logger)
}
func main() {
if err := checkGitInPath(); err != nil {
fail(ErrorString, err)
}
gitRoot, err := findGitDir()
if err != nil {
fail(ErrorString, err)
}
if err := os.Chdir(gitRoot); err != nil {
fail("Could not change directory: %s", err)
}
config, err := loadConfig(AFS)
if err != nil {
fail(ErrorString, err)
}
var newCommit Commit
theme := huh.ThemeCatppuccin()
if config.ShowIntro && (isFlagPassed("skip-intro") && !skipIntro) {
introForm := huh.NewForm(
huh.NewGroup(
splashScreen(),
),
)
if err := introForm.Run(); err != nil {
fail(ErrorString, err)
}
}
if len(config.Boards) > 0 {
boardForm := huh.NewForm(
huh.NewGroup(
huh.NewSelect[string]().
Title("Board").
Description("Select the board for this commit").
Options(config.Boards...).
Value(&newCommit.Board),
).WithHideFunc(func() bool {
return len(config.Boards) < 1
}),
).WithTheme(theme)
err = boardForm.Run()
if err != nil {
fail(ErrorString, err)
}
}
if len(newCommit.Board) > 0 && newCommit.Board != "NONE" {
ticketNumber := getGitTicketNumber(newCommit.Board)
if ticketNumber == "" {
newCommit.TicketNumber = fmt.Sprintf("%s-", newCommit.Board)
} else {
newCommit.TicketNumber = ticketNumber
}
ticketNumberForm := huh.NewForm(
huh.NewGroup(
huh.NewInput().
Title("Ticket number").
Description("The ticket number associated with this commit").
CharLimit(10).
Value(&newCommit.TicketNumber),
).WithHideFunc(func() bool {
return len(config.Boards) < 1
}),
).WithTheme(theme)
err = ticketNumberForm.Run()
if err != nil {
fail(ErrorString, err)
}
}
// if the user has specified scopes in their config, use a select input, otherwise use a text input
var scopeInput huh.Field
if len(config.Scopes) > 0 {
scopeInput = huh.NewSelect[string]().
Title("Scope").
Description("Choose a scope for the changes").
Options(config.Scopes...).
Value(&newCommit.Scope)
} else {
scopeInput = huh.NewInput().
Title("Scope").
Description("Specify a scope of the changes").
CharLimit(16).
Value(&newCommit.Scope)
}
mainForm := huh.NewForm(
huh.NewGroup(
huh.NewSelect[string]().
Title("Type").
Description("Select the type of change that you're committing").
Options(config.Prefixes...).
Value(&newCommit.Type),
huh.NewConfirm().
Title("Breaking Change").
Description("Is this a breaking change?").
Affirmative("Yes!").
Negative("Nope.").
Value(&newCommit.IsBreakingChange),
scopeInput,
),
huh.NewGroup(
huh.NewMultiSelect[string]().
Title("Coauthors").
Description("Select any coauthors for this commit").
Options(config.Coauthors...).
Value(&newCommit.Coauthors),
).WithHideFunc(func() bool {
return len(config.Coauthors) < 1
}),
).WithTheme(theme)
err = mainForm.Run()
if err != nil {
fail(ErrorString, err)
}
var tmpl *template.Template
if len(newCommit.Board) > 0 && newCommit.Board != "NONE" {
tmpl = template.Must(template.New("message").Parse(config.MessageWithTicketTemplate))
} else {
tmpl = template.Must(template.New("message").Parse(config.MessageTemplate))
}
buf := new(bytes.Buffer)
err = tmpl.Execute(buf, newCommit)
if err != nil {
fail(ErrorString, err)
}
newCommit.Message = buf.String()
doesWantToCommit := true
messageForm := huh.NewForm(
huh.NewGroup(
huh.NewInput().
Value(&newCommit.Message).
Title("Message").
CharLimit(config.CommitTitleCharLimit),
huh.NewText().
Value(&newCommit.Body).
Title("Body").
CharLimit(0).
Lines(8),
),
huh.NewGroup(
huh.NewConfirm().
Title("Ready to commit?").
Affirmative("Yes!").
Negative("No.").
Value(&doesWantToCommit),
),
).WithKeyMap(&huh.KeyMap{
Quit: key.NewBinding(key.WithKeys("ctrl+c"), key.WithHelp("ctrl+c", "quit")),
Text: huh.TextKeyMap{
Next: key.NewBinding(key.WithKeys("enter"), key.WithHelp("enter", "next")),
NewLine: key.NewBinding(key.WithKeys("alt+enter", "ctrl+j"), key.WithHelp("alt+enter / ctrl+j", "new line")),
Editor: key.NewBinding(key.WithKeys("ctrl+e"), key.WithHelp("ctrl+e", "open editor")),
Prev: key.NewBinding(key.WithKeys(ShiftTab), key.WithHelp(ShiftTab, "back")),
},
Input: huh.InputKeyMap{
Next: key.NewBinding(key.WithKeys("enter", "tab"), key.WithHelp("enter / tab", "next")),
},
Confirm: huh.ConfirmKeyMap{
Toggle: key.NewBinding(key.WithKeys("left", "right", "h", "l"), key.WithHelp("left / right", "toggle")),
Prev: key.NewBinding(key.WithKeys(ShiftTab), key.WithHelp(ShiftTab, "back")),
Submit: key.NewBinding(key.WithKeys("enter", "tab"), key.WithHelp("enter / tab", "submit")),
},
}).WithTheme(theme)
err = messageForm.Run()
if err != nil {
fail(ErrorString, err)
}
if len(newCommit.Coauthors) > 0 {
newCommit.Body = newCommit.Body + buildCoauthorString(newCommit.Coauthors)
}
args := flag.Args()
var commitFile string
// If we're operating in GIT_EDITOR="meteor --as-git-editor" mode, the first argument is the path (.git/COMMIT_EDITMSG)
// where we should write the git commit message, so we shift that from args before constructing the end-user command line
if isFlagPassed(AsGitEditor) {
commitFile = args[0]
args = args[1:]
}
rawCommitCommand, printableCommitCommand := buildCommitCommand(newCommit.Message, newCommit.Body, args)
if isFlagPassed(AsGitEditor) {
// We intent to do the commit
if doesWantToCommit {
// Write the commit message file (.git/COMMIT_EDITMSG) in same format as git would have,
// the message, a blank line, and a body - if body is empty, trailing newlines will be removed
if err := os.WriteFile(commitFile, bytes.TrimRight([]byte(newCommit.Message+"\n\n"+newCommit.Body), "/n"), os.FileMode(os.O_WRONLY)); err != nil {
// In case of failure, give the regular error-ish output to the end-user so no inputs are lost
writeToClipboard(printableCommitCommand)
fail(
"\n%s\n%s\n\n%s\n\n",
color.RedString(fmt.Sprintf("It looks like the commit failed.\nError: %s", err)),
color.YellowString("To run it again without going through meteor's wizard, simply run the following command (I've copied it to your clipboard!):"),
color.BlueString(printableCommitCommand),
)
return
}
// we wrote the commit message file, nothing left for us to do, success!
return
}
// end-user decided to abort the commit, which mean we don't write the git commit message file (.git/COMMIT_EDITMSG)
// which will make git abort the operation
writeToClipboard(printableCommitCommand)
fmt.Printf(
"\n%s\n\n%s\n%s\n\n",
color.RedString("Commit aborted."),
color.YellowString("I've copied the following command to your clipboard, so you can run it again later:"),
color.BlueString(printableCommitCommand))
return
}
if doesWantToCommit {
err := commit(rawCommitCommand)
if err != nil {
writeToClipboard(printableCommitCommand)
fail(
"\n%s\n%s\n\n%s\n\n",
color.RedString(fmt.Sprintf("It looks like the commit failed.\nError: %s", err)),
color.YellowString("To run it again without going through meteor's wizard, simply run the following command (I've copied it to your clipboard!):"),
color.BlueString(printableCommitCommand),
)
}
} else {
writeToClipboard(printableCommitCommand)
fmt.Printf(
"\n%s\n\n%s\n%s\n\n",
color.RedString("Commit aborted."),
color.YellowString("I've copied the following command to your clipboard, so you can run it again later:"),
color.BlueString(printableCommitCommand))
}
}
// writeToClipboard writes a string to the clipboard
func writeToClipboard(s string) {
if err := clipboard.WriteAll(s); err != nil {
fail("Failed to copy to clipboard: %s", err)
}
}
// buildCoauthorString takes a slice of selected coauthors and returns a formatted
// string which Github recognises
func buildCoauthorString(coauthors []string) string {
s := `
`
for _, coauthor := range coauthors {
if coauthor == "none" {
return ""
}
s += fmt.Sprintf("\nCo-authored-by: %s", coauthor)
}
return s
}
// splashScreen returns a note with a splash screen
func splashScreen() *huh.Note {
return huh.NewNote().
Title("meteor").
Description("A highly customisable command line tool\nfor writing conventional commit messages")
}
// fail prints an error message and exits with a non-zero exit code
func fail(format string, args ...interface{}) {
_, _ = fmt.Fprintf(os.Stderr, format+"\n", args...)
os.Exit(1)
}