Last active
October 27, 2023 04:31
-
-
Save Nezteb/9eb91294e0ad8dc12fc03f6863023e09 to your computer and use it in GitHub Desktop.
Get and maybe update a VS Code setting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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