magefile / mage

a Make/rake-like dev tool using Go
https://magefile.org
Apache License 2.0
4.01k stars 250 forks source link

Exec args always expanded #434

Open hborham opened 1 year ago

hborham commented 1 year ago

Within the sh package, args are being expanded in the case where normal sh command would not expand args within single quotes. Example command:

pact-broker create-webhook --user='admin:${user.bitbucketAppPassword}'

  1. Via normal shell the single quotes will not expand ${user.bitbucketAppPassword}
  2. Via mage the expansion occurs and the actual command becomes: pact-broker create-webhook --user='admin:'

https://github.com/magefile/mage/blob/26cdb5c7369a9b03981eb906dd3606c566e45c16/sh/cmd.go#L92-L115

fergonco commented 11 months ago

Similar problem here. I am calling sh.Exec with a list of arguments one of which contains $. I want the literal $ but mage tries to expand it and I found no way to prevent this.

rkennedy commented 2 months ago

I've just submitted a PR that I think would solve this. But @hborham, note that for your example to work the way you want, you'd need to remove the apostrophes from your arguments. Apostrophes are one way to avoid having the shell expand environment variables. In doing so, the shell would strip the apostrophes from the argument prior to invoking the given command, so pact-broker would receive literally --user=admin:${user.bitbucketAppPassword} as its argument. Using my PR, you might write code like this:

sh.Run("pact-broker", "create-webhook", sh.Escape("--user=admin:${user.bitbucketAppPassword}"))