Skip to content

Instantly share code, notes, and snippets.

@knu
Last active March 1, 2025 09:04
Show Gist options
  • Save knu/86c3d1a2c4cbcf02b1d6306fdffd8ded to your computer and use it in GitHub Desktop.
Save knu/86c3d1a2c4cbcf02b1d6306fdffd8ded to your computer and use it in GitHub Desktop.
Zero-downtime mise upgrade
#!/bin/sh
set -e
config=$HOME/.config/mise/config.toml
config_bak=${config%.toml}.bak.toml
while getopts "f:" opt; do
case $opt in
f)
config=$OPTARG
;;
\?)
echo "Usage: $0 [-c config] [tool...]"
exit 1
;;
esac
done
shift $((OPTIND - 1))
if [ -f "$config_bak" ]; then
echo "$config_bak exists; upgrade seems to be in progress."
exit 1
fi
cd "$(dirname "$config")"
# Get outdated tools.
outdated=$(mise outdated --json "$@")
if [ "$outdated" = '{}' ]; then
echo Nothing to upgrade.
exit
fi
# Pin the tools to the currently installed versions while new versions are being installed.
cp -p "$config" "$config_bak"
mise use --path "$config" \
$(printf %s "$outdated" | jq -r 'to_entries | map("\(.key)@\(.value.current // .value.latest)") | .[]')
# Install the latest versions.
mise install \
$(printf %s "$outdated" | jq -r 'to_entries | map("\(.key)@\(.value.latest)") | .[]')
mv "$config_bak" "$config"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment