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

adding item to array for multiple paths with unexpected result #750

Closed
ak-at-gvl opened this issue Mar 15, 2021 · 3 comments
Closed

adding item to array for multiple paths with unexpected result #750

ak-at-gvl opened this issue Mar 15, 2021 · 3 comments
Labels
Milestone

Comments

@ak-at-gvl
Copy link

ak-at-gvl commented Mar 15, 2021

Bug-Desciption
My use case is to add an environment variable to all given services in a docker-compose.yml file – without knowing the service names.
Unfortunately the specific list of environment-variables of each services is not appended with the new variable, but only the very first occurrence of the array is appended correctly and then copied wrongly the whole resulting array of the first service to all other services.

Version of yq: 4.4.1 & 4.6.1
Operating system: mac & linux
Installed on linux via:

wget "https://github.com/mikefarah/yq/releases/download/v4.6.1/yq_linux_amd64" -O /usr/bin/yq && chmod +x /usr/bin/yq

on mac via homebrew

Input Yaml

sample.yaml:

version: "1.2.3"
services:
  svc1:
    environment:
      - ENV_VAR1_1
      - ENV_VAR1_2
  svc2:
    environment:
      - ENV_VAR2_1
      - ENV_VAR2_2
      - ENV_VAR2_3
      - ENV_VAR2_4
  svc3:
    environment:
      - ENV_VAR3_1
      - ENV_VAR3_2
      - ENV_VAR3_3
  svc4:
    image: busybox:latest

Command
The command i ran:

yq eval '.services[].environment += ["NEW_ARRAY_ENTRY"]' sample.yaml

Actual behavior

version: "1.2.3"
services:
  svc1:
    environment:
      - ENV_VAR1_1
      - ENV_VAR1_2
      - NEW_ARRAY_ENTRY
  svc2:
    environment:
      - ENV_VAR1_1
      - ENV_VAR1_2
      - NEW_ARRAY_ENTRY
  svc3:
    environment:
      - ENV_VAR1_1
      - ENV_VAR1_2
      - NEW_ARRAY_ENTRY
  svc4:
    image: busybox:latest
    environment:
      - ENV_VAR1_1
      - ENV_VAR1_2
      - NEW_ARRAY_ENTRY

Expected behavior

version: "1.2.3"
services:
  svc1:
    environment:
      - ENV_VAR1_1
      - ENV_VAR1_2
      - NEW_ARRAY_ENTRY
  svc2:
    environment:
      - ENV_VAR2_1
      - ENV_VAR2_2
      - ENV_VAR2_3
      - ENV_VAR2_4
      - NEW_ARRAY_ENTRY
  svc3:
    environment:
      - ENV_VAR3_1
      - ENV_VAR3_2
      - ENV_VAR3_3
      - NEW_ARRAY_ENTRY
  svc4:
    image: busybox:latest
    environment:
      - NEW_ARRAY_ENTRY

Additional context
My current workaround is to use yq to transform the file into JSON, modifying with jq and using yq again to re-transform the result into YAML – which is actually very ugly:

yq --tojson eval '.' docker-compose.yml > docker-compose.json && \
  jq '.services[].environment += ["NEW_ARRAY_ENTRY"]' docker-compose.json | \
  yq eval -P > docker-compose.yml && \
  rm docker-compose.json;
@ak-at-gvl ak-at-gvl changed the title adding item to array for multiple path works unexpectedly adding item to array for multiple paths with unexpected result Mar 15, 2021
@mikefarah
Copy link
Owner

Looks like a bug with '+=' :( I'll look into it asap - as a work around you can:

yq e '.services.*.environment |= . + ["cat"]' examples/data1.yaml

@mikefarah mikefarah added this to the 4.6.2 milestone Mar 19, 2021
mikefarah added a commit that referenced this issue Mar 19, 2021
@mikefarah
Copy link
Owner

Fixed in 4.6.2

@ak-at-gvl
Copy link
Author

ak-at-gvl commented Mar 19, 2021

Great! Thanks a lot for fixing this so quickly! 👍 🥳

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