From 20962c942536bc5b008ec98692d2dfcc1d5dfded Mon Sep 17 00:00:00 2001 From: dan9186 Date: Tue, 23 Sep 2025 16:37:28 -0700 Subject: [PATCH] implementing delete branches --- client/clienter.go | 2 +- client/repos_branch.go | 68 +++++++++++++++++++++++++++++++++++++ client/testclient/client.go | 4 +-- cmd/branch.go | 33 +++++++----------- 4 files changed, 84 insertions(+), 23 deletions(-) diff --git a/client/clienter.go b/client/clienter.go index 4615bc2..c2bb74d 100644 --- a/client/clienter.go +++ b/client/clienter.go @@ -13,7 +13,7 @@ type Clienter interface { GetDirs(ctx context.Context, dir string) ([]string, error) GetLogins(ctx context.Context) ([]string, error) GetRepos(ctx context.Context, name string) ([]*github.Repository, error) - ListBranches(ctx context.Context, repoDirs []string, args ...string) error + Branches(ctx context.Context, repoDirs []string, args ...string) error ListTags(ctx context.Context, repoDirs []string, args ...string) error PullRepos(ctx context.Context, repoDirs []string, args ...string) error PushRepos(ctx context.Context, repoDirs []string, args ...string) error diff --git a/client/repos_branch.go b/client/repos_branch.go index 8654a8e..4637256 100644 --- a/client/repos_branch.go +++ b/client/repos_branch.go @@ -3,8 +3,11 @@ package client import ( "bytes" "context" + "fmt" "os/exec" "strings" + + "github.com/gosuri/uiprogress" ) func (c *Client) ListBranches(ctx context.Context, dirs []string, args ...string) error { @@ -41,3 +44,68 @@ func (c *Client) ListBranches(ctx context.Context, dirs []string, args ...string return nil } + +func (c *Client) Branches(ctx context.Context, dirs []string, args ...string) error { + count := len(dirs) + args = append([]string{"branch"}, args...) + + verbose := Verbose(ctx) + + var bar *uiprogress.Bar + currRepo := "" + + if verbose { + c.scrb.BeginDescribe("Command") + defer c.scrb.EndDescribe() + + c.scrb.Print(fmt.Sprintf("git %s", strings.Join(args, " "))) + + c.scrb.BeginDescribe("directories") + defer c.scrb.EndDescribe() + } else { + bar = uiprogress.AddBar(count). + AppendCompleted(). + PrependElapsed(). + PrependFunc(func(b *uiprogress.Bar) string { + return fmt.Sprintf("Branches (%d/%d)", b.Current(), count) + }). + AppendFunc(func(b *uiprogress.Bar) string { + return currRepo + }) + } + + for _, dir := range dirs { + currRepo = fmt.Sprintf("\nCurrent Repo: %v", dir) + + out := &bytes.Buffer{} + errout := &bytes.Buffer{} + + cmd := exec.CommandContext(ctx, "git", args...) + cmd.Stdout = out + cmd.Stderr = errout + cmd.Dir = dir + + err := cmd.Run() + if err != nil && !verbose { + return fmt.Errorf("branch: %w", err) // TODO: collect errors and return them all + } + + if verbose { + c.scrb.BeginDescribe(dir) + if err != nil { + c.scrb.Error(err) + c.scrb.PrintLines(errout) + } else { + c.scrb.PrintLines(out) + } + + c.scrb.EndDescribe() + } else { + bar.Incr() + } + } + + currRepo = "" + + return nil +} diff --git a/client/testclient/client.go b/client/testclient/client.go index 0f06009..9fcf52d 100644 --- a/client/testclient/client.go +++ b/client/testclient/client.go @@ -45,8 +45,8 @@ func (c *TestClient) GetRepos(ctx context.Context, name string) ([]*github.Repos return nil, nil } -func (c *TestClient) ListBranches(ctx context.Context, repoDirs []string, args ...string) error { - c.CommandsCalled = append(c.CommandsCalled, "ListBranches") +func (c *TestClient) Branches(ctx context.Context, repoDirs []string, args ...string) error { + c.CommandsCalled = append(c.CommandsCalled, "Branches") return nil } diff --git a/cmd/branch.go b/cmd/branch.go index 2c6c5df..6d0333c 100644 --- a/cmd/branch.go +++ b/cmd/branch.go @@ -20,10 +20,10 @@ func init() { branchCmd.Flags().BoolVarP(&all, "all", "a", false, "list all branches") branchCmd.Flags().BoolVarP(&del, "delete", "d", false, "delete the branch from the repos") - branchCmd.Flags().BoolVar(&delForce, "D", false, "force delete the branch from the repos") + branchCmd.Flags().BoolVarP(&delForce, "force-delete", "D", false, "force delete the branch from the repos") branchCmd.Flags().BoolVarP(&force, "force", "f", false, "force the desired action") - branchCmd.MarkFlagsMutuallyExclusive("all", "delete", "D") + branchCmd.MarkFlagsMutuallyExclusive("all", "delete", "force-delete") } var branchCmd = &cobra.Command{ @@ -65,34 +65,27 @@ func branchFunc(cmd *cobra.Command, args []string) error { args = append(args, name) - return deleteBranch(ctx, cmd, args, repoDirs) + err := clt.Branches(ctx, repoDirs, args...) + if err != nil { + cmd.SilenceUsage = true + return fmt.Errorf("delete: %w", err) + } + + return nil } if all { args = append(args, "--all") } - return listBranches(ctx, cmd, args, repoDirs) -} + // This must be verbose to show anything + ctx = client.WithVerbose(ctx, true) -func listBranches(ctx context.Context, cmd *cobra.Command, args []string, repoDirs []string) error { - err := clt.ListBranches(ctx, repoDirs, args...) + err = clt.Branches(ctx, repoDirs, args...) if err != nil { cmd.SilenceUsage = true - return fmt.Errorf("list branches: %w", err) + return fmt.Errorf("list: %w", err) } return nil } - -func deleteBranch(ctx context.Context, cmd *cobra.Command, args []string, repoDirs []string) error { - /* - err := clt.DeleteBranch(ctx, repoDirs, args...) - if err != nil { - cmd.SilenceUsage = true - return fmt.Errorf("delete branch: %w", err) - } - */ - - return nil -}