skip to Main Content

Here is my first input (recorded as bash var $services)
It can contain multiple key based values.

[
{
  "key": "movies-wikibase-jobrunner",
  "subPath": "LocalSettings.php",
  "cm": "movies-wikibase-jobrunner-localsettings-override-php"
},
{
  "key": "movies-wikibase-jobrunner",
  "subPath": "jobrunner-entrypoint.sh",
  "cm": "movies-wikibase-jobrunner-jobrunner-entrypoint-sh"

},
{
  "key": "movies-wikibase-wdqs-frontend",
  "subPath": "wdqs_front_index.html",
  "cm": "movies-wikibase-wdqs-frontend-index-html"
}
]

I’d like to use subPath to update deployment.json.

{
 "spec": {
  "template": {
   "spec": {
    "containers": [
     {
      "volumeMounts": [
       {
        "mountPath": "/usr/share/nginx/html/index.html",
        "name": "movies-wikibase-wdqs-frontend-index-html",
        "subPath": "wdqs_front_index.html"
}]}]}}}}

I tried to pass subPath as a bash argument to jq but nothing works

cat deployment.json | jq -r --arg services $services                                
  '.spec.template.spec.containers[].volumeMounts[]? |=
   { mountPath: .mountPath, name: (.key, subPath: $services
  |select(.key=="movies-wikibase-wdqs-frontend").subPath) }'

How can I achieve this, and is this the good way?

2

Answers


  1. Chosen as BEST ANSWER

    A simple bash loop based answer

    for i in $services |jq -r '.key'|uniq);
    do
      pair=$(echo $services| jq -r --arg i "$i" 'select(.key==$i).subPath'
        for j in $pair;
          do 
            cat $i-deployment.json | 
            jq -r --arg subPath $j 
            '(.spec.template.spec.containers[].volumeMounts+=[{subPath: $subPath}])' | sponge $i-deployment.json
          done
    done
    

  2. jq --argjson services "$services" '
      .spec.template.spec.containers[].volumeMounts[] |=
        (
          .name as $name |
          . + { mountPath: ($services[] | select(.key == $name).subPath // .mountPath) }
        )
    ' deployment.json > updated_deployment.json
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search