Skip to content

Instantly share code, notes, and snippets.

@djeikyb
Last active October 10, 2023 21:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djeikyb/bf33dd6522472df3a662fd5148a6f516 to your computer and use it in GitHub Desktop.
Save djeikyb/bf33dd6522472df3a662fd5148a6f516 to your computer and use it in GitHub Desktop.
Turns an appsettings.json into flat json of key-value pairs, or, shell friendly list of plain key-value pairs
#!/bin/sh
toEnvVars() {
toFlatJson "$1" | jq --raw-output '. | to_entries[] | "\(.key)=\(.value)"'
}
toFlatJson() {
# thank you jeff mercado https://stackoverflow.com/a/42303836/659715
jq --arg delim '__' 'reduce (tostream|select(length==2)) as $i ({};
.[[$i[0][]|tostring]|join($delim)] = $i[1]
)' "$1"
}
# One of the args should be a json file.
# If there's only one arg, then that's the json file.
if [ -z "$2" ]; then
toFlatJson "$1"
exit 0
fi
# So we've been passed a file.
# Assume it's json. Sorry.
# Translate the json file to a shell-friendly list of k=v lines
[ "$1" = "-e" ] && toEnvVars "$2"
# Translate the json file to a flat object of key value pairs
[ "$1" = "-j" ] && toFlatJson "$2"
@djeikyb
Copy link
Author

djeikyb commented Oct 10, 2023

Can use like:

docker run --rm --env-file <(flatten -e SomeProject/appsettings.Docker.json) some-project

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment