Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(foreach): supports quoted commands #132

Merged
merged 4 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion cmd/foreach/foreach.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package foreach
import (
"os"
"path"
"strconv"
"strings"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -98,10 +99,16 @@ func run(c *cobra.Command, args []string) {
}
readCampaignActivity.EndWithSuccess()

for i := range args {
if strings.Contains(args[i], " ") {
args[i] = strconv.Quote(args[i])
}
}
command := strings.Join(args, " ")

var doneCount, skippedCount, errorCount int
for _, repo := range dir.Repos {
repoDirPath := path.Join("work", repo.OrgName, repo.RepoName) // i.e. work/org/repo
command := strings.Join(args, " ")

execActivity := logger.StartActivity("Executing %s in %s", command, repoDirPath)

Expand Down
17 changes: 17 additions & 0 deletions cmd/foreach/foreach_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,23 @@ func TestItRunsCommandInShellAgainstWorkingCopies(t *testing.T) {
})
}

func TestItRunsCommandQuotedInShellAgainstWorkingCopied(t *testing.T) {
fakeExecutor := executor.NewAlwaysSucceedsFakeExecutor()
exec = fakeExecutor

testsupport.PrepareTempCampaign(true, "org/repo1", "org/repo2")

out, err := runCommand("some", "command", "with spaces")
assert.NoError(t, err)
assert.Contains(t, out, "turbolift foreach completed")
assert.Contains(t, out, "2 OK, 0 skipped")

fakeExecutor.AssertCalledWith(t, [][]string{
{"work/org/repo1", userShell(), "-c", "some command \"with spaces\""},
{"work/org/repo2", userShell(), "-c", "some command \"with spaces\""},
})
}

func TestItSkipsMissingWorkingCopies(t *testing.T) {
fakeExecutor := executor.NewAlwaysSucceedsFakeExecutor()
exec = fakeExecutor
Expand Down
Loading