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

Appending to an existing array with double quoted style not working #722

Closed
inianv opened this issue Feb 11, 2021 · 5 comments
Closed

Appending to an existing array with double quoted style not working #722

inianv opened this issue Feb 11, 2021 · 5 comments
Labels

Comments

@inianv
Copy link

inianv commented Feb 11, 2021

When trying to update/append to an array of existing strings, keeping "double" style also modifies other elements. As seen on yq version 4.5.1 (Linux)

apiVersion: batch/v1
kind: Job
metadata:
    name: test004
    annotations:
      kompose.cmd: kompose convert -f docker-compose.yml
spec:
    template:
        spec:
            containers:
                - name: "eee"
                  image: "test"
                  command: ["a", "b", "c"]
                  ports:
                   - containerPort: 6565

Objective is to modify the command to

command: ["a", "b", "c", "d"]

Tried below which works, but also applies the style attribute to containerPort, converting it to a string.

yq e '.spec.template.spec.containers[0].command |= ["a", "b", "c","d"]|..style="double"' yaml
@mikefarah
Copy link
Owner

.. is a recursive operator that matches everything, you need to match the elements of that specific array:

yq e '.spec.template.spec.containers[0].command |= ["a", "b", "c","d"]| .spec.template.spec.containers[0].command.[] style="double"' yaml

I'm not at a pc with yq but I'm pretty sure you can also just:

yq e '.spec.template.spec.containers[0].command += "d"' yaml

@inianv
Copy link
Author

inianv commented Feb 12, 2021

@mikefarah I was hoping below would work too, but it is leaving d, not quoted like the rest i.e. ["a", "b", "c", d]

yq e '.spec.template.spec.containers[0].command += "d"' yaml

Any chance to optimize this to a shorter form?

yq e '.spec.template.spec.containers[0].command |= ["a", "b", "c","d"]| .spec.template.spec.containers[0].command.[] style="double"'

@mikefarah
Copy link
Owner

Ah yep I see it now - the newly appended element has no styling and so it defaults to idiomatic yaml styling - that is no quotes.

I guess the best thing for yq to do would be to look at the previous element in the array and copy the styling when appending.

@mikefarah
Copy link
Owner

mikefarah commented Feb 18, 2021

Ooh I did just think of a shorter form using variables you could use in the interim:

yq e '.spec.template.spec.containers[0].command as $x | $x = ["a", "b", "c","d"] | ($x | .[]) style= "double"' file.yaml

It looks like you're just adding 'd' so you could also:

yq e '.spec.template.spec.containers[0].command as $x | $x += "d" | $x[-1] style="double"' file.yaml

@mikefarah
Copy link
Owner

Fixed in 4.19.1.

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

No branches or pull requests

2 participants