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

Allow shebang on the first line of a file of yq filters #1851

Closed
rlee-arx opened this issue Nov 7, 2023 · 5 comments
Closed

Allow shebang on the first line of a file of yq filters #1851

rlee-arx opened this issue Nov 7, 2023 · 5 comments

Comments

@rlee-arx
Copy link

rlee-arx commented Nov 7, 2023

Please describe your feature request.
To enable the creation of .yq executable files, I would like yq to allow the first line of a --from-file to be a "shebang" (#!) which is subsequently ignored in the interpretation of the file contents.

Describe the solution you'd like
If we have data1.yml like:

country:
  Australia:
    - city: Perth

and data2.yml like:

country:
  Australia:
    - city: Brisbane

and merge-aus.yq like:

#!/usr/bin/env yq --null-input --from-file
load("data1.yml") as $perth |
load("data2.yml") as $bris |
$perth *+ $bris

And we run a command:

./merge-aus.yq

it could output

country:
  Australia:
  - city: Perth
  - city: Brisbane

Describe alternatives you've considered
I usually just write a shell script wrapper called merge-aus.yq which calls the appropriate yq --from-file command, but first class support would be awesome.

I have cloned the source locally, and will fork to create a PR if I can get to it :)

@mikefarah
Copy link
Owner

That would be very cool - would love a PR

rlee-arx added a commit to rlee-arx/yq that referenced this issue Nov 16, 2023
Fairly dirty way of allowing shebang lines

This snippet will read the first two characters of a file passed in to
`yq` and remove the line if it begins with "`#!`". This will allow `yq`
to continue parsing it as though the shebang line was not there.

I was not able to find canonical documentation that the shebang is
limited to a single line or cannot include continuation characters.
However these seem to be accepted truths. See:

https://linux.die.net/man/2/execve

https://en.wikipedia.org/wiki/Shebang_(Unix)#Syntax
rlee-arx added a commit to rlee-arx/yq that referenced this issue Nov 16, 2023
Fairly dirty way of allowing shebang lines

This snippet will read the first two characters of a file passed in to
`yq` and remove the line if it begins with "`#!`". This will allow `yq`
to continue parsing it as though the shebang line was not there.

I was not able to find canonical documentation that the shebang is
limited to a single line or cannot include continuation characters.
However these seem to be accepted truths. See:

https://linux.die.net/man/2/execve

https://en.wikipedia.org/wiki/Shebang_(Unix)#Syntax
mikefarah added a commit that referenced this issue Feb 15, 2024
@mikefarah
Copy link
Owner

Had a look at your draft PR @rlee-arx and worked out how to do it 👍🏼 . Will be in the next release.

@mikefarah
Copy link
Owner

Fixed in 4.42.1

@Myahr208
Copy link

Please describe your feature request. To enable the creation of .yq executable files, I would like yq to allow the first line of a --from-file to be a "shebang" (#!) which is subsequently ignored in the interpretation of the file contents.

Describe the solution you'd like If we have data1.yml like:

country:
  Australia:
    - city: Perth

and data2.yml like:

country:
  Australia:
    - city: Brisbane

and merge-aus.yq like:

#!/usr/bin/env yq --null-input --from-file
load("data1.yml") as $perth |
load("data2.yml") as $bris |
$perth *+ $bris

And we run a command:

./merge-aus.yq

it could output

country:
  Australia:
  - city: Perth
  - city: Brisbane

Describe alternatives you've considered I usually just write a shell script wrapper called merge-aus.yq which calls the appropriate yq --from-file command, but first class support would be awesome.

I have cloned the source locally, and will fork to create a PR if I can get to it :)

<chatgpt_3.5_turbo><help_block_devices_/>
``

@Myahr208
Copy link

Allow shebang on the first line of a file of yq filters
#1851 (comment)

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