From 5187ad4431f953b5690701536c832dd85f9b85e3 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Mon, 23 Mar 2020 16:32:29 -0500 Subject: [PATCH] move preparecmd and Runnable to its own package --- command/issue_test.go | 14 +++++----- command/pr_checkout.go | 4 +-- command/pr_checkout_test.go | 20 +++++++------- command/pr_test.go | 22 ++++++++-------- command/repo.go | 11 ++++---- command/repo_test.go | 29 ++++++++++----------- command/testing.go | 8 +++--- git/git.go | 24 ++++++++--------- git/git_test.go | 6 ++--- git/remote.go | 6 ++--- utils/prepare_cmd.go => internal/run/run.go | 2 +- utils/utils.go | 3 ++- 12 files changed, 75 insertions(+), 74 deletions(-) rename utils/prepare_cmd.go => internal/run/run.go (99%) diff --git a/command/issue_test.go b/command/issue_test.go index 0d839d5a2..17a698a22 100644 --- a/command/issue_test.go +++ b/command/issue_test.go @@ -10,8 +10,8 @@ import ( "strings" "testing" + "github.com/cli/cli/internal/run" "github.com/cli/cli/test" - "github.com/cli/cli/utils" ) func TestIssueStatus(t *testing.T) { @@ -229,7 +229,7 @@ func TestIssueView(t *testing.T) { `)) var seenCmd *exec.Cmd - restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable { + restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable { seenCmd = cmd return &test.OutputStub{} }) @@ -263,7 +263,7 @@ func TestIssueView_numberArgWithHash(t *testing.T) { `)) var seenCmd *exec.Cmd - restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable { + restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable { seenCmd = cmd return &test.OutputStub{} }) @@ -386,7 +386,7 @@ func TestIssueView_notFound(t *testing.T) { `)) var seenCmd *exec.Cmd - restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable { + restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable { seenCmd = cmd return &test.OutputStub{} }) @@ -433,7 +433,7 @@ func TestIssueView_urlArg(t *testing.T) { `)) var seenCmd *exec.Cmd - restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable { + restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable { seenCmd = cmd return &test.OutputStub{} }) @@ -518,7 +518,7 @@ func TestIssueCreate_web(t *testing.T) { http.StubRepoResponse("OWNER", "REPO") var seenCmd *exec.Cmd - restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable { + restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable { seenCmd = cmd return &test.OutputStub{} }) @@ -544,7 +544,7 @@ func TestIssueCreate_webTitleBody(t *testing.T) { http.StubRepoResponse("OWNER", "REPO") var seenCmd *exec.Cmd - restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable { + restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable { seenCmd = cmd return &test.OutputStub{} }) diff --git a/command/pr_checkout.go b/command/pr_checkout.go index 08a37033d..bf7cedbb7 100644 --- a/command/pr_checkout.go +++ b/command/pr_checkout.go @@ -7,7 +7,7 @@ import ( "os/exec" "github.com/cli/cli/git" - "github.com/cli/cli/utils" + "github.com/cli/cli/internal/run" "github.com/spf13/cobra" ) @@ -91,7 +91,7 @@ func prCheckout(cmd *cobra.Command, args []string) error { cmd := exec.Command(args[0], args[1:]...) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr - if err := utils.PrepareCmd(cmd).Run(); err != nil { + if err := run.PrepareCmd(cmd).Run(); err != nil { return err } } diff --git a/command/pr_checkout_test.go b/command/pr_checkout_test.go index ce0266c5e..cc7eddd2b 100644 --- a/command/pr_checkout_test.go +++ b/command/pr_checkout_test.go @@ -7,8 +7,8 @@ import ( "testing" "github.com/cli/cli/context" + "github.com/cli/cli/internal/run" "github.com/cli/cli/test" - "github.com/cli/cli/utils" ) func TestPRCheckout_sameRepo(t *testing.T) { @@ -41,7 +41,7 @@ func TestPRCheckout_sameRepo(t *testing.T) { `)) ranCommands := [][]string{} - restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable { + restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable { switch strings.Join(cmd.Args, " ") { case "git show-ref --verify --quiet refs/heads/feature": return &errorStub{"exit status: 1"} @@ -93,7 +93,7 @@ func TestPRCheckout_urlArg(t *testing.T) { `)) ranCommands := [][]string{} - restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable { + restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable { switch strings.Join(cmd.Args, " ") { case "git show-ref --verify --quiet refs/heads/feature": return &errorStub{"exit status: 1"} @@ -142,7 +142,7 @@ func TestPRCheckout_branchArg(t *testing.T) { `)) ranCommands := [][]string{} - restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable { + restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable { switch strings.Join(cmd.Args, " ") { case "git show-ref --verify --quiet refs/heads/feature": return &errorStub{"exit status: 1"} @@ -191,7 +191,7 @@ func TestPRCheckout_existingBranch(t *testing.T) { `)) ranCommands := [][]string{} - restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable { + restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable { switch strings.Join(cmd.Args, " ") { case "git show-ref --verify --quiet refs/heads/feature": return &test.OutputStub{} @@ -243,7 +243,7 @@ func TestPRCheckout_differentRepo_remoteExists(t *testing.T) { `)) ranCommands := [][]string{} - restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable { + restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable { switch strings.Join(cmd.Args, " ") { case "git show-ref --verify --quiet refs/heads/feature": return &errorStub{"exit status: 1"} @@ -295,7 +295,7 @@ func TestPRCheckout_differentRepo(t *testing.T) { `)) ranCommands := [][]string{} - restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable { + restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable { switch strings.Join(cmd.Args, " ") { case "git config branch.feature.merge": return &errorStub{"exit status 1"} @@ -347,7 +347,7 @@ func TestPRCheckout_differentRepo_existingBranch(t *testing.T) { `)) ranCommands := [][]string{} - restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable { + restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable { switch strings.Join(cmd.Args, " ") { case "git config branch.feature.merge": return &test.OutputStub{[]byte("refs/heads/feature\n")} @@ -397,7 +397,7 @@ func TestPRCheckout_differentRepo_currentBranch(t *testing.T) { `)) ranCommands := [][]string{} - restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable { + restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable { switch strings.Join(cmd.Args, " ") { case "git config branch.feature.merge": return &test.OutputStub{[]byte("refs/heads/feature\n")} @@ -447,7 +447,7 @@ func TestPRCheckout_maintainerCanModify(t *testing.T) { `)) ranCommands := [][]string{} - restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable { + restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable { switch strings.Join(cmd.Args, " ") { case "git config branch.feature.merge": return &errorStub{"exit status 1"} diff --git a/command/pr_test.go b/command/pr_test.go index 4b9d0c6de..c47e28963 100644 --- a/command/pr_test.go +++ b/command/pr_test.go @@ -11,8 +11,8 @@ import ( "strings" "testing" + "github.com/cli/cli/internal/run" "github.com/cli/cli/test" - "github.com/cli/cli/utils" "github.com/google/shlex" "github.com/spf13/cobra" "github.com/spf13/pflag" @@ -108,7 +108,7 @@ func TestPRStatus_fork(t *testing.T) { defer jsonFile.Close() http.StubResponse(200, jsonFile) - defer utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable { + defer run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable { switch strings.Join(cmd.Args, " ") { case `git config --get-regexp ^branch\.blueberries\.(remote|merge)$`: return &test.OutputStub{[]byte(`branch.blueberries.remote origin @@ -439,7 +439,7 @@ func TestPRView_previewCurrentBranch(t *testing.T) { defer jsonFile.Close() http.StubResponse(200, jsonFile) - restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable { + restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable { return &test.OutputStub{} }) defer restoreCmd() @@ -474,7 +474,7 @@ func TestPRView_previewCurrentBranchWithEmptyBody(t *testing.T) { defer jsonFile.Close() http.StubResponse(200, jsonFile) - restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable { + restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable { return &test.OutputStub{} }) defer restoreCmd() @@ -509,7 +509,7 @@ func TestPRView_currentBranch(t *testing.T) { http.StubResponse(200, jsonFile) var seenCmd *exec.Cmd - restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable { + restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable { switch strings.Join(cmd.Args, " ") { case `git config --get-regexp ^branch\.blueberries\.(remote|merge)$`: return &test.OutputStub{} @@ -547,7 +547,7 @@ func TestPRView_noResultsForBranch(t *testing.T) { http.StubResponse(200, jsonFile) var seenCmd *exec.Cmd - restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable { + restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable { switch strings.Join(cmd.Args, " ") { case `git config --get-regexp ^branch\.blueberries\.(remote|merge)$`: return &test.OutputStub{} @@ -580,7 +580,7 @@ func TestPRView_numberArg(t *testing.T) { `)) var seenCmd *exec.Cmd - restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable { + restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable { seenCmd = cmd return &test.OutputStub{} }) @@ -612,7 +612,7 @@ func TestPRView_numberArgWithHash(t *testing.T) { `)) var seenCmd *exec.Cmd - restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable { + restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable { seenCmd = cmd return &test.OutputStub{} }) @@ -644,7 +644,7 @@ func TestPRView_urlArg(t *testing.T) { `)) var seenCmd *exec.Cmd - restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable { + restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable { seenCmd = cmd return &test.OutputStub{} }) @@ -678,7 +678,7 @@ func TestPRView_branchArg(t *testing.T) { `)) var seenCmd *exec.Cmd - restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable { + restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable { seenCmd = cmd return &test.OutputStub{} }) @@ -713,7 +713,7 @@ func TestPRView_branchWithOwnerArg(t *testing.T) { `)) var seenCmd *exec.Cmd - restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable { + restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable { seenCmd = cmd return &test.OutputStub{} }) diff --git a/command/repo.go b/command/repo.go index 1dc9b13a5..5ea27a2ce 100644 --- a/command/repo.go +++ b/command/repo.go @@ -12,6 +12,7 @@ import ( "github.com/cli/cli/api" "github.com/cli/cli/git" "github.com/cli/cli/internal/ghrepo" + "github.com/cli/cli/internal/run" "github.com/cli/cli/utils" "github.com/spf13/cobra" ) @@ -98,7 +99,7 @@ func repoClone(cmd *cobra.Command, args []string) error { cloneCmd.Stdin = os.Stdin cloneCmd.Stdout = os.Stdout cloneCmd.Stderr = os.Stderr - return utils.PrepareCmd(cloneCmd).Run() + return run.PrepareCmd(cloneCmd).Run() } func repoCreate(cmd *cobra.Command, args []string) error { @@ -198,7 +199,7 @@ func repoCreate(cmd *cobra.Command, args []string) error { remoteAdd := git.GitCommand("remote", "add", "origin", remoteURL) remoteAdd.Stdout = os.Stdout remoteAdd.Stderr = os.Stderr - err = utils.PrepareCmd(remoteAdd).Run() + err = run.PrepareCmd(remoteAdd).Run() if err != nil { return err } @@ -218,14 +219,14 @@ func repoCreate(cmd *cobra.Command, args []string) error { gitInit := git.GitCommand("init", path) gitInit.Stdout = os.Stdout gitInit.Stderr = os.Stderr - err = utils.PrepareCmd(gitInit).Run() + err = run.PrepareCmd(gitInit).Run() if err != nil { return err } gitRemoteAdd := git.GitCommand("-C", path, "remote", "add", "origin", remoteURL) gitRemoteAdd.Stdout = os.Stdout gitRemoteAdd.Stderr = os.Stderr - err = utils.PrepareCmd(gitRemoteAdd).Run() + err = run.PrepareCmd(gitRemoteAdd).Run() if err != nil { return err } @@ -364,7 +365,7 @@ func repoFork(cmd *cobra.Command, args []string) error { cloneCmd.Stdin = os.Stdin cloneCmd.Stdout = os.Stdout cloneCmd.Stderr = os.Stderr - err = utils.PrepareCmd(cloneCmd).Run() + err = run.PrepareCmd(cloneCmd).Run() if err != nil { return fmt.Errorf("failed to clone fork: %w", err) } diff --git a/command/repo_test.go b/command/repo_test.go index 6001be817..d3b035d18 100644 --- a/command/repo_test.go +++ b/command/repo_test.go @@ -11,8 +11,8 @@ import ( "time" "github.com/cli/cli/context" + "github.com/cli/cli/internal/run" "github.com/cli/cli/test" - "github.com/cli/cli/utils" ) func TestRepoFork_already_forked(t *testing.T) { @@ -116,7 +116,7 @@ func TestRepoFork_in_parent_yes(t *testing.T) { defer http.StubWithFixture(200, "forkResult.json")() var seenCmds []*exec.Cmd - defer utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable { + defer run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable { seenCmds = append(seenCmds, cmd) return &test.OutputStub{} })() @@ -155,7 +155,7 @@ func TestRepoFork_outside_yes(t *testing.T) { defer http.StubWithFixture(200, "forkResult.json")() var seenCmd *exec.Cmd - defer utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable { + defer run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable { seenCmd = cmd return &test.OutputStub{} })() @@ -187,7 +187,7 @@ func TestRepoFork_outside_survey_yes(t *testing.T) { defer http.StubWithFixture(200, "forkResult.json")() var seenCmd *exec.Cmd - defer utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable { + defer run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable { seenCmd = cmd return &test.OutputStub{} })() @@ -226,7 +226,7 @@ func TestRepoFork_outside_survey_no(t *testing.T) { defer http.StubWithFixture(200, "forkResult.json")() cmdRun := false - defer utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable { + defer run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable { cmdRun = true return &test.OutputStub{} })() @@ -262,7 +262,7 @@ func TestRepoFork_in_parent_survey_yes(t *testing.T) { defer http.StubWithFixture(200, "forkResult.json")() var seenCmds []*exec.Cmd - defer utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable { + defer run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable { seenCmds = append(seenCmds, cmd) return &test.OutputStub{} })() @@ -310,7 +310,7 @@ func TestRepoFork_in_parent_survey_no(t *testing.T) { defer http.StubWithFixture(200, "forkResult.json")() cmdRun := false - defer utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable { + defer run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable { cmdRun = true return &test.OutputStub{} })() @@ -368,7 +368,7 @@ func TestRepoClone(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { var seenCmd *exec.Cmd - restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable { + restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable { seenCmd = cmd return &test.OutputStub{} }) @@ -408,7 +408,7 @@ func TestRepoCreate(t *testing.T) { `)) var seenCmd *exec.Cmd - restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable { + restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable { seenCmd = cmd return &test.OutputStub{} }) @@ -473,7 +473,7 @@ func TestRepoCreate_org(t *testing.T) { `)) var seenCmd *exec.Cmd - restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable { + restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable { seenCmd = cmd return &test.OutputStub{} }) @@ -538,7 +538,7 @@ func TestRepoCreate_orgWithTeam(t *testing.T) { `)) var seenCmd *exec.Cmd - restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable { + restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable { seenCmd = cmd return &test.OutputStub{} }) @@ -580,7 +580,6 @@ func TestRepoCreate_orgWithTeam(t *testing.T) { } } - func TestRepoView(t *testing.T) { initBlankContext("OWNER/REPO", "master") http := initFakeHTTP() @@ -590,7 +589,7 @@ func TestRepoView(t *testing.T) { `)) var seenCmd *exec.Cmd - restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable { + restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable { seenCmd = cmd return &test.OutputStub{} }) @@ -623,7 +622,7 @@ func TestRepoView_ownerRepo(t *testing.T) { `)) var seenCmd *exec.Cmd - restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable { + restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable { seenCmd = cmd return &test.OutputStub{} }) @@ -655,7 +654,7 @@ func TestRepoView_fullURL(t *testing.T) { { } `)) var seenCmd *exec.Cmd - restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable { + restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable { seenCmd = cmd return &test.OutputStub{} }) diff --git a/command/testing.go b/command/testing.go index 0221ce8cf..6d1a2594e 100644 --- a/command/testing.go +++ b/command/testing.go @@ -11,8 +11,8 @@ import ( "github.com/cli/cli/api" "github.com/cli/cli/context" + "github.com/cli/cli/internal/run" "github.com/cli/cli/test" - "github.com/cli/cli/utils" ) // TODO this is split between here and test/helpers.go. I did that because otherwise our test @@ -27,7 +27,7 @@ type CmdStubber struct { func initCmdStubber() (*CmdStubber, func()) { cs := CmdStubber{} - teardown := utils.SetPrepareCmd(createStubbedPrepareCmd(&cs)) + teardown := run.SetPrepareCmd(createStubbedPrepareCmd(&cs)) return &cs, teardown } @@ -36,8 +36,8 @@ func (cs *CmdStubber) Stub(desiredOutput string) { cs.Stubs = append(cs.Stubs, &test.OutputStub{[]byte(desiredOutput)}) } -func createStubbedPrepareCmd(cs *CmdStubber) func(*exec.Cmd) utils.Runnable { - return func(cmd *exec.Cmd) utils.Runnable { +func createStubbedPrepareCmd(cs *CmdStubber) func(*exec.Cmd) run.Runnable { + return func(cmd *exec.Cmd) run.Runnable { cs.Calls = append(cs.Calls, cmd) call := cs.Count cs.Count += 1 diff --git a/git/git.go b/git/git.go index cdf1e869f..b604c398a 100644 --- a/git/git.go +++ b/git/git.go @@ -10,18 +10,18 @@ import ( "regexp" "strings" - "github.com/cli/cli/utils" + "github.com/cli/cli/internal/run" ) func VerifyRef(ref string) bool { showRef := exec.Command("git", "show-ref", "--verify", "--quiet", ref) - err := utils.PrepareCmd(showRef).Run() + err := run.PrepareCmd(showRef).Run() return err == nil } // CurrentBranch reads the checked-out branch for the git repository func CurrentBranch() (string, error) { - err := utils.PrepareCmd(GitCommand("log")).Run() + err := run.PrepareCmd(GitCommand("log")).Run() if err != nil { // this is a hack. errRe := regexp.MustCompile("your current branch '([^']+)' does not have any commits yet") @@ -32,7 +32,7 @@ func CurrentBranch() (string, error) { } // we avoid using `git branch --show-current` for compatibility with git < 2.22 branchCmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD") - output, err := utils.PrepareCmd(branchCmd).Output() + output, err := run.PrepareCmd(branchCmd).Output() branchName := firstLine(output) if err == nil && branchName == "HEAD" { return "", errors.New("git: not on any branch") @@ -42,13 +42,13 @@ func CurrentBranch() (string, error) { func listRemotes() ([]string, error) { remoteCmd := exec.Command("git", "remote", "-v") - output, err := utils.PrepareCmd(remoteCmd).Output() + output, err := run.PrepareCmd(remoteCmd).Output() return outputLines(output), err } func Config(name string) (string, error) { configCmd := exec.Command("git", "config", name) - output, err := utils.PrepareCmd(configCmd).Output() + output, err := run.PrepareCmd(configCmd).Output() if err != nil { return "", fmt.Errorf("unknown config key: %s", name) } @@ -63,7 +63,7 @@ var GitCommand = func(args ...string) *exec.Cmd { func UncommittedChangeCount() (int, error) { statusCmd := GitCommand("status", "--porcelain") - output, err := utils.PrepareCmd(statusCmd).Output() + output, err := run.PrepareCmd(statusCmd).Output() if err != nil { return 0, err } @@ -90,7 +90,7 @@ func Commits(baseRef, headRef string) ([]*Commit, error) { "-c", "log.ShowSignature=false", "log", "--pretty=format:%H,%s", "--cherry", fmt.Sprintf("%s...%s", baseRef, headRef)) - output, err := utils.PrepareCmd(logCmd).Output() + output, err := run.PrepareCmd(logCmd).Output() if err != nil { return []*Commit{}, err } @@ -118,7 +118,7 @@ func Commits(baseRef, headRef string) ([]*Commit, error) { func CommitBody(sha string) (string, error) { showCmd := GitCommand("-c", "log.ShowSignature=false", "show", "-s", "--pretty=format:%b", sha) - output, err := utils.PrepareCmd(showCmd).Output() + output, err := run.PrepareCmd(showCmd).Output() if err != nil { return "", err } @@ -130,7 +130,7 @@ func Push(remote string, ref string) error { pushCmd := GitCommand("push", "--set-upstream", remote, ref) pushCmd.Stdout = os.Stdout pushCmd.Stderr = os.Stderr - return utils.PrepareCmd(pushCmd).Run() + return run.PrepareCmd(pushCmd).Run() } type BranchConfig struct { @@ -143,7 +143,7 @@ type BranchConfig struct { func ReadBranchConfig(branch string) (cfg BranchConfig) { prefix := regexp.QuoteMeta(fmt.Sprintf("branch.%s.", branch)) configCmd := GitCommand("config", "--get-regexp", fmt.Sprintf("^%s(remote|merge)$", prefix)) - output, err := utils.PrepareCmd(configCmd).Output() + output, err := run.PrepareCmd(configCmd).Output() if err != nil { return } @@ -174,7 +174,7 @@ func ReadBranchConfig(branch string) (cfg BranchConfig) { // ToplevelDir returns the top-level directory path of the current repository func ToplevelDir() (string, error) { showCmd := exec.Command("git", "rev-parse", "--show-toplevel") - output, err := utils.PrepareCmd(showCmd).Output() + output, err := run.PrepareCmd(showCmd).Output() return firstLine(output), err } diff --git a/git/git_test.go b/git/git_test.go index 9e3f9a333..d52902dbe 100644 --- a/git/git_test.go +++ b/git/git_test.go @@ -4,8 +4,8 @@ import ( "os/exec" "testing" + "github.com/cli/cli/internal/run" "github.com/cli/cli/test" - "github.com/cli/cli/utils" ) func Test_UncommittedChangeCount(t *testing.T) { @@ -20,13 +20,13 @@ func Test_UncommittedChangeCount(t *testing.T) { c{Label: "untracked file", Expected: 2, Output: " M poem.txt\n?? new.txt"}, } - teardown := utils.SetPrepareCmd(func(*exec.Cmd) utils.Runnable { + teardown := run.SetPrepareCmd(func(*exec.Cmd) run.Runnable { return &test.OutputStub{} }) defer teardown() for _, v := range cases { - _ = utils.SetPrepareCmd(func(*exec.Cmd) utils.Runnable { + _ = run.SetPrepareCmd(func(*exec.Cmd) run.Runnable { return &test.OutputStub{[]byte(v.Output)} }) ucc, _ := UncommittedChangeCount() diff --git a/git/remote.go b/git/remote.go index f98606a2e..bfecfd3b0 100644 --- a/git/remote.go +++ b/git/remote.go @@ -6,7 +6,7 @@ import ( "regexp" "strings" - "github.com/cli/cli/utils" + "github.com/cli/cli/internal/run" ) var remoteRE = regexp.MustCompile(`(.+)\s+(.+)\s+\((push|fetch)\)`) @@ -76,7 +76,7 @@ func parseRemotes(gitRemotes []string) (remotes RemoteSet) { // after the fetch. func AddRemote(name, initURL, finalURL string) (*Remote, error) { addCmd := exec.Command("git", "remote", "add", "-f", name, initURL) - err := utils.PrepareCmd(addCmd).Run() + err := run.PrepareCmd(addCmd).Run() if err != nil { return nil, err } @@ -85,7 +85,7 @@ func AddRemote(name, initURL, finalURL string) (*Remote, error) { finalURL = initURL } else { setCmd := exec.Command("git", "remote", "set-url", name, finalURL) - err := utils.PrepareCmd(setCmd).Run() + err := run.PrepareCmd(setCmd).Run() if err != nil { return nil, err } diff --git a/utils/prepare_cmd.go b/internal/run/run.go similarity index 99% rename from utils/prepare_cmd.go rename to internal/run/run.go index 6b354cb80..67de76fa2 100644 --- a/utils/prepare_cmd.go +++ b/internal/run/run.go @@ -1,4 +1,4 @@ -package utils +package run import ( "bytes" diff --git a/utils/utils.go b/utils/utils.go index 6217871b7..1f7fd7106 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -7,6 +7,7 @@ import ( "github.com/briandowns/spinner" "github.com/charmbracelet/glamour" + "github.com/cli/cli/internal/run" "github.com/cli/cli/pkg/browser" ) @@ -16,7 +17,7 @@ func OpenInBrowser(url string) error { if err != nil { return err } - return PrepareCmd(browseCmd).Run() + return run.PrepareCmd(browseCmd).Run() } func RenderMarkdown(text string) (string, error) {