Skip to content

Commit c325f1f

Browse files
committed
feat Refactor flags and update commit handling in main.go 🚀
1 parent c27cfa1 commit c325f1f

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

main.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,24 @@ func main() {
3333
cmd.Help()
3434
return
3535
}
36-
var newBranch, message string
37-
cmd.Flags().StringVarP(&message, "message", "m", "", "Auto generate commit message")
38-
cmd.Flags().StringVarP(&newBranch, "newBranch", "b", "", "Auto generate branch name")
39-
err := cmd.Flags().Parse(args)
36+
4037
if err == nil || strings.Contains(err.Error(), "flag needs an argument") {
4138
// Handle specific commands
4239
switch args[0] {
4340
case "commit":
41+
var message string
42+
var all bool
43+
cmd.Flags().StringVarP(&message, "message", "m", "", "Auto generate commit message")
44+
cmd.Flags().BoolVarP(&all, "all", "a", false, "Auto add all to stage")
45+
err := cmd.Flags().Parse(args)
4446
if err != nil && strings.Contains(err.Error(), "flag needs an argument") && message == "" {
45-
handleCommit(*config)
47+
handleCommit(*config, all)
4648
return
4749
}
4850
case "checkout":
51+
var newBranch string
52+
cmd.Flags().StringVarP(&newBranch, "newBranch", "b", "", "Auto generate branch name")
53+
err := cmd.Flags().Parse(args)
4954
if err != nil && strings.Contains(err.Error(), "flag needs an argument") && newBranch == "" {
5055
handleCheckout(*config)
5156
return
@@ -73,7 +78,7 @@ func main() {
7378
}
7479
}
7580

76-
func handleCommit(config ai.Config) {
81+
func handleCommit(config ai.Config, addAll bool) {
7782
// Get detailed git changes information
7883
changes, err := git.GetChanges()
7984
if err != nil {
@@ -148,9 +153,12 @@ func handleCommit(config ai.Config) {
148153
fmt.Println("Commit message is empty. Commit cancelled.")
149154
return
150155
}
151-
156+
arg := "-m"
157+
if addAll {
158+
arg = "-am"
159+
}
152160
// Execute git commit with the edited message
153-
commitCmd := exec.Command("git", "commit", "-m", message)
161+
commitCmd := exec.Command("git", "commit", arg, message)
154162
commitCmd.Stdout = os.Stdout
155163
commitCmd.Stderr = os.Stderr
156164

0 commit comments

Comments
 (0)