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

Add Powershell and CMD wrappers #841

Open
phreed opened this issue May 28, 2021 · 7 comments
Open

Add Powershell and CMD wrappers #841

phreed opened this issue May 28, 2021 · 7 comments

Comments

@phreed
Copy link

phreed commented May 28, 2021

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
Copy link
Author

phreed commented May 28, 2021

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

@mikefarah
Copy link
Owner

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

@phreed
Copy link
Author

phreed commented May 30, 2021

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.

@mikefarah
Copy link
Owner

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
Copy link
Owner

Any luck with a PR for this?

@phreed
Copy link
Author

phreed commented Jul 7, 2021 via email

@MartinDelille
Copy link

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

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

No branches or pull requests

3 participants