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

Conversation

jesusfcr
Copy link
Contributor

@jesusfcr jesusfcr commented Apr 29, 2024

This PR allows to execute foreach commands with parameters that include spaces.

turbolift foreach git commit -a -m "my commit that usually has spaces"
turbolift foreach sed -i 's/text with spaces/text-without-spaces/g' README.md

Also improves the code by reusing the same command slice for all the repositories.

Copy link
Contributor

@sledigabel sledigabel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution and great finding,
I left a small suggestion about the string builder.

Also could you rename the PR with something about what it fixes which is quoted commands in foreach?
Ideally something standard like

fix(foreach): supports quoted commands

cmd/foreach/foreach.go Outdated Show resolved Hide resolved
@jesusfcr jesusfcr changed the title Add support to execute commands with spaces fix(foreach): supports quoted commands Apr 30, 2024
Copy link
Contributor

@sledigabel sledigabel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM now, but could you now add some unit tests around this fix?

Copy link
Contributor

@sledigabel sledigabel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you for your contribution!

@sledigabel sledigabel merged commit be8dfba into Skyscanner:main May 1, 2024
5 checks passed
@annettejanewilson
Copy link
Contributor

I think this breaks intended usages. Turbolift invokes a shell specifically to support invoking commands with pipes. So you can do things like this (slightly contrived).

turbolift foreach 'grep "foobar" README.md | sort`

If I understand correctly, this will cause that entire string to be quoted when passed to the shell, and then fail as an unrecognised command.

I do think that as it stood the handling for multiple arguments was a bit confused, but I'm worried this confuses things further. I think turbolift needs to sit on one side of this fence, and pick one of the following behaviours:

  • It takes all the arguments from foreach unmodified, doesn't invoke a shell and just executes them as a command directly.
  • It takes a single argument to foreach that is a shell script, and passes it to the shell via -c.

Some other concerns I have about this solution:

  • I don't think it handles empty arguments. (E.g. printf '[%s]\n' 'one' '' 'three')
  • I think it may have unexpected and confusing results if there are other characters that are special to the shell.
  • I'm not convinced that golang's quoting rules match up with POSIX shell.

I'll have a look at how this behaves in practice and raise a ticket.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants