Skip to content

Instantly share code, notes, and snippets.

@Nezteb
Last active October 27, 2023 04:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Nezteb/9eb91294e0ad8dc12fc03f6863023e09 to your computer and use it in GitHub Desktop.
Save Nezteb/9eb91294e0ad8dc12fc03f6863023e09 to your computer and use it in GitHub Desktop.
Get and maybe update a VS Code setting
# Usage: codeSettings <key> [<value>]
codeSettings() {
SETTINGS_DIR="$HOME/Library/Application Support/Code/User"
FILE="$SETTINGS_DIR/settings.json"
KEY=$1
VALUE=$2
if ! command -v jq &> /dev/null; then
echo -e "ERROR\n\tjq must be installed to process JSON"
return 1
fi
if [ -z "$KEY" ]; then
echo -e "ERROR\n\tUsage: codeSettings <key> [<value>]"
return 1
fi
# Validate JSON first
jq < "$FILE" &> /dev/null
KEY_VALUE=$(jq ".\"$KEY\"" < "$FILE")
if [ "$KEY_VALUE" = "null" ]; then
echo "Key does not exist: \"$KEY\""
else
echo -e "Key exists:\n\t\"$KEY\": $KEY_VALUE"
fi
if [ "$KEY_VALUE" = "\"$VALUE\"" ]; then
echo "Key already has desired value, skipping"
elif [ "$VALUE" != "" ]; then
# Backup file (you'll have to delete backup manually)
cp "$FILE" "$FILE.bak"
echo -e "Adding value to key:\n\t\"$KEY\": \"$VALUE\""
jq ". += { \"$KEY\": \"$VALUE\" }" < "$FILE" > "$FILE.tmp"
mv "$FILE.tmp" "$FILE"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment