forked from stefanlogue/meteor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
315 lines (281 loc) · 8.06 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
package main
import (
"fmt"
"os"
"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
FS afero.Fs = afero.NewOsFs()
AFS *afero.Afero = &afero.Afero{Fs: FS}
)
func init() {
flag.BoolP("version", "v", false, "show version")
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("Error: %s", err)
}
gitRoot, err := findGitDir()
if err != nil {
fail("Error: %s", err)
}
if err := os.Chdir(gitRoot); err != nil {
fail("Could not change directory: %s", err)
}
prefixes, coauthors, boards, showIntro, commitTitleCharLimit, err := loadConfig(AFS)
if err != nil {
fail("Error: %s", err)
}
var newCommit Commit
theme := huh.ThemeCatppuccin()
if showIntro {
introForm := huh.NewForm(
huh.NewGroup(
splashScreen(),
),
)
if err := introForm.Run(); err != nil {
fail("Error: %s", err)
}
}
if len(boards) > 0 {
boardForm := huh.NewForm(
huh.NewGroup(
huh.NewSelect[string]().
Title("Board").
Description("Select the board for this commit").
Options(boards...).
Value(&newCommit.Board),
).WithHideFunc(func() bool {
return len(boards) < 1
}),
).WithTheme(theme)
err = boardForm.Run()
if err != nil {
fail("Error: %s", 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(boards) < 1
}),
).WithTheme(theme)
err = ticketNumberForm.Run()
if err != nil {
fail("Error: %s", err)
}
}
mainForm := huh.NewForm(
huh.NewGroup(
huh.NewSelect[string]().
Title("Type").
Description("Select the type of change that you're committing").
Options(prefixes...).
Value(&newCommit.Type),
huh.NewConfirm().
Title("Breaking Change").
Description("Is this a breaking change?").
Affirmative("Yes!").
Negative("Nope.").
Value(&newCommit.IsBreakingChange),
huh.NewInput().
Title("Scope").
Description("Specify a scope of the change").
CharLimit(16).
Value(&newCommit.Scope),
),
huh.NewGroup(
huh.NewMultiSelect[string]().
Title("Coauthors").
Description("Select any coauthors for this commit").
Options(coauthors...).
Value(&newCommit.Coauthors),
).WithHideFunc(func() bool {
return len(coauthors) < 1
}),
).WithTheme(theme)
err = mainForm.Run()
if err != nil {
fail("Error: %s", err)
}
if len(newCommit.Board) > 0 && newCommit.Board != "NONE" {
if newCommit.IsBreakingChange {
if len(newCommit.Scope) > 0 {
newCommit.Message = fmt.Sprintf("%s(%s)!: <%s> ", newCommit.TicketNumber, newCommit.Scope, newCommit.Type)
} else {
newCommit.Message = fmt.Sprintf("%s!: <%s> ", newCommit.TicketNumber, newCommit.Type)
}
} else {
if len(newCommit.Scope) > 0 {
newCommit.Message = fmt.Sprintf("%s(%s): <%s> ", newCommit.TicketNumber, newCommit.Scope, newCommit.Type)
} else {
newCommit.Message = fmt.Sprintf("%s: <%s> ", newCommit.TicketNumber, newCommit.Type)
}
}
} else {
if newCommit.IsBreakingChange {
if len(newCommit.Scope) > 0 {
newCommit.Message = fmt.Sprintf("%s(%s)!: ", newCommit.Type, newCommit.Scope)
} else {
newCommit.Message = fmt.Sprintf("%s!: ", newCommit.Type)
}
} else {
if len(newCommit.Scope) > 0 {
newCommit.Message = fmt.Sprintf("%s(%s): ", newCommit.Type, newCommit.Scope)
} else {
newCommit.Message = fmt.Sprintf("%s: ", newCommit.Type)
}
}
}
doesWantToCommit := true
messageForm := huh.NewForm(
huh.NewGroup(
huh.NewInput().
Value(&newCommit.Message).
Title("Message").
CharLimit(commitTitleCharLimit),
huh.NewText().
Value(&newCommit.Body).
Title("Body").
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("shift+tab"), key.WithHelp("shift+tab", "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("shift+tab"), key.WithHelp("shift+tab", "back")),
Submit: key.NewBinding(key.WithKeys("enter", "tab"), key.WithHelp("enter / tab", "submit")),
},
}).WithTheme(theme)
err = messageForm.Run()
if err != nil {
fail("Error: %s", err)
}
if len(newCommit.Coauthors) > 0 {
newCommit.Body = newCommit.Body + buildCoauthorString(newCommit.Coauthors)
}
args := flag.Args()
rawCommitCommand, printableCommitCommand := buildCommitCommand(newCommit.Message, newCommit.Body, args)
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) {
clipboard.WriteAll(s)
}
// 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)
}