diff --git a/internal/command/command.go b/internal/command/command.go index 84f70b2e26..57b292ae83 100644 --- a/internal/command/command.go +++ b/internal/command/command.go @@ -24,6 +24,14 @@ import ( "os/exec" ) +const ( + // Cargo is the command name for the cargo executable. + Cargo = "cargo" + + // Git is the command name for the git executable. + Git = "git" +) + var ( // Verbose controls whether commands are printed to stderr before execution. // diff --git a/internal/git/git_test.go b/internal/git/git_test.go index 6504199963..0546e44e1f 100644 --- a/internal/git/git_test.go +++ b/internal/git/git_test.go @@ -36,7 +36,7 @@ func TestGetLastTag(t *testing.T) { const wantTag = "v1.2.3" remoteDir := testhelper.SetupRepoWithChange(t, wantTag) testhelper.CloneRepository(t, remoteDir) - got, err := GetLastTag(t.Context(), command.GetExecutablePath(nil, "git"), config.RemoteUpstream, config.BranchMain) + got, err := GetLastTag(t.Context(), command.GetExecutablePath(nil, command.Git), config.RemoteUpstream, config.BranchMain) if err != nil { t.Fatal(err) } @@ -47,7 +47,7 @@ func TestGetLastTag(t *testing.T) { func TestLastTagGitError(t *testing.T) { t.Chdir(t.TempDir()) - _, err := GetLastTag(t.Context(), command.GetExecutablePath(nil, "git"), config.RemoteUpstream, config.BranchMain) + _, err := GetLastTag(t.Context(), command.GetExecutablePath(nil, command.Git), config.RemoteUpstream, config.BranchMain) if err == nil { t.Fatal("expected an error but got none") } @@ -59,7 +59,7 @@ func TestLastTagGitError(t *testing.T) { func TestIsNewFileSuccess(t *testing.T) { testhelper.SetupForVersionBump(t, "dummy-tag") // Get the HEAD commit hash, which serves as a unique reference for this test. - cmd := exec.CommandContext(t.Context(), "git", "rev-parse", "HEAD") + cmd := exec.CommandContext(t.Context(), command.Git, "rev-parse", "HEAD") out, err := cmd.Output() if err != nil { t.Fatal(err) @@ -70,7 +70,7 @@ func TestIsNewFileSuccess(t *testing.T) { t.Fatal(err) } cfg := &config.Release{} - gitExe := command.GetExecutablePath(cfg.Preinstalled, "git") + gitExe := command.GetExecutablePath(cfg.Preinstalled, command.Git) newName := path.Join("src", "storage", "src", "new.rs") if err := os.MkdirAll(path.Dir(newName), 0755); err != nil { @@ -94,7 +94,7 @@ func TestIsNewFileDiffError(t *testing.T) { t.Chdir(t.TempDir()) testhelper.SetupForVersionBump(t, wantTag) cfg := &config.Release{} - gitExe := command.GetExecutablePath(cfg.Preinstalled, "git") + gitExe := command.GetExecutablePath(cfg.Preinstalled, command.Git) existingName := path.Join("src", "storage", "src", "lib.rs") if IsNewFile(t.Context(), gitExe, "invalid-tag", existingName) { t.Errorf("diff errors should return false for isNewFile(): %s", existingName) @@ -106,7 +106,7 @@ func TestFilesChangedSuccess(t *testing.T) { remoteDir := testhelper.SetupRepoWithChange(t, wantTag) testhelper.CloneRepository(t, remoteDir) - got, err := FilesChangedSince(t.Context(), command.GetExecutablePath(nil, "git"), wantTag, nil) + got, err := FilesChangedSince(t.Context(), command.GetExecutablePath(nil, command.Git), wantTag, nil) if err != nil { t.Fatal(err) } @@ -120,7 +120,7 @@ func TestFilesBadRef(t *testing.T) { const wantTag = "release-2002-03-04" remoteDir := testhelper.SetupRepoWithChange(t, wantTag) testhelper.CloneRepository(t, remoteDir) - if got, err := FilesChangedSince(t.Context(), command.GetExecutablePath(nil, "git"), "--invalid--", nil); err == nil { + if got, err := FilesChangedSince(t.Context(), command.GetExecutablePath(nil, command.Git), "--invalid--", nil); err == nil { t.Errorf("expected an error with invalid tag, got=%v", got) } } @@ -229,7 +229,7 @@ func TestAssertGitStatusClean(t *testing.T) { tmpDir := t.TempDir() t.Chdir(tmpDir) test.setup(t) - err := AssertGitStatusClean(t.Context(), command.GetExecutablePath(cfg.Preinstalled, "git")) + err := AssertGitStatusClean(t.Context(), command.GetExecutablePath(cfg.Preinstalled, command.Git)) if !errors.Is(err, test.wantErr) { t.Errorf("AssertGitStatusClean() error = %v, wantErr %v", err, test.wantErr) } @@ -238,41 +238,41 @@ func TestAssertGitStatusClean(t *testing.T) { } func TestMatchesBranchPointSuccess(t *testing.T) { - testhelper.RequireCommand(t, "git") + testhelper.RequireCommand(t, command.Git) remoteDir := testhelper.SetupRepoWithChange(t, "v1.0.0") testhelper.CloneRepository(t, remoteDir) - if err := MatchesBranchPoint(t.Context(), "git", config.RemoteUpstream, config.BranchMain); err != nil { + if err := MatchesBranchPoint(t.Context(), command.Git, config.RemoteUpstream, config.BranchMain); err != nil { t.Fatal(err) } } func TestMatchesBranchDiffError(t *testing.T) { - testhelper.RequireCommand(t, "git") + testhelper.RequireCommand(t, command.Git) remoteDir := testhelper.SetupRepoWithChange(t, "v1.0.0") testhelper.CloneRepository(t, remoteDir) - if err := MatchesBranchPoint(t.Context(), "git", config.RemoteUpstream, "not-a-valid-branch"); err == nil { + if err := MatchesBranchPoint(t.Context(), command.Git, config.RemoteUpstream, "not-a-valid-branch"); err == nil { t.Errorf("expected an error with an invalid branch") } } func TestMatchesDirtyCloneError(t *testing.T) { - testhelper.RequireCommand(t, "git") + testhelper.RequireCommand(t, command.Git) remoteDir := testhelper.SetupRepoWithChange(t, "v1.0.0") testhelper.CloneRepository(t, remoteDir) testhelper.AddCrate(t, path.Join("src", "pubsub"), "google-cloud-pubsub") testhelper.RunGit(t, "add", path.Join("src", "pubsub")) testhelper.RunGit(t, "commit", "-m", "feat: created pubsub", ".") - if err := MatchesBranchPoint(t.Context(), "git", config.RemoteUpstream, "not-a-valid-branch"); err == nil { + if err := MatchesBranchPoint(t.Context(), command.Git, config.RemoteUpstream, "not-a-valid-branch"); err == nil { t.Errorf("expected an error with a dirty clone") } } func TestShowFileAtRemoteBranch(t *testing.T) { - testhelper.RequireCommand(t, "git") + testhelper.RequireCommand(t, command.Git) remoteDir := testhelper.SetupRepo(t) testhelper.CloneRepository(t, remoteDir) - got, err := ShowFileAtRemoteBranch(t.Context(), "git", config.RemoteUpstream, config.BranchMain, testhelper.ReadmeFile) + got, err := ShowFileAtRemoteBranch(t.Context(), command.Git, config.RemoteUpstream, config.BranchMain, testhelper.ReadmeFile) if err != nil { t.Fatal(err) } @@ -282,10 +282,10 @@ func TestShowFileAtRemoteBranch(t *testing.T) { } func TestShowFileAtRemoteBranch_Error(t *testing.T) { - testhelper.RequireCommand(t, "git") + testhelper.RequireCommand(t, command.Git) remoteDir := testhelper.SetupRepo(t) testhelper.CloneRepository(t, remoteDir) - _, err := ShowFileAtRemoteBranch(t.Context(), "git", config.RemoteUpstream, config.BranchMain, "does_not_exist") + _, err := ShowFileAtRemoteBranch(t.Context(), command.Git, config.RemoteUpstream, config.BranchMain, "does_not_exist") if err == nil { t.Fatal("expected an error showing file that should not exist") } @@ -295,7 +295,7 @@ func TestShowFileAtRemoteBranch_Error(t *testing.T) { } func TestShowFileAtRevision(t *testing.T) { - testhelper.RequireCommand(t, "git") + testhelper.RequireCommand(t, command.Git) opts := testhelper.SetupOptions{ WithChanges: []string{testhelper.ReadmeFile}, } @@ -324,7 +324,7 @@ func TestShowFileAtRevision(t *testing.T) { }, } { t.Run(test.name, func(t *testing.T) { - got, err := ShowFileAtRevision(t.Context(), "git", test.revision, testhelper.ReadmeFile) + got, err := ShowFileAtRevision(t.Context(), command.Git, test.revision, testhelper.ReadmeFile) if err != nil { t.Fatal(err) } @@ -336,9 +336,9 @@ func TestShowFileAtRevision(t *testing.T) { } func TestShowFileAtRevision_Error(t *testing.T) { - testhelper.RequireCommand(t, "git") + testhelper.RequireCommand(t, command.Git) testhelper.SetupRepo(t) - _, err := ShowFileAtRevision(t.Context(), "git", "HEAD", "does_not_exist") + _, err := ShowFileAtRevision(t.Context(), command.Git, "HEAD", "does_not_exist") if err == nil { t.Fatal("expected an error showing file that should not exist") } @@ -349,9 +349,9 @@ func TestShowFileAtRevision_Error(t *testing.T) { func TestCheckVersion(t *testing.T) { t.Parallel() - testhelper.RequireCommand(t, "git") + testhelper.RequireCommand(t, command.Git) - if err := CheckVersion(t.Context(), "git"); err != nil { + if err := CheckVersion(t.Context(), command.Git); err != nil { t.Fatal(err) } } @@ -364,27 +364,27 @@ func TestCheckVersion_Error(t *testing.T) { } func TestCheckRemoteURL(t *testing.T) { - testhelper.RequireCommand(t, "git") + testhelper.RequireCommand(t, command.Git) remoteDir := testhelper.SetupRepo(t) testhelper.CloneRepository(t, remoteDir) - if err := CheckRemoteURL(t.Context(), "git", testhelper.TestRemote); err != nil { + if err := CheckRemoteURL(t.Context(), command.Git, testhelper.TestRemote); err != nil { t.Fatal(err) } } func TestCheckRemoteURL_Error(t *testing.T) { - testhelper.RequireCommand(t, "git") + testhelper.RequireCommand(t, command.Git) remoteDir := testhelper.SetupRepo(t) testhelper.CloneRepository(t, remoteDir) - if err := CheckRemoteURL(t.Context(), "git", "remote_that_does_not_exist"); err == nil { + if err := CheckRemoteURL(t.Context(), command.Git, "remote_that_does_not_exist"); err == nil { t.Errorf("expected an error checking for a remote URL, but did not get one") } } func TestFindCommitsForPath(t *testing.T) { - testhelper.RequireCommand(t, "git") + testhelper.RequireCommand(t, command.Git) opts := testhelper.SetupOptions{ WithChanges: []string{testhelper.ReadmeFile}, } @@ -406,7 +406,7 @@ func TestFindCommitsForPath(t *testing.T) { }, } { t.Run(test.name, func(t *testing.T) { - got, err := FindCommitsForPath(t.Context(), "git", test.path) + got, err := FindCommitsForPath(t.Context(), command.Git, test.path) if err != nil { t.Fatal(err) } @@ -424,21 +424,21 @@ func TestFindCommitsForPath(t *testing.T) { } func TestFindCommitsForPath_Error(t *testing.T) { - testhelper.RequireCommand(t, "git") + testhelper.RequireCommand(t, command.Git) testhelper.SetupRepo(t) // It's invalid to try to get the log for a path outside the repo - if _, err := FindCommitsForPath(t.Context(), "git", ".."); err == nil { + if _, err := FindCommitsForPath(t.Context(), command.Git, ".."); err == nil { t.Errorf("expected an error finding commits for path outside the repo, but did not get one") } } func TestCheckout(t *testing.T) { - testhelper.RequireCommand(t, "git") + testhelper.RequireCommand(t, command.Git) opts := testhelper.SetupOptions{ WithChanges: []string{testhelper.ReadmeFile}, } testhelper.Setup(t, opts) - if err := Checkout(t.Context(), "git", "HEAD~"); err != nil { + if err := Checkout(t.Context(), command.Git, "HEAD~"); err != nil { t.Fatal(err) } readmeContent, err := os.ReadFile(testhelper.ReadmeFile) @@ -451,30 +451,30 @@ func TestCheckout(t *testing.T) { } func TestCheckout_Error(t *testing.T) { - testhelper.RequireCommand(t, "git") + testhelper.RequireCommand(t, command.Git) testhelper.SetupRepo(t) - err := Checkout(t.Context(), "git", "invalid-revision") + err := Checkout(t.Context(), command.Git, "invalid-revision") if err == nil { t.Errorf("expected error when checking out a non-existent revision, but did not get one") } } func TestTag(t *testing.T) { - testhelper.RequireCommand(t, "git") + testhelper.RequireCommand(t, command.Git) const tagName = "test-tag" opts := testhelper.SetupOptions{ WithChanges: []string{testhelper.ReadmeFile}, } testhelper.Setup(t, opts) - commit, err := GetCommitHash(t.Context(), "git", "HEAD~") + commit, err := GetCommitHash(t.Context(), command.Git, "HEAD~") if err != nil { t.Fatal(err) } - err = Tag(t.Context(), "git", tagName, commit) + err = Tag(t.Context(), command.Git, tagName, commit) if err != nil { t.Fatal(err) } - taggedCommit, err := GetCommitHash(t.Context(), "git", tagName) + taggedCommit, err := GetCommitHash(t.Context(), command.Git, tagName) if err != nil { t.Fatal(err) } @@ -485,7 +485,7 @@ func TestTag(t *testing.T) { } func TestTag_Error(t *testing.T) { - testhelper.RequireCommand(t, "git") + testhelper.RequireCommand(t, command.Git) for _, test := range []struct { name string tagName string @@ -519,7 +519,7 @@ func TestTag_Error(t *testing.T) { } { t.Run(test.name, func(t *testing.T) { testhelper.SetupRepo(t) - err := Tag(t.Context(), "git", test.tagName, test.commit) + err := Tag(t.Context(), command.Git, test.tagName, test.commit) if err == nil { t.Fatal("wanted an error; got none") } @@ -528,16 +528,16 @@ func TestTag_Error(t *testing.T) { } func TestGetCommitHash(t *testing.T) { - testhelper.RequireCommand(t, "git") + testhelper.RequireCommand(t, command.Git) opts := testhelper.SetupOptions{ WithChanges: []string{testhelper.ReadmeFile}, } testhelper.Setup(t, opts) - commits, err := FindCommitsForPath(t.Context(), "git", ".") + commits, err := FindCommitsForPath(t.Context(), command.Git, ".") if err != nil { t.Fatal(err) } - headCommit, err := GetCommitHash(t.Context(), "git", "HEAD") + headCommit, err := GetCommitHash(t.Context(), command.Git, "HEAD") if err != nil { t.Fatal(err) } @@ -546,7 +546,7 @@ func TestGetCommitHash(t *testing.T) { t.Errorf("GetCommitHash() for HEAD: got = %s; want = %s", headCommit, commits[0]) } - previousToHeadCommit, err := GetCommitHash(t.Context(), "git", "HEAD~") + previousToHeadCommit, err := GetCommitHash(t.Context(), command.Git, "HEAD~") if err != nil { t.Fatal(err) } @@ -557,7 +557,7 @@ func TestGetCommitHash(t *testing.T) { } func TestGetCommitSubject(t *testing.T) { - testhelper.RequireCommand(t, "git") + testhelper.RequireCommand(t, command.Git) for _, test := range []struct { name string setup func(*testing.T) @@ -593,7 +593,7 @@ func TestGetCommitSubject(t *testing.T) { t.Run(test.name, func(t *testing.T) { testhelper.SetupRepo(t) test.setup(t) - got, err := GetCommitSubject(t.Context(), "git", test.revision) + got, err := GetCommitSubject(t.Context(), command.Git, test.revision) if err != nil { t.Fatal(err) } @@ -606,7 +606,7 @@ func TestGetCommitSubject(t *testing.T) { func TestGetCommitSubject_Error(t *testing.T) { testhelper.SetupRepo(t) - _, err := GetCommitSubject(t.Context(), "git", "bad-revision") + _, err := GetCommitSubject(t.Context(), command.Git, "bad-revision") if err == nil { t.Fatal("wanted an error; got none") } diff --git a/internal/librarian/bump.go b/internal/librarian/bump.go index e370668879..72ffb85678 100644 --- a/internal/librarian/bump.go +++ b/internal/librarian/bump.go @@ -104,7 +104,7 @@ func runBump(ctx context.Context, cfg *config.Config, all bool, libraryName, ver if cfg.Release != nil { preinstalled = cfg.Release.Preinstalled } - gitExe := command.GetExecutablePath(preinstalled, "git") + gitExe := command.GetExecutablePath(preinstalled, command.Git) if err := git.AssertGitStatusClean(ctx, gitExe); err != nil { return err } @@ -232,9 +232,9 @@ func bumpLibrary(cfg *config.Config, lib *config.Library, versionOverride string func postBump(ctx context.Context, cfg *config.Config) error { switch cfg.Language { case config.LanguageRust: - cargoExe := "cargo" + cargoExe := command.Cargo if cfg.Release != nil { - cargoExe = command.GetExecutablePath(cfg.Release.Preinstalled, "cargo") + cargoExe = command.GetExecutablePath(cfg.Release.Preinstalled, command.Cargo) } if err := command.Run(ctx, cargoExe, "update", "--workspace"); err != nil { return err diff --git a/internal/librarian/publish.go b/internal/librarian/publish.go index 917b5e1ba0..3ed2ad5217 100644 --- a/internal/librarian/publish.go +++ b/internal/librarian/publish.go @@ -85,9 +85,9 @@ func legacyRustPublish(ctx context.Context, cfg *config.Config, cmd *cli.Command // publish, in case of overlapping releases being performed. The execute flag // says whether to actually publish (true) or just perform a dry run (false). func publish(ctx context.Context, cfg *config.Config, releaseCommit string, execute bool) error { - gitExe := "git" + gitExe := command.Git if cfg.Release != nil { - gitExe = command.GetExecutablePath(cfg.Release.Preinstalled, "git") + gitExe = command.GetExecutablePath(cfg.Release.Preinstalled, command.Git) } if err := git.AssertGitStatusClean(ctx, gitExe); err != nil { return err diff --git a/internal/librarian/rust/create.go b/internal/librarian/rust/create.go index 0e420d2104..58563bc434 100644 --- a/internal/librarian/rust/create.go +++ b/internal/librarian/rust/create.go @@ -24,13 +24,13 @@ import ( // create creates a cargo workspace skeleton. func create(ctx context.Context, outputDir string) error { - if err := command.Run(ctx, "cargo", "--version"); err != nil { + if err := command.Run(ctx, command.Cargo, "--version"); err != nil { return fmt.Errorf("got an error trying to run `cargo --version`, the instructions on https://www.rust-lang.org/learn/get-started may solve this problem: %w", err) } if err := command.Run(ctx, "taplo", "--version"); err != nil { return fmt.Errorf("got an error trying to run `taplo --version`, please install using `cargo install taplo-cli`: %w", err) } - if err := command.Run(ctx, "cargo", "new", "--vcs", "none", "--lib", outputDir); err != nil { + if err := command.Run(ctx, command.Cargo, "new", "--vcs", "none", "--lib", outputDir); err != nil { return err } return command.Run(ctx, "taplo", "fmt", "Cargo.toml") @@ -39,14 +39,14 @@ func create(ctx context.Context, outputDir string) error { // validate does formatting and other post generation tasks to validate the library. var validate = func(ctx context.Context, outputDir string) error { manifestPath := path.Join(outputDir, "Cargo.toml") - if err := command.Run(ctx, "cargo", "fmt", "--manifest-path", manifestPath); err != nil { + if err := command.Run(ctx, command.Cargo, "fmt", "--manifest-path", manifestPath); err != nil { return err } - if err := command.Run(ctx, "cargo", "test", "--manifest-path", manifestPath); err != nil { + if err := command.Run(ctx, command.Cargo, "test", "--manifest-path", manifestPath); err != nil { return err } - if err := command.RunWithEnv(ctx, map[string]string{"RUSTDOCFLAGS": "-D warnings"}, "cargo", "doc", "--manifest-path", manifestPath, "--no-deps"); err != nil { + if err := command.RunWithEnv(ctx, map[string]string{"RUSTDOCFLAGS": "-D warnings"}, command.Cargo, "doc", "--manifest-path", manifestPath, "--no-deps"); err != nil { return err } - return command.Run(ctx, "cargo", "clippy", "--manifest-path", manifestPath, "--", "--deny", "warnings") + return command.Run(ctx, command.Cargo, "clippy", "--manifest-path", manifestPath, "--", "--deny", "warnings") } diff --git a/internal/librarian/rust/generate.go b/internal/librarian/rust/generate.go index cede5305fc..9f4ec2cb08 100644 --- a/internal/librarian/rust/generate.go +++ b/internal/librarian/rust/generate.go @@ -121,7 +121,7 @@ func createRepoMetadata(cfg *config.Config, library *config.Library, sources *so // UpdateWorkspace updates dependencies for the entire Rust workspace. func UpdateWorkspace(ctx context.Context) error { - return command.Run(ctx, "cargo", "update", "--workspace") + return command.Run(ctx, command.Cargo, "update", "--workspace") } // Format formats a generated Rust library. Must be called sequentially; @@ -131,7 +131,7 @@ func Format(ctx context.Context, library *config.Library) error { if err := command.Run(ctx, "taplo", "fmt", filepath.Join(library.Output, "Cargo.toml")); err != nil { return err } - if err := command.Run(ctx, "cargo", "fmt", "-p", library.Name); err != nil { + if err := command.Run(ctx, command.Cargo, "fmt", "-p", library.Name); err != nil { return err } return nil diff --git a/internal/librarian/rust/publish.go b/internal/librarian/rust/publish.go index e2f54540b3..992e3ffbbc 100644 --- a/internal/librarian/rust/publish.go +++ b/internal/librarian/rust/publish.go @@ -65,10 +65,10 @@ var errSemverCheck = errors.New("semver check failed") // Publish finds all the crates that should be published. It can optionally // run in dry-run mode, dry-run mode with continue on errors, and/or skip semver checks. func Publish(ctx context.Context, cfg *config.Release, dryRun, dryRunKeepGoing, skipSemverChecks bool) error { - if err := preFlight(ctx, cfg.Preinstalled, cfg.Tools["cargo"]); err != nil { + if err := preFlight(ctx, cfg.Preinstalled, cfg.Tools[command.Cargo]); err != nil { return err } - gitExe := command.GetExecutablePath(cfg.Preinstalled, "git") + gitExe := command.GetExecutablePath(cfg.Preinstalled, command.Git) lastTag, err := git.GetLastTag(ctx, gitExe, config.RemoteUpstream, config.BranchMain) if err != nil { return err @@ -96,7 +96,7 @@ func publishCrates(ctx context.Context, cfg *config.Release, dryRun, dryRunKeepG } } slog.Info("computing publication plan with: cargo workspaces plan") - cargoPath := command.GetExecutablePath(cfg.Preinstalled, "cargo") + cargoPath := command.GetExecutablePath(cfg.Preinstalled, command.Cargo) output, err := command.Output(ctx, cargoPath, "workspaces", "plan", "--skip-published") if err != nil { return err @@ -117,7 +117,7 @@ func publishCrates(ctx context.Context, cfg *config.Release, dryRun, dryRunKeepG slog.Info(fmt.Sprintf("there are %d crates in need of publishing, summary=%v", totalCrates, crateSummary)) if !skipSemverChecks { - gitPath := command.GetExecutablePath(cfg.Preinstalled, "git") + gitPath := command.GetExecutablePath(cfg.Preinstalled, command.Git) if err := runSemverChecks(ctx, semverData{ dryRunKeepGoing: dryRunKeepGoing, manifests: manifests, diff --git a/internal/librarian/rust/verify_cargo_tools.go b/internal/librarian/rust/verify_cargo_tools.go index 2e7d6395bd..5cd0569af4 100644 --- a/internal/librarian/rust/verify_cargo_tools.go +++ b/internal/librarian/rust/verify_cargo_tools.go @@ -26,14 +26,14 @@ import ( // preFlight performs all the necessary checks before a release. func preFlight(ctx context.Context, preinstalled map[string]string, cargoTools []config.Tool) error { - gitExe := command.GetExecutablePath(preinstalled, "git") + gitExe := command.GetExecutablePath(preinstalled, command.Git) if err := git.CheckVersion(ctx, gitExe); err != nil { return err } if err := git.CheckRemoteURL(ctx, gitExe, config.RemoteUpstream); err != nil { return err } - return cargoPreFlight(ctx, command.GetExecutablePath(preinstalled, "cargo"), cargoTools) + return cargoPreFlight(ctx, command.GetExecutablePath(preinstalled, command.Cargo), cargoTools) } // cargoPreFlight verifies all the necessary cargo tools are installed. diff --git a/internal/librarian/tag.go b/internal/librarian/tag.go index 4cd29c50a8..ed7a435a0f 100644 --- a/internal/librarian/tag.go +++ b/internal/librarian/tag.go @@ -66,9 +66,9 @@ func tagCommand() *cli.Command { // release commit to publish (unless already specified). The configuration at // the release commit is used for all further operations. func tag(ctx context.Context, cfg *config.Config, releaseCommit string, createReleaseTag bool) error { - gitExe := "git" + gitExe := command.Git if cfg.Release != nil { - gitExe = command.GetExecutablePath(cfg.Release.Preinstalled, "git") + gitExe = command.GetExecutablePath(cfg.Release.Preinstalled, command.Git) } if err := git.AssertGitStatusClean(ctx, gitExe); err != nil { return err diff --git a/internal/librarianops/generate.go b/internal/librarianops/generate.go index 896e53c0e9..04df4c66d7 100644 --- a/internal/librarianops/generate.go +++ b/internal/librarianops/generate.go @@ -177,18 +177,18 @@ func cloneRepo(ctx context.Context, repoDir, repoName string) error { func createBranch(ctx context.Context, now time.Time) error { branchName := fmt.Sprintf("%s%s", branchPrefix, now.UTC().Format("20060102T150405Z")) - return command.Run(ctx, "git", "checkout", "-b", branchName) + return command.Run(ctx, command.Git, "checkout", "-b", branchName) } func commitChanges(ctx context.Context) error { - if err := command.Run(ctx, "git", "add", "."); err != nil { + if err := command.Run(ctx, command.Git, "add", "."); err != nil { return err } - return command.Run(ctx, "git", "commit", "-m", commitTitle) + return command.Run(ctx, command.Git, "commit", "-m", commitTitle) } func pushBranch(ctx context.Context) error { - return command.Run(ctx, "git", "push", "-u", "origin", "HEAD") + return command.Run(ctx, command.Git, "push", "-u", "origin", "HEAD") } func createPR(ctx context.Context, repoName string) error { @@ -202,7 +202,7 @@ func createPR(ctx context.Context, repoName string) error { } func runCargoUpdate(ctx context.Context) error { - return command.Run(ctx, "cargo", "update", "--workspace") + return command.Run(ctx, command.Cargo, "update", "--workspace") } func runLibrarianWithVersion(ctx context.Context, version string, verbose bool, args ...string) error { diff --git a/internal/sidekick/rust_prost/generate.go b/internal/sidekick/rust_prost/generate.go index ab47e1aca5..58acd404ea 100644 --- a/internal/sidekick/rust_prost/generate.go +++ b/internal/sidekick/rust_prost/generate.go @@ -38,7 +38,7 @@ func Generate(ctx context.Context, model *api.API, outdir string, cfg *parser.Mo if cfg.SpecificationFormat != libconfig.SpecProtobuf { return fmt.Errorf("the `rust+prost` generator only supports `protobuf` as a specification source, outdir=%s", outdir) } - if err := command.Run(ctx, "cargo", "--version"); err != nil { + if err := command.Run(ctx, command.Cargo, "--version"); err != nil { return fmt.Errorf("got an error trying to run `cargo --version`, the instructions on https://www.rust-lang.org/learn/get-started may solve this problem: %w", err) } if err := command.Run(ctx, "protoc", "--version"); err != nil { @@ -80,7 +80,7 @@ func buildRS(ctx context.Context, rootName, tmpDir, outDir string) error { if err != nil { return err } - cmd := exec.CommandContext(ctx, "cargo", "build", "--features", "_generate-protos") + cmd := exec.CommandContext(ctx, command.Cargo, "build", "--features", "_generate-protos") cmd.Dir = tmpDir cmd.Env = append(os.Environ(), fmt.Sprintf("SOURCE_ROOT=%s", absRoot)) cmd.Env = append(cmd.Env, fmt.Sprintf("DEST=%s", absOutDir)) diff --git a/internal/testhelper/testhelper.go b/internal/testhelper/testhelper.go index dee32c7b43..c5e01008ef 100644 --- a/internal/testhelper/testhelper.go +++ b/internal/testhelper/testhelper.go @@ -86,7 +86,7 @@ func SetupForVersionBump(t *testing.T, wantTag string) { // and changes the current working directory to it. func ContinueInNewGitRepository(t *testing.T, tmpDir string) { t.Helper() - RequireCommand(t, "git") + RequireCommand(t, command.Git) t.Chdir(tmpDir) RunGit(t, "init", "-b", config.BranchMain) configNewGitRepository(t) @@ -100,7 +100,7 @@ func configNewGitRepository(t *testing.T) { func initRepositoryContents(t *testing.T) { t.Helper() - RequireCommand(t, "git") + RequireCommand(t, command.Git) if err := os.WriteFile(ReadmeFile, []byte(ReadmeContents), 0644); err != nil { t.Fatal(err) } @@ -261,7 +261,7 @@ func CloneRepositoryBranch(t *testing.T, remoteDir, branch string) { // RunGit runs git with the specified arguments, aborting the test on any error. func RunGit(t *testing.T, args ...string) { - if err := command.Run(t.Context(), "git", args...); err != nil { + if err := command.Run(t.Context(), command.Git, args...); err != nil { t.Fatal(err) } } diff --git a/tool/cmd/builddockerimages/main.go b/tool/cmd/builddockerimages/main.go index 72f686be3b..52913a519c 100644 --- a/tool/cmd/builddockerimages/main.go +++ b/tool/cmd/builddockerimages/main.go @@ -118,5 +118,5 @@ func cloneRepo(ctx context.Context, repoDir, revision string) error { "--revision=" + revision, repoDir, } - return command.Run(ctx, "git", args...) + return command.Run(ctx, command.Git, args...) }