Skip to content

Commit

Permalink
fix 🐛: testing dynamic flags
Browse files Browse the repository at this point in the history
Signed-off-by: moualhi zine el abidine <zmoualhi@outlook.com>

fix 🐛: testing dynamic flags

Signed-off-by: moualhi zine el abidine <zmoualhi@outlook.com>

fix 🐛: testing dynamic flags

Signed-off-by: moualhi zine el abidine <zmoualhi@outlook.com>

fix 🐛: testing dynamic flags

Signed-off-by: moualhi zine el abidine <zmoualhi@outlook.com>
  • Loading branch information
muandane committed Aug 27, 2024
1 parent 5128cea commit 5e689b0
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 104 deletions.
167 changes: 67 additions & 100 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,113 +9,49 @@ import (
"github.com/fatih/color"
"github.com/muandane/goji/pkg/config"
"github.com/muandane/goji/pkg/utils"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

var (
version string
versionFlag bool
noVerifyFlag bool
typeFlag string
scopeFlag string
messageFlag string
addFlag bool
amendFlag bool
gitFlags []string
version, typeFlag, messageFlag, scopeFlag string
versionFlag, noVerifyFlag, amendFlag, addFlag bool
)

var rootCmd = &cobra.Command{
Use: "goji",
Short: "Goji CLI",
Long: `Goji is a cli tool to generate conventional commits with emojis`,
Run: func(cmd *cobra.Command, args []string) {
Long: `Goji is a CLI tool to generate conventional commits with emojis`,
RunE: func(cmd *cobra.Command, args []string) error {

Check warning on line 25 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L25

Added line #L25 was not covered by tests
if versionFlag {
color.Set(color.FgGreen)
fmt.Printf("goji version: v%s\n", version)
color.Unset()
return
color.Green("goji version: v%s", version)
return nil

Check warning on line 28 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L27-L28

Added lines #L27 - L28 were not covered by tests
}

_, err := config.GitRepo()
cfg, err := config.ViperConfig()

Check warning on line 31 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L31

Added line #L31 was not covered by tests
if err != nil {
log.Fatal().Msg(err.Error())
return fmt.Errorf("failed to load config: %w", err)

Check warning on line 33 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L33

Added line #L33 was not covered by tests
}

config, err := config.ViperConfig()
if err != nil {
log.Fatal().Msg(err.Error())
}
typeFlag, _ := cmd.Flags().GetString("type")
messageFlag, _ := cmd.Flags().GetString("message")

Check warning on line 37 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L36-L37

Added lines #L36 - L37 were not covered by tests

var commitMessage string
var commitBody string
var commitMessage, commitBody string

Check warning on line 39 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L39

Added line #L39 was not covered by tests
if typeFlag != "" && messageFlag != "" {
// If both flags are provided, construct the commit message from them
typeMatch := ""
for _, t := range config.Types {
if typeFlag == t.Name {
if !config.NoEmoji {
typeMatch = fmt.Sprintf("%s %s", t.Name, t.Emoji)
} else {
typeMatch = t.Name
}
break
}
}

// If no match was found, use the type flag as is
if typeMatch == "" {
typeMatch = typeFlag
}

// Construct the commit message from the flags
commitMessage = messageFlag
if typeMatch != "" {
commitMessage = fmt.Sprintf("%s: %s", typeMatch, commitMessage)
if scopeFlag != "" {
commitMessage = fmt.Sprintf("%s(%s): %s", typeMatch, scopeFlag, messageFlag)
}
}
commitMessage = constructCommitMessage(cfg, typeFlag, "", messageFlag)

Check warning on line 41 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L41

Added line #L41 was not covered by tests
} else {
// If not all flags are provided, fall back to the interactive prompt logic
commitMessages, err := utils.AskQuestions(config, typeFlag, messageFlag)
messages, err := utils.AskQuestions(cfg, typeFlag, messageFlag)

Check warning on line 43 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L43

Added line #L43 was not covered by tests
if err != nil {
log.Fatal().Msg(err.Error())
return fmt.Errorf("failed to get commit details: %w", err)

Check warning on line 45 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L45

Added line #L45 was not covered by tests
}
commitMessage = commitMessages[0]
commitBody = commitMessages[1]
commitMessage, commitBody = messages[0], messages[1]

Check warning on line 47 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L47

Added line #L47 was not covered by tests
}

if commitMessage == "" {
log.Fatal().Msg("Commit message cannot be empty")
}
var gitCommitError error
action := func() {
signOff := config.SignOff
var extraArgs []string
if noVerifyFlag {
extraArgs = append(extraArgs, "--no-verify")
}
if addFlag {
extraArgs = append(extraArgs, "-a")
}
if amendFlag {
extraArgs = append(extraArgs, "--amend")
}
command := buildCommitCommand(
commitMessage,
commitBody,
signOff,
extraArgs,
)
fmt.Println("Executing command:", strings.Join(append([]string{"git"}, command...), " "))
if err := commit(command); err != nil {
log.Fatal().Msg(err.Error())
}
}
action()
if gitCommitError != nil {
fmt.Println("\nError committing changes:", gitCommitError)
fmt.Println("Check the output above for details.")
return fmt.Errorf("commit message cannot be empty")

Check warning on line 51 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L51

Added line #L51 was not covered by tests
}

return executeGitCommit(commitMessage, commitBody, cfg.SignOff)

Check warning on line 54 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L54

Added line #L54 was not covered by tests
},
}

Expand All @@ -135,20 +71,29 @@ func init() {
rootCmd.Flags().StringVarP(&typeFlag, "type", "t", "", "Specify the type from the config file")
rootCmd.Flags().StringVarP(&scopeFlag, "scope", "s", "", "Specify a custom scope")
rootCmd.Flags().StringVarP(&messageFlag, "message", "m", "", "Specify a commit message")
rootCmd.Flags().
BoolVarP(&noVerifyFlag, "no-verify", "n", false, "bypass pre-commit and commit-msg hooks")
rootCmd.Flags().BoolVarP(&noVerifyFlag, "no-verify", "n", false, "bypass pre-commit and commit-msg hooks")
rootCmd.Flags().BoolVarP(&versionFlag, "version", "v", false, "Display version information")
rootCmd.Flags().
BoolVarP(&addFlag, "add", "a", false, "Automatically stage files that have been modified and deleted")
rootCmd.Flags().
BoolVar(&amendFlag, "amend", false, "Change last commit")
rootCmd.Flags().BoolVarP(&addFlag, "add", "a", false, "Automatically stage files that have been modified and deleted")
rootCmd.Flags().BoolVar(&amendFlag, "amend", false, "Change last commit")

rootCmd.Flags().StringArrayVar(&gitFlags, "git-flag", []string{}, "Git flags (can be used multiple times)")
}

func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
func constructCommitMessage(cfg *config.Config, typeFlag, scopeFlag, messageFlag string) string {
typeMatch := typeFlag
for _, t := range cfg.Types {
if typeFlag == t.Name {
if !cfg.NoEmoji {
typeMatch = fmt.Sprintf("%s %s", t.Name, t.Emoji)

Check warning on line 87 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L82-L87

Added lines #L82 - L87 were not covered by tests
}
break

Check warning on line 89 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L89

Added line #L89 was not covered by tests
}
}

if scopeFlag != "" {
return fmt.Sprintf("%s(%s): %s", typeMatch, scopeFlag, messageFlag)

Check warning on line 94 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L93-L94

Added lines #L93 - L94 were not covered by tests
}
return fmt.Sprintf("%s: %s", typeMatch, messageFlag)

Check warning on line 96 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L96

Added line #L96 was not covered by tests
}

// commit executes a git commit with the given message and body.
Expand All @@ -157,27 +102,49 @@ func Execute() {
// - message: the commit message.
// - body: the commit body.
// - sign: a boolean indicating whether to add a Signed-off-by trailer.
// - amend: a boolean indicating whether to amend the last commit.
// - commits the changes to git
//
// Returns:
// - error: an error if the git commit execution fails.
func buildCommitCommand(message string, body string, sign bool, extraArgs []string) []string {
func executeGitCommit(message, body string, signOff bool) error {
args := []string{"commit", "-m", message}

Check warning on line 111 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L110-L111

Added lines #L110 - L111 were not covered by tests
if body != "" {
args = append(args, "-m", body)
}
if sign {
if signOff {
args = append(args, "--signoff")

Check warning on line 116 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L115-L116

Added lines #L115 - L116 were not covered by tests
}
return append(args, extraArgs...)
}
var extraArgs []string
if noVerifyFlag {
extraArgs = append(extraArgs, "--no-verify")

Check warning on line 120 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L118-L120

Added lines #L118 - L120 were not covered by tests
}
if addFlag {
extraArgs = append(extraArgs, "-a")

Check warning on line 123 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L122-L123

Added lines #L122 - L123 were not covered by tests
}
if amendFlag {
extraArgs = append(extraArgs, "--amend")

Check warning on line 126 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L125-L126

Added lines #L125 - L126 were not covered by tests
}
// Add all dynamically passed Git flags
baseArgs := append(args, extraArgs...)
args = append(baseArgs, gitFlags...)

Check warning on line 130 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L129-L130

Added lines #L129 - L130 were not covered by tests

// commit commits the changes to git
func commit(args []string) error {
gitCmd := exec.Command("git", args...)
output, err := gitCmd.CombinedOutput()

Check warning on line 133 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L132-L133

Added lines #L132 - L133 were not covered by tests
// Print the executed command
fmt.Println("Executing command:", strings.Join(append([]string{"git"}, args...), " "))

Check warning on line 135 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L135

Added line #L135 was not covered by tests
// Print the command output
if err != nil {
return fmt.Errorf("git command failed: %v\nOutput: %s", err, output)

Check warning on line 138 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L137-L138

Added lines #L137 - L138 were not covered by tests
}
fmt.Printf("Git command output:\n%s", output)

Check warning on line 140 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L140

Added line #L140 was not covered by tests

return nil
}

func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)

Check warning on line 148 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L145-L148

Added lines #L145 - L148 were not covered by tests
}
}
7 changes: 3 additions & 4 deletions pkg/utils/askQuestions.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ func AskQuestions(config *config.Config, presetType, presetMessage string) ([]st
}
}
}
if presetMessage != "" {
commitSubject = presetMessage

Check warning on line 43 in pkg/utils/askQuestions.go

View check run for this annotation

Codecov / codecov/patch

pkg/utils/askQuestions.go#L42-L43

Added lines #L42 - L43 were not covered by tests
}

form := huh.NewForm(
huh.NewGroup(
Expand Down Expand Up @@ -85,10 +88,6 @@ func AskQuestions(config *config.Config, presetType, presetMessage string) ([]st
),
)

if presetMessage != "" {
commitSubject = presetMessage
}

if err := form.Run(); err != nil {
return nil, err

Check warning on line 92 in pkg/utils/askQuestions.go

View check run for this annotation

Codecov / codecov/patch

pkg/utils/askQuestions.go#L91-L92

Added lines #L91 - L92 were not covered by tests
}
Expand Down

0 comments on commit 5e689b0

Please sign in to comment.