2

I have the following yaml which I want to replace the command field

Have

apiVersion: batch/v1
kind: Job
metadata:
  name: test004
spec:
  template:
    spec:
      containers:
        - name: "eee"
          image: "test"
          command: ["a", "b", "c"]

want

apiVersion: batch/v1
kind: Job
metadata:
  name: test004
spec:
  template:
    spec:
      containers:
        - name: "eee"
          image: "test"
          command: ["a", "b", "c", "d"]
yq  -i n --style  myfile.yaml 'spec.template.spec.containers[0].command' "["a", "b", "c","d"]"

is there a way to achive this with yq, I try with style without success, If I change it to simple string it works, but not when I want to pass full array, any idea ?

Noel Yap
  • 18,822
  • 21
  • 92
  • 144
Rayn D
  • 579
  • 1
  • 13
  • 29

1 Answers1

5

You can use the field [+] notation to append fields in yq v3. So your command results in

yq --style double w -i myfile.yaml 'spec.template.spec.containers[0].command[+]' "c"

The above command appends the entry "c" to the array.

Or if you want to update the whole array command you could do in v4. Note that this creates the array entries in separate lines than what is shown in the OP. See YAML Multi-Line Arrays

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

Note that yq v4 is already out there and supports much powerful path expressions and operations. See Upgrading from v3 for instructions

Inian
  • 80,270
  • 14
  • 142
  • 161
  • 1
    Thanks, regard the first option, how does it know to put it in the third place of the array? – Rayn D Feb 10 '21 at 14:50
  • 1
    @RaynD: Just specify the index to insert on i.e. `command[2]` instead of `+` – Inian Feb 10 '21 at 14:52
  • 1
    Thanks i've tried it and it work however it delete third entry "d", any idea how to add and not delete ? – Rayn D Feb 10 '21 at 14:55
  • 1
    @RaynD: Am afraid you can't do that with `yq` v3 or v4 – Inian Feb 10 '21 at 14:57
  • 1
    so maybe add "c" and "d" , is it possible ? but each as new item – Rayn D Feb 10 '21 at 14:58
  • or maybe replace the all value with new value but type array ? `["a", "b", "c","d"]` ? – Rayn D Feb 10 '21 at 15:00
  • again, no support to append an array in v3 atleast, can you upgrade to v4? – Inian Feb 10 '21 at 15:03
  • what do you say, is it possible in version4? – Rayn D Feb 10 '21 at 16:50
  • Thanks, I've tried it but got and error, please see my update – Rayn D Feb 10 '21 at 18:49
  • @RaynD: Did you upgrade your yq version to latest? – Inian Feb 10 '21 at 18:52
  • @RaynD: You missed the `.` before `spec`. In v4 it is needed – Inian Feb 10 '21 at 19:00
  • thanks I try it like this `yq e '.spec.template.spec.containers[0].command |= ["a", "b", "c","d"] | ..style="double"' -i deployment.yaml` and the file gone mass, does it works for you? ` – Rayn D Feb 10 '21 at 19:19
  • does it works for you? i've provided the file in the question – Rayn D Feb 10 '21 at 19:22
  • @RaynD: Your original attempt had the `[0]` notation. Seems like it is not needed. See update – Inian Feb 10 '21 at 19:24
  • thanks but when I remove the 0 I got error : Error: Cannot index array with 'command' ... – Rayn D Feb 10 '21 at 19:33
  • @RaynD: It works for the YAML you pasted in the question. If you are testing this on a different YAML, post that to the question. – Inian Feb 11 '21 at 07:38
  • Sorry I think when I copied it some properties wasn't aligned, I put the link with the exact file, could you please have a look as I got error : `Error: Cannot index array with 'command' (strconv.ParseInt: parsing "command": invalid syntax) ` – Rayn D Feb 11 '21 at 10:09
  • @RaynD: With the current input `yq e '.spec.template.spec.containers[0].command |= ["a", "b", "c","d"] | ..style="double"'` works. – Inian Feb 11 '21 at 10:13
  • Thanks it works but the problem it add to other properties `""` e.g. before i've `containerPort: 6565` after I run the command it become `containerPort: "6565"` , any idea how to avoid it? – Rayn D Feb 11 '21 at 10:35
  • Again, where is `containerPort` in your YAML – Inian Feb 11 '21 at 10:49
  • Sorry, I didnt want to put all the file since its long but please have a look on this https://codebeautify.org/yaml-validator/cb05aa0c , `continarport` and `compose.cmd` values are changed . – Rayn D Feb 11 '21 at 11:48
  • @RaynD: Can't get this to work myself - have raised a bug report - https://github.com/mikefarah/yq/issues/722 – Inian Feb 11 '21 at 12:15
  • @Inian Can I pass a bash array variable to this command instead of hard coded arrray ["a", "b", "c","d"] ? – AnjK Nov 22 '22 at 04:12