From d021e1d5b53ea07e488b3e99efb71780bfbe1087 Mon Sep 17 00:00:00 2001 From: Thorsten Ball Date: Tue, 22 Sep 2020 16:37:51 +0200 Subject: [PATCH 1/2] Check that Docker and git are available when executing campaign spec We had the check for git before, but lost it in the switch to the new model. The Docker check I added after running into a hard-to-decipher error when trying to run `src campaigns apply` on a VM without Docker. --- cmd/src/campaigns_common.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/cmd/src/campaigns_common.go b/cmd/src/campaigns_common.go index 47e7eefaef..9d435db44a 100644 --- a/cmd/src/campaigns_common.go +++ b/cmd/src/campaigns_common.go @@ -6,6 +6,7 @@ import ( "fmt" "io" "os" + "os/exec" "path" "runtime" "strings" @@ -148,6 +149,14 @@ func campaignsOpenFileFlag(flag *string) (io.ReadCloser, error) { // to Sourcegraph, including execution as needed. The return values are the // spec ID, spec URL, and error. func campaignsExecute(ctx context.Context, out *output.Output, svc *campaigns.Service, flags *campaignsApplyFlags) (campaigns.CampaignSpecID, string, error) { + if err := checkExecutable("git", "version"); err != nil { + return "", "", err + } + + if err := checkExecutable("docker", "version"); err != nil { + return "", "", err + } + // Parse flags and build up our service options. var errs *multierror.Error @@ -410,3 +419,16 @@ func diffStatDiagram(stat diff.Stat) string { output.StyleLinesDeleted, strings.Repeat("-", int(deleted)), ) } + +func checkExecutable(cmd string, args ...string) error { + if err := exec.Command(cmd, args...).Run(); err != nil { + return fmt.Errorf( + "failed to execute \"%s %s\":\n\t%s\n\n'src campaigns' require %q to be available.", + cmd, + strings.Join(args, " "), + err, + cmd, + ) + } + return nil +} From 2b914924428ebfe9cbb2c4db473e4e3b3728da79 Mon Sep 17 00:00:00 2001 From: Thorsten Ball Date: Tue, 22 Sep 2020 16:40:06 +0200 Subject: [PATCH 2/2] Add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index fe41538e13..a4fecc23b8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ All notable changes to `src-cli` are documented in this file. - Error reporting by `src campaign [preview|apply]` has been improved and now includes more information about which step failed in which repository. [#325](https://github.com/sourcegraph/src-cli/pull/325) - The default behaviour of `src campaigns [preview|apply]` has been changed to retain downloaded archives of repositories for better performance across re-runs of the command. To use the old behaviour and delete the archives use the `-clean-archives` flag. Repository archives are also not stored in the directory for temp data (see `-tmp` flag) anymore but in the cache directory, which can be configured with the `-cache` flag. To manually delete archives between runs, delete the `*.zip` files in the `-cache` directory (see `src campaigns -help` for its default location). +- `src campaign [preview|apply]` now check whether `git` and `docker` are available before trying to execute a campaign spec's steps. [#326](https://github.com/sourcegraph/src-cli/pull/326) ### Fixed