0
0
mirror of https://github.com/thegeeklab/git-sv.git synced 2024-11-25 17:30:40 +00:00
git-sv/app/commands/prompt.go

174 lines
4.0 KiB
Go
Raw Normal View History

package commands
2020-12-02 02:15:51 +00:00
import (
2023-10-12 14:18:25 +00:00
"errors"
2020-12-02 02:15:51 +00:00
"fmt"
"reflect"
"regexp"
"github.com/manifoldco/promptui"
)
type commitType struct {
Type string
Description string
Example string
}
2023-10-12 14:18:25 +00:00
var errInvalidValue = errors.New("invalid value")
func promptType(types []string) (commitType, error) {
defaultTypes := map[string]commitType{
2023-10-12 14:18:25 +00:00
"build": {
Type: "build",
Description: "changes that affect the build system or external dependencies",
Example: "gradle, maven, go mod, npm",
},
"ci": {
Type: "ci",
Description: "changes to our CI configuration files and scripts",
Example: "Circle, BrowserStack, SauceLabs",
},
"chore": {
Type: "chore",
Description: "update something without impacting the user",
Example: "gitignore",
},
"docs": {
Type: "docs",
Description: "documentation only changes",
},
"feat": {
Type: "feat",
Description: "a new feature",
},
"fix": {
Type: "fix",
Description: "a bug fix",
},
"perf": {
Type: "perf",
Description: "a code change that improves performance",
},
"refactor": {
Type: "refactor",
Description: "a code change that neither fixes a bug nor adds a feature",
},
"style": {
Type: "style",
Description: "changes that do not affect the meaning of the code",
Example: "white-space, formatting, missing semi-colons, etc",
},
"test": {
Type: "test",
Description: "adding missing tests or correcting existing tests",
},
"revert": {
Type: "revert",
Description: "revert a single commit",
},
}
var items []commitType
2023-10-12 14:18:25 +00:00
for _, t := range types {
if v, exists := defaultTypes[t]; exists {
items = append(items, v)
} else {
items = append(items, commitType{Type: t})
}
2020-12-02 02:15:51 +00:00
}
template := &promptui.SelectTemplates{
Label: "{{ . }}",
Active: "> {{ .Type | white }} - {{ .Description | faint }}",
Inactive: " {{ .Type | white }} - {{ .Description | faint }}",
Selected: `{{ "type:" | faint }} {{ .Type | white }}`,
Details: `
{{ "Type:" | faint }} {{ .Type }}
{{ "Description:" | faint }} {{ .Description }}
{{ "Example:" | faint }} {{ .Example }}`,
}
i, err := promptSelect("type", items, template)
if err != nil {
return commitType{}, err
}
2023-10-12 14:18:25 +00:00
2020-12-02 02:15:51 +00:00
return items[i], nil
}
func promptScope(values []string) (string, error) {
if len(values) > 0 {
selected, err := promptSelect("scope", values, nil)
if err != nil {
return "", err
}
2023-10-12 14:18:25 +00:00
return values[selected], nil
}
2023-10-12 14:18:25 +00:00
2020-12-02 02:15:51 +00:00
return promptText("scope", "^[a-z0-9-]*$", "")
}
func promptSubject() (string, error) {
return promptText("subject", "^[a-z].+$", "")
}
func promptBody() (string, error) {
return promptText("body (leave empty to finish)", "^.*$", "")
}
func promptIssueID(issueLabel, issueRegex, defaultValue string) (string, error) {
return promptText(issueLabel, "^("+issueRegex+")?$", defaultValue)
}
func promptBreakingChanges() (string, error) {
2021-07-31 03:47:58 +00:00
return promptText("Breaking change description", "[a-z].+", "")
2020-12-02 02:15:51 +00:00
}
func promptSelect(label string, items interface{}, template *promptui.SelectTemplates) (int, error) {
if items == nil || reflect.TypeOf(items).Kind() != reflect.Slice {
2023-10-12 14:18:25 +00:00
return 0, fmt.Errorf("%w: %v is not a slice", errInvalidValue, items)
2020-12-02 02:15:51 +00:00
}
prompt := promptui.Select{
Label: label,
Size: reflect.ValueOf(items).Len(),
Items: items,
Templates: template,
}
index, _, err := prompt.Run()
2023-10-12 14:18:25 +00:00
2020-12-02 02:15:51 +00:00
return index, err
}
func promptText(label, regex, defaultValue string) (string, error) {
validate := func(input string) error {
regex := regexp.MustCompile(regex)
if !regex.MatchString(input) {
2023-10-12 14:18:25 +00:00
return fmt.Errorf("%w, expected: %s", errInvalidValue, regex)
2020-12-02 02:15:51 +00:00
}
2023-10-12 14:18:25 +00:00
2020-12-02 02:15:51 +00:00
return nil
}
prompt := promptui.Prompt{
Label: label,
Default: defaultValue,
Validate: validate,
}
return prompt.Run()
}
func promptConfirm(label string) (bool, error) {
r, err := promptText(label+" [y/n]", "^y|n$", "")
if err != nil {
return false, err
}
2023-10-12 14:18:25 +00:00
2020-12-02 02:15:51 +00:00
return r == "y", nil
}