@@ -13,13 +13,6 @@ import (
1313 "github.com/somaz94/go-git-commit-action/internal/gitcmd"
1414)
1515
16- // CommandDef defines a command to be executed
17- type CommandDef struct {
18- name string
19- args []string
20- desc string
21- }
22-
2316// FileBackup is a struct for file backups.
2417type FileBackup struct {
2518 path string
@@ -161,37 +154,15 @@ func changeWorkingDirectory(config *config.GitConfig) error {
161154// setupGitConfig configures Git with user information and safety settings.
162155// It runs a series of git config commands to ensure the proper environment.
163156func setupGitConfig (config * config.GitConfig ) error {
164- baseCommands := []CommandDef {
157+ baseCommands := []Command {
165158 {gitcmd .CmdGit , gitcmd .ConfigSafeDirArgs (gitcmd .PathApp ), "Setting safe directory (/app)" },
166159 {gitcmd .CmdGit , gitcmd .ConfigSafeDirArgs (gitcmd .PathGitHubWorkspace ), "Setting safe directory (/github/workspace)" },
167160 {gitcmd .CmdGit , gitcmd .ConfigUserEmailArgs (config .UserEmail ), "Configuring user email" },
168161 {gitcmd .CmdGit , gitcmd .ConfigUserNameArgs (config .UserName ), "Configuring user name" },
169162 {gitcmd .CmdGit , gitcmd .ConfigListArgs (), "Checking git configuration" },
170163 }
171164
172- return executeCommandBatch (baseCommands , "\n ⚙️ Executing Git Commands:" )
173- }
174-
175- // executeCommandBatch runs a batch of commands, providing consistent output
176- // formatting and error handling.
177- func executeCommandBatch (commands []CommandDef , headerMessage string ) error {
178- if headerMessage != "" {
179- fmt .Println (headerMessage )
180- }
181-
182- for _ , cmd := range commands {
183- fmt .Printf (" • %s... " , cmd .desc )
184- command := exec .Command (cmd .name , cmd .args ... )
185- command .Stdout = os .Stdout
186- command .Stderr = os .Stderr
187-
188- if err := command .Run (); err != nil {
189- fmt .Println ("❌ Failed" )
190- return fmt .Errorf ("failed to execute %s: %v" , cmd .name , err )
191- }
192- fmt .Println ("✅ Done" )
193- }
194- return nil
165+ return ExecuteCommandBatch (baseCommands , "\n ⚙️ Executing Git Commands:" )
195166}
196167
197168// handleBranch manages branch-related operations, checking for local and remote
@@ -219,12 +190,12 @@ func handleBranch(config *config.GitConfig) error {
219190// createNewBranch creates a new branch and pushes it to the remote repository.
220191func createNewBranch (config * config.GitConfig ) error {
221192 fmt .Printf ("\n ⚠️ Branch '%s' not found, creating it...\n " , config .Branch )
222- createCommands := []CommandDef {
193+ createCommands := []Command {
223194 {gitcmd .CmdGit , gitcmd .CheckoutNewBranchArgs (config .Branch ), "Creating new branch" },
224195 {gitcmd .CmdGit , gitcmd .PushUpstreamArgs (gitcmd .RefOrigin , config .Branch ), "Pushing new branch" },
225196 }
226197
227- return executeCommandBatch (createCommands , "" )
198+ return ExecuteCommandBatch (createCommands , "" )
228199}
229200
230201// checkoutRemoteBranch checks out an existing remote branch while handling
@@ -329,13 +300,13 @@ func stashChanges() error {
329300
330301// fetchAndCheckout fetches the remote branch and checks it out locally.
331302func fetchAndCheckout (config * config.GitConfig ) error {
332- checkoutCommands := []CommandDef {
303+ checkoutCommands := []Command {
333304 {gitcmd .CmdGit , gitcmd .FetchArgs (gitcmd .RefOrigin , config .Branch ), "Fetching remote branch" },
334305 {gitcmd .CmdGit , gitcmd .CheckoutArgs (config .Branch ), "Checking out branch" },
335306 {gitcmd .CmdGit , gitcmd .ResetHardArgs (fmt .Sprintf ("origin/%s" , config .Branch )), "Resetting to remote state" },
336307 }
337308
338- return executeCommandBatch (checkoutCommands , "" )
309+ return ExecuteCommandBatch (checkoutCommands , "" )
339310}
340311
341312// restoreChanges brings back the backed up files after branch switching.
@@ -480,30 +451,10 @@ func executeGitAdd(pattern string) error {
480451
481452// performCommitAndPush commits the staged changes and pushes them to the remote.
482453func performCommitAndPush (config * config.GitConfig ) error {
483- commitPushCommands := []CommandDef {
454+ commitPushCommands := []Command {
484455 {gitcmd .CmdGit , gitcmd .CommitArgs (config .CommitMessage ), "Committing changes" },
485456 {gitcmd .CmdGit , gitcmd .PushArgs (gitcmd .RefOrigin , config .Branch ), "Pushing to remote" },
486457 }
487458
488- for _ , cmd := range commitPushCommands {
489- fmt .Printf (" • %s... " , cmd .desc )
490- command := exec .Command (cmd .name , cmd .args ... )
491- command .Stdout = os .Stdout
492- command .Stderr = os .Stderr
493-
494- if err := command .Run (); err != nil {
495- // Special handling for "nothing to commit" case
496- if cmd .args [0 ] == "commit" && err .Error () == "exit status 1" {
497- fmt .Println ("⚠️ Nothing to commit, skipping..." )
498- continue
499- }
500-
501- fmt .Println ("❌ Failed" )
502- return fmt .Errorf ("failed to execute %s: %v" , cmd .name , err )
503- }
504-
505- fmt .Println ("✅ Done" )
506- }
507-
508- return nil
459+ return ExecuteCommandBatch (commitPushCommands , "" )
509460}
0 commit comments