yq数组操作返回字符串文字而非数组

huangapple go评论75阅读模式
英文:

yq array manipulation returns string literal instead of an array

问题

I am currently trying to manipulate a yaml file using yq. For example purposes I condensed it down to the important parts. There is a keys variables that I am using to replace the values of .spec.data. However, I am getting no the desired result. It creates a string literal for spec.data instead of keeping it in array format with its new items. How to achieve the replacement of an array with its new items without turning into as string literal?

original yaml

spec:
  data:
    - secretKey: VARIABLE_1
      remoteRef: 
        key: VARIABL_1
    - secretKey: VARIABLE_N
      remoteRef: 
        key: VARIABLE_N

current yaml output

spec:
  data: |-
    - secretKey: NEW_VARIABLE_1
      remoteRef:
        key: NEW_VARIABLE_1
    - secretKey: NEW_VARIABLE_2
      remoteRef:
        key: NEW_VARIABLE_2
    - secretKey: NEW_VARIABLE_N
      remoteRef:
        key: NEW_VARIABLE_N    

desired yaml output

spec:
  data: 
    - secretKey: NEW_VARIABLE_1
      remoteRef:
        key: NEW_VARIABLE_1
    - secretKey: NEW_VARIABLE_2
      remoteRef:
        key: NEW_VARIABLE_2
    - secretKey: NEW_VARIABLE_N
      remoteRef:
        key: NEW_VARIABLE_N

code

bashArray=("NEW_VARIABLE_1" "NEW_VARIABLE_2" "NEW_VARIABLE_N")

keys=$(jq -c -n '$ARGS.positional' --args "${bashArray[@]}")

# Create a JSON array of the new data
new_data=$(echo "$keys" | jq -nR '[inputs|{secretKey: ., remoteRef: {key: .}}]')

# Convert the JSON array to YAML and update the file
new_data_yaml=$(echo "$new_data" | yq e -P -  )

yq e -i ".spec.data = \"$new_data_yaml\"" test.yaml
英文:

I am currently trying to manipulate a yaml file using yq. For example purposes I condensed it down to the important parts. There is a keys variables that I am using to replace the values of .spec.data. However, I am getting no the desired result. It creates a string literal for spec.data instead of keeping it in array format with its new items. How to achieve the replacement of an array with its new items without turning into as string literal?

original yaml

spec:
  data:
    - secretKey: VARIABLE_1
      remoteRef: 
        key: VARIABL_1
    - secretKey: VARIABLE_N
      remoteRef: 
        key: VARIABLE_N

curent yaml output

spec:
  data: |-
    - secretKey: NEW_VARIABLE_1
      remoteRef:
        key: NEW_VARIABLE_1
    - secretKey: NEW_VARIABLE_2
      remoteRef:
        key: NEW_VARIABLE_2
    - secretKey: NEW_VARIABLE_N
      remoteRef:
        key: NEW_VARIABLE_N

desires yaml output

spec:
  data: 
    - secretKey: NEW_VARIABLE_1
      remoteRef:
        key: NEW_VARIABLE_1
    - secretKey: NEW_VARIABLE_2
      remoteRef:
        key: NEW_VARIABLE_2
    - secretKey: NEW_VARIABLE_N
      remoteRef:
        key: NEW_VARIABLE_N

code

bashArray=("NEW_VARIABLE_1" "NEW_VARIABLE_2" "NEW_VARIABLE_N")

keys=$(jq -c -n '$ARGS.positional' --args "${bashArray[@]}")

# Create a JSON array of the new data
new_data=$(echo "$keys" | jq -nR '[inputs|{secretKey: ., remoteRef: {key: .}}]')

# # Convert the JSON array to YAML and update the file
new_data_yaml=$(echo "$new_data" | yq e -P -  )

yq e -i ".spec.data = \"$new_data_yaml\"" test.yaml

答案1

得分: 1

假设您正在使用 mikefarah/yq,您可以使用 ... style="" 来重新格式化YAML代码。此外,您还可以直接导入JSON,可以使用 env 通过环境变量更轻松地导入,并且可以使用 += 将内容追加到现有数组中:

# 如果 $new_data 是一个 JSON 数组
new_data="$new_data" yq e -i '.spec.data += (env(new_data) | ... style="")' file.yaml
英文:

Assuming you are using mikefarah/yq, you can use ... style="" to reformat the YAML code. Also, you can import JSON directly, you can use env for easier import through environment variables, and you can use += to append to an existing array:

# If $new_data is a JSON array
new_data="$new_data" yq e -i '.spec.data += (env(new_data) | ... style="")' file.yaml

huangapple
  • 本文由 发表于 2023年5月18日 03:06:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76275446.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定