diff --git a/README.md b/README.md index e69672a..b865720 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,11 @@ There is already a nice [gitmoji-cli][gitmoji-cli] command line interface available. Because I was searching for a nice project to get more into golang this project was created. +It is possible to use different commit message formats. +Per default the format is [conventional-commits[conventional-commits] with emoji +`[optional scope]: :smile: ` + + ## Configuration It is possible to configure the cli either with a `.go-gitmoji-cli.json` file within the repo directory @@ -59,6 +64,7 @@ make help [gitmoji]: https://gitmoji.dev/ [gitmoji-cli]: https://github.com/carloscuesta/gitmoji-cli +[conventional-commits]: https://www.conventionalcommits.org/en/v1.0.0/ ## Contributors ✨ diff --git a/cmd/commit.go b/cmd/commit.go index bad651d..26b1706 100644 --- a/cmd/commit.go +++ b/cmd/commit.go @@ -1,17 +1,18 @@ package cmd import ( - "fmt" "github.com/AndreasAugustin/go-gitmoji-cli/pkg" "github.com/AndreasAugustin/go-gitmoji-cli/pkg/ui" log "github.com/sirupsen/logrus" - "strings" - "github.com/spf13/cobra" ) var commitMsg []string var isDryRun bool +var scope string +var desc string +var isBreaking bool +var _type string // CommitCmd represents the commit command var CommitCmd = &cobra.Command{ @@ -45,18 +46,10 @@ to quickly create a Cobra application.`, log.Debugf("selected gitmoji %s", selectedGitmoji) textInputsData := buildTextInputsData() inputsRes := ui.TextInputsRun("please add", textInputsData) - scopeAndTitle := buildTitleEventualWithScope(inputsRes) - log.Debugf("scope and title: %s ", scopeAndTitle) - var selectedEmoji string - if pkg.ConfigInstance.EmojiFormat == pkg.CODE { - selectedEmoji = selectedGitmoji.Code - } else { - selectedEmoji = selectedGitmoji.Emoji - } - completeMessage := fmt.Sprintf("%s %s", selectedEmoji, scopeAndTitle) - log.Debugf("complete message: %s", completeMessage) + msg := pkg.BuildCommitTitle(extractType(inputsRes), extractScope(inputsRes), isBreaking, extractDesc(inputsRes), selectedGitmoji, pkg.ConfigInstance) + log.Debugf("complete message: %s", msg) if isDryRun { - log.Infof("The commit message: %s", completeMessage) + log.Infof("The commit message: %s", msg) } //longMessage := ui.TextAreaRun() // TODO(anau) check if message prompt enabled @@ -70,31 +63,38 @@ func init() { var a []string CommitCmd.PersistentFlags().StringSliceVarP(&commitMsg, "message", "m", a, "The commit message. Can be repeated") CommitCmd.PersistentFlags().BoolVarP(&isDryRun, "dry-run", "n", false, "dry run: just output the commit message without doing a commit") + CommitCmd.PersistentFlags().StringVar(&_type, "type", "", "add the type") + CommitCmd.PersistentFlags().StringVar(&scope, "scope", "", "add a scope") + CommitCmd.PersistentFlags().StringVar(&desc, "desc", "", "add a description") + CommitCmd.PersistentFlags().BoolVar(&isBreaking, "is-breaking", false, "set if the commit is a breaking change") } -func buildTitleEventualWithScope(inputsRes []string) string { - if len(inputsRes) == 0 { - return "" - } else if len(inputsRes) == 1 { - return eventualCapitalizeTitle(inputsRes[0]) +func extractType(inputsRes []string) string { + return inputsRes[0] +} + +func extractScope(inputsRes []string) string { + if len(inputsRes) > 2 { + return inputsRes[1] } else { - return fmt.Sprintf("(%s) %s", inputsRes[0], eventualCapitalizeTitle(inputsRes[1])) + return scope } } -func eventualCapitalizeTitle(title string) string { - if pkg.ConfigInstance.CapitalizeTitle { - return strings.ToUpper(title) +func extractDesc(inputsRes []string) string { + if len(inputsRes) > 2 { + return inputsRes[2] + } else { + return inputsRes[1] } - return title } func buildTextInputsData() []ui.TextInputData { - var textInputsData []ui.TextInputData + var textInputsData = []ui.TextInputData{{Placeholder: "type", Charlimit: 64, Label: "type", InitialValue: _type}} if pkg.ConfigInstance.ScopePrompt { - textInputsData = append(textInputsData, ui.TextInputData{Placeholder: "scope", Charlimit: 64, Label: "scope"}) + textInputsData = append(textInputsData, ui.TextInputData{Placeholder: "scope", Charlimit: 64, Label: "scope", InitialValue: scope}) } - return append(textInputsData, ui.TextInputData{Placeholder: "title", Charlimit: 64, Label: "title"}) + return append(textInputsData, ui.TextInputData{Placeholder: "description", Charlimit: 64, Label: "description", InitialValue: desc}) } diff --git a/pkg/commit.go b/pkg/commit.go new file mode 100644 index 0000000..93d7381 --- /dev/null +++ b/pkg/commit.go @@ -0,0 +1,41 @@ +package pkg + +import ( + "fmt" + "strings" +) + +func BuildCommitTitle(_type string, scope string, isBreaking bool, desc string, gitmoji Gitmoji, config Config) string { + + var s strings.Builder + + s.WriteString(_type) + + if scope != "" { + s.WriteString(fmt.Sprintf("(%s)", scope)) + } + + if isBreaking { + s.WriteString("!") + } + + s.WriteString(fmt.Sprintf(": %s ", gitmojiToString(gitmoji, config))) + s.WriteString(eventualCapitalizeTitle(desc, config)) + + return s.String() +} + +func gitmojiToString(gitmoji Gitmoji, config Config) string { + if config.EmojiFormat == CODE { + return gitmoji.Code + } else { + return gitmoji.Emoji + } +} + +func eventualCapitalizeTitle(title string, config Config) string { + if config.CapitalizeTitle { + return strings.ToUpper(title) + } + return title +} diff --git a/pkg/commit_test.go b/pkg/commit_test.go new file mode 100644 index 0000000..24ede61 --- /dev/null +++ b/pkg/commit_test.go @@ -0,0 +1 @@ +package pkg_test