github-pr-resource icon indicating copy to clipboard operation
github-pr-resource copied to clipboard

load_var friendly format for metadata.json

Open vixus0 opened this issue 4 years ago • 3 comments

The current output format for metadata.json that gets populated on a get step is a list of maps:

[{"name": "item1", "value": "bla"},
 {"name": "item2", "value": "foo"},
 ...]

Instead, a simple map like {"item1": "bla", "item2": "foo"} would be more useful for Concourse's load_var step and then we wouldn't need a separate load_var for every metadata file that gets dumped.

vixus0 avatar Jan 10 '21 20:01 vixus0

Currently, the metadata.json is being populated with a list of maps:

[
  {"name": "item1", "value": "bla"},
  {"name": "item2", "value": "foo"},
  ...
]

To change this to a simple map structure like:

{
  "item1": "bla",
  "item2": "foo",
  ...
}

You'll need to modify the logic that generates the metadata.json file in Concourse. Here's a high-level approach to achieve this:

  1. Modify the Metadata Generation Logic: Locate the part of your Concourse pipeline or resource definition where the metadata.json is generated after a get step. This is typically done in a custom script or resource script.

  2. Transform List of Maps to Map: Inside the script, after fetching the metadata, you'll need to transform the list of maps into the desired map structure. You can achieve this by iterating through the list of maps and constructing the new map:

    # Fetch metadata.json content
    metadata_json=$(cat metadata.json)
    
    # Transform list of maps to map
    map_json="{}"
    for item in $(echo "$metadata_json" | jq -r '.[] | @base64'); do
      name=$(echo "$item" | base64 --decode | jq -r '.name')
      value=$(echo "$item" | base64 --decode | jq -r '.value')
      map_json=$(echo "$map_json" | jq --arg name "$name" --arg value "$value" '.[$name] = $value')
    done
    
    # Output the transformed map as JSON
    echo "$map_json" > transformed_metadata.json
    
  3. Use the Transformed Metadata: The transformed_metadata.json file now contains the desired map structure. You can use this transformed metadata in your Concourse tasks or steps that rely on it.

ljluestc avatar Sep 04 '23 20:09 ljluestc