optional to hide outputs.

This commit is contained in:
Daveberry committed 2026-06-10 01:08:12 +08:00
1 parent 61ac03e7b4
commit 70144e6ee0
8 files changed
+20 -14

No files matched your search

+4 -4
View File
@@ -18,7 +18,7 @@ var commitCmd = &cobra.Command{
Args: cobra.RangeArgs(0, 3), Args: cobra.RangeArgs(0, 3),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 { if len(args) == 0 {
return utils.RunGit("status", "--short") return utils.RunGitSequence(false, []string{"status", "--short"})
} }
if len(args) < 2 { if len(args) < 2 {
@@ -37,18 +37,18 @@ var commitCmd = &cobra.Command{
} }
fmt.Println("Edited files:") fmt.Println("Edited files:")
if err := utils.RunGit("status", "--short"); err != nil { if err := utils.RunGitSequence(false, []string{"status", "--short"}); err != nil {
return err return err
} }
fmt.Println() fmt.Println()
utils.RunGitSequence(addArgs) utils.RunGitSequence(false, addArgs)
commitArgs := []string{"commit", "-m", args[1]} commitArgs := []string{"commit", "-m", args[1]}
if len(args) == 3 { if len(args) == 3 {
commitArgs = append(commitArgs, "-m", args[2]) commitArgs = append(commitArgs, "-m", args[2])
} }
utils.RunGitSequence(commitArgs) utils.RunGitSequence(false, commitArgs)
return nil return nil
}, },
} }
+1 -1
View File
@@ -24,7 +24,7 @@ var createBranchCmd = &cobra.Command{
Short: "Create and switch to a branch", Short: "Create and switch to a branch",
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
return utils.RunGitSequence([]string{"switch", "-c", args[0]}) return utils.RunGitSequence(false, []string{"switch", "-c", args[0]})
}, },
} }
+1 -1
View File
@@ -14,6 +14,6 @@ var diffCmd = &cobra.Command{
Short: "Show working tree changes", Short: "Show working tree changes",
Args: cobra.NoArgs, Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
return utils.RunGitSequence([]string{"diff"}) return utils.RunGitSequence(false, []string{"diff"})
}, },
} }
+1 -1
View File
@@ -14,7 +14,7 @@ var pullCmd = &cobra.Command{
Short: "Fetch and pull updates", Short: "Fetch and pull updates",
Args: cobra.NoArgs, Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
return utils.RunGitSequence( return utils.RunGitSequence(false,
[]string{"fetch"}, []string{"fetch"},
[]string{"pull"}, []string{"pull"},
) )
+1 -1
View File
@@ -14,6 +14,6 @@ var pushCmd = &cobra.Command{
Short: "Add current changes and push", Short: "Add current changes and push",
Args: cobra.NoArgs, Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
return utils.RunGitSequence([]string{"push"}) return utils.RunGitSequence(false, []string{"push"})
}, },
} }
+1 -1
View File
@@ -14,6 +14,6 @@ var stashCmd = &cobra.Command{
Short: "Stash current changes", Short: "Stash current changes",
Args: cobra.NoArgs, Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
return utils.RunGitSequence([]string{"stash"}) return utils.RunGitSequence(false, []string{"stash"})
}, },
} }
+2 -2
View File
@@ -44,7 +44,7 @@ func performAccountSwitch(username string) error {
return err return err
} }
if err := utils.RunGitSequence( if err := utils.RunGitSequence(false,
[]string{"config", "--global", "user.name", account.Username}, []string{"config", "--global", "user.name", account.Username},
[]string{"config", "--global", "user.email", account.Email}, []string{"config", "--global", "user.email", account.Email},
); err != nil { ); err != nil {
@@ -66,5 +66,5 @@ func performAccountSwitch(username string) error {
} }
func performBranchSwitch(branchName string) error { func performBranchSwitch(branchName string) error {
return utils.RunGitSequence([]string{"switch", branchName}) return utils.RunGitSequence(false, []string{"switch", branchName})
} }
+9 -3
View File
@@ -2,23 +2,29 @@ package utils
import ( import (
"fmt" "fmt"
"io"
"os" "os"
"os/exec" "os/exec"
"strings" "strings"
) )
func RunGit(args ...string) error { func RunGit(hideOutput bool, args ...string) error {
gitCmd := exec.Command("git", args...) gitCmd := exec.Command("git", args...)
gitCmd.Stdin = os.Stdin gitCmd.Stdin = os.Stdin
if hideOutput {
gitCmd.Stdout = io.Discard
gitCmd.Stderr = io.Discard
} else {
gitCmd.Stdout = os.Stdout gitCmd.Stdout = os.Stdout
gitCmd.Stderr = os.Stderr gitCmd.Stderr = os.Stderr
}
return gitCmd.Run() return gitCmd.Run()
} }
func RunGitSequence(commands ...[]string) error { func RunGitSequence(hideOutput bool, commands ...[]string) error {
for _, commandArgs := range commands { for _, commandArgs := range commands {
if err := RunGit(commandArgs...); err != nil { if err := RunGit(hideOutput, commandArgs...); err != nil {
return fmt.Errorf("git %s failed: %w", strings.Join(commandArgs, " "), err) return fmt.Errorf("git %s failed: %w", strings.Join(commandArgs, " "), err)
} }
} }