Skip to content

Instantly share code, notes, and snippets.

@Mixerou
Last active July 30, 2025 12:09
Show Gist options
  • Select an option

  • Save Mixerou/2ccefad2a93a0c212205c0ccb605f2e0 to your computer and use it in GitHub Desktop.

Select an option

Save Mixerou/2ccefad2a93a0c212205c0ccb605f2e0 to your computer and use it in GitHub Desktop.
JetBrains Cloud Scratch Files Linker
#!/usr/bin/env bash
# Error Codes
# 1 — Platform is not supported
# 2 — No IDEs have been installed
# 3 — Location for cloud scratches does not exist
set -eu +x
PLATFORM=$(uname -s)
if [[ $PLATFORM == "Darwin" ]]; then
IDES_LOCATION="$HOME/Library/Application Support/JetBrains"
elif [[ $PLATFORM == "Linux" ]]; then
IDES_LOCATION="$HOME/.config/JetBrains"
else
echo "ERROR: $PLATFORM platform is not supported" >&2
exit 1
fi
if [[ ! -d $IDES_LOCATION ]]; then
echo "ERROR: At least one IDE must be installed" >&2
exit 2
fi
link() {
declare -a IDES=()
EXCLUDED_IDES_REGEX=".*\/(CLionNova|FleetBackend|GitClient|JetBrainsGateway).*$"
if [[ $PLATFORM == "Darwin" ]]; then
RAW_IDES=( find -E
"$IDES_LOCATION"
-type d
-d 1
! -regex "$EXCLUDED_IDES_REGEX"
-regex ".*\/[A-z]+([0-9]|\.)+$"
-print0 )
elif [[ $PLATFORM == "Linux" ]]; then
RAW_IDES=( find "$IDES_LOCATION"
-type d
-d 1
-regextype posix-extended
! -regex "$EXCLUDED_IDES_REGEX"
-regex '.*\/[A-z]+([0-9]|\.)+$'
-print0 )
else
echo "ERROR: $PLATFORM platform is not supported" >&2
exit 1
fi
while IFS= read -r -d '' dir; do
IDES+=( "$(basename "$dir")" )
done < <("${RAW_IDES[@]}")
if [[ -z ${CLOUD_SCRATCHES_LOCATION:-} ]]; then
# Here you can change the fallback
CLOUD_SCRATCHES_LOCATION="$HOME/cloud/scratches"
fi
if [[ ! -d $CLOUD_SCRATCHES_LOCATION ]]; then
printf "ERROR: Unable to verify the existence of cloud scratches location: %s\n\n" "$CLOUD_SCRATCHES_LOCATION" >&2
echo "Did you specify it correctly in the CLOUD_SCRATCHES_LOCATION environment variable, if at all?"
echo "Did you specify it correctly in the script as a fallback, if at all?"
echo "Does your terminal have access to this location?"
exit 3
fi
for IDE in "${IDES[@]}"; do
IDE_LOCATION="$IDES_LOCATION/$IDE"
IDE_SCRATCHES_LOCATION="$IDE_LOCATION/scratches"
IDE_CLOUD_SCRATCHES_LOCATION="$IDE_SCRATCHES_LOCATION/cloud"
if [[ ! -d $IDE_LOCATION ]]; then
echo "Location of $IDE not found"
continue
fi
if [[ -d $IDE_CLOUD_SCRATCHES_LOCATION && -L $IDE_CLOUD_SCRATCHES_LOCATION ]]; then
echo "Cloud scratches already linked for $IDE"
continue
fi
if [[ -d $IDE_CLOUD_SCRATCHES_LOCATION && ! -L $IDE_CLOUD_SCRATCHES_LOCATION ]]; then
echo "Cloud scratches directory exists, but is not linked for $IDE"
continue
fi
mkdir -p "$IDE_SCRATCHES_LOCATION"
ln -s "$CLOUD_SCRATCHES_LOCATION" "$IDE_CLOUD_SCRATCHES_LOCATION"
echo "Cloud scratches linked for $IDE"
done
}
unlink() {
echo "Unlinking..."
find "$IDES_LOCATION" -type l -d 3 -name "cloud" -print -exec rm -rf {} + -depth
}
output_help() {
echo "JetBrains Cloud Scratch Files Linker
Options:
-h, --help Print this message
Commands:
link Create a symbolic link to the remote directory with cloud scratches.
You need to specify the CLOUD_SCRATCHES_LOCATION environment variable
or edit the fallback value in the script so you don't have to mess around the next time you use it.
unlink Remove a symbolic link to the remote directory with cloud scratches
help Print this message"
}
if [ "$#" -lt 1 ]; then
output_help
elif [[ $1 == "link" ]]; then
link
elif [[ $1 == "unlink" ]]; then
unlink
else
output_help
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment