yq icon indicating copy to clipboard operation
yq copied to clipboard

Add Powershell and CMD wrappers

Open phreed opened this issue 3 years ago • 7 comments

There are issues with escaping evaluation strings on Windows. see #747 #777 et al. Add a yq.ps1 file

Param(
    [string]$command,
    [string]$yamlFile
  )
$escCommand =  $command -replace '"','\"' 
$evalArgs = @("eval", "$escCommand", $yamlFile)

Write-Output ".\yq.exe"  $evalArgs
& ".\yq.exe"  $evalArgs

alternatives

Update the documentation to show how to escape/replace '"' with '"'.

yq e -n '.someNew="content"'.replace('"','\"' )
yq e -n '{"wrap": .}' .replace('"','\"' ) name.yaml

I have not looked into what a similar *.bat or *.cmd file might look like.

phreed avatar May 28 '21 22:05 phreed

The approaches mentioned above may be useful in correcting the documentation found here. https://mikefarah.gitbook.io/yq/usage/tips-and-tricks#quotes-in-windows-powershell

phreed avatar May 28 '21 23:05 phreed

Cool - thanks for this @phreed - if the ps1 file is added, then what should the instructions be for running yq in Powershell?

mikefarah avatar May 29 '21 23:05 mikefarah

I did not write it to be a complete wrapper (the eval command is assumed) but other than that it is pretty much what you might expect. e.g.

yq.ps1 <filter> sample.yaml
yq.ps1 '.someNew="content"' sample.yaml

The filter needs to be enclosed in single quotes to protect any double quotes. I can rework the ps1 script to if you like to make it use all the yq commands, etc. As currently written yq.ps1 assumes the yq.exe is located in the current working directory. It should probably be located in the same directory as the yq.ps1 (not a big problem to correct). I personally despise *.cmd files but you could probably find someone willing to convert a powershell script.

phreed avatar May 30 '21 18:05 phreed

Cool - happy to take your direction, I'm not a powershell user so I'm at a bit of a loss with these things.

mikefarah avatar May 30 '21 23:05 mikefarah

Any luck with a PR for this?

mikefarah avatar Jun 25 '21 03:06 mikefarah

I got most of it working and then took a holiday. I have to get some things done for my day job and then I will get back to this.

On Thu, Jun 24, 2021 at 10:21 PM Mike Farah @.***> wrote:

Any luck with a PR for this?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/mikefarah/yq/issues/841#issuecomment-868173754, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABTVPGMFD5HOQ2F4B5IQKLTUPY5LANCNFSM45XRQHYQ .

phreed avatar Jul 07 '21 00:07 phreed

I'm using yq in powershell and would be happy to see it work without workaround!

MartinDelille avatar Apr 27 '22 14:04 MartinDelille