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

Environment variable nested items function #1087

Closed
aaron-uk opened this issue Jan 26, 2022 · 5 comments
Closed

Environment variable nested items function #1087

aaron-uk opened this issue Jan 26, 2022 · 5 comments

Comments

@aaron-uk
Copy link

aaron-uk commented Jan 26, 2022

Please describe your feature request.

I wish I could use yq to split a variable string into an array to use within my code.

As 'variable=a yq evel '.[env(variable)] = "something"' only works at one level nesting, I would like a new function to work with multiple nested paths e.g. 'nested_variable=a.b.c yq evel '.[arrenv(nested_variable)] = "something"' (I have used 'arr' prefix as an example but it could be anything.)

The main reason for this is to have a reusable task for my continuous integration pipeline where the path to the 'tag' in the example below will be in different nested positions in the .yaml file on different projects.

Describe the solution you'd like
If we have values.yaml like:

project:
  environment:
    PORT: 389
  image:
    repository: project/image
    pullPolicy: Always
    tag: "1.0.0"

And we run a command:

tag_path=project.image.tag yq eval '.[arrenv(tag_path)] = "1.0.1"' -i values.yaml

it could output

project:
  environment:
    PORT: 389
  image:
    repository: project/image
    pullPolicy: Always
    tag: "1.0.1"

I love using yq and this would make it even better!
cheers Aaron

@mikefarah
Copy link
Owner

Ah I see - it would need to evaluate the expression in .[exp] as a fully embedded yq expression instead of assuming its just a key/index into the current context. I think this is tricky but doable - just need enough free time.

As a work around, you could use bash string interpolation to work around it - though that can get tricky with quote escaping as you'll need to wrap the expression in double quotes so bash knows to interpolate it.

tag_path=project.image.tag yq eval "${tag_path} = \"1.0.1\"" -i values.yaml

@mikefarah
Copy link
Owner

I've added a eval operator - will be out in the next release :)

@mikefarah
Copy link
Owner

Fixed in 4.19.1 - see

@mikefarah
Copy link
Owner

Forgot link: https://mikefarah.gitbook.io/yq/operators/eval

@aaron-uk
Copy link
Author

aaron-uk commented Feb 6, 2022

Hi Mike, I just wanted to say a massive thank you for adding this so quickly. I'll trial it in my development environment this coming week and let you know how I get on.

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

2 participants