Skip to content

Instantly share code, notes, and snippets.

@AdamGagorik
Last active April 20, 2024 21:23
Show Gist options
  • Save AdamGagorik/327f092afb35895971a2596e4fedf29f to your computer and use it in GitHub Desktop.
Save AdamGagorik/327f092afb35895971a2596e4fedf29f to your computer and use it in GitHub Desktop.
cron script to update brew and python
#!/usr/bin/env bash
set -e
######################################################################
NOT_FOUND="EXECUTABLE NOT FOUND"
get_first_path() {
for path in "$@"; do
if [ -e "$path" ]; then
echo "$path"
return
fi
done
echo "${NOT_FOUND}"
}
declare -A LOOKUP
LOOKUP[home]=$(get_first_path "${HOME}" /Users/adam /home/adam /home/agagorik)
LOOKUP[cat]=$(get_first_path "/home/linuxbrew/.linuxbrew/bin/mdcat" "/usr/local/bin/mdcat" "/usr/bin/cat")
LOOKUP[git]=$(get_first_path "/home/linuxbrew/.linuxbrew/bin/git" "/usr/local/bin/git")
LOOKUP[apt]=$(get_first_path "/usr/bin/apt")
LOOKUP[brew]=$(get_first_path "/home/linuxbrew/.linuxbrew/bin/brew" "/usr/local/bin/brew")
LOOKUP[pyenv_root]=$(get_first_path "${LOOKUP[home]}/.pyenv")
LOOKUP[pyenv]=$(get_first_path "${LOOKUP[pyenv_root]}/libexec/pyenv")
LOOKUP[pipx]=$(get_first_path "/home/linuxbrew/.linuxbrew/bin/pipx" "/usr/local/bin/pipx")
LOOKUP[python]=$(get_first_path "${LOOKUP[pyenv_root]}/versions/3.11.8/bin/python")
LOOKUP[poetry]=$(get_first_path "${LOOKUP[home]}/.local/bin/poetry")
LOOKUP[rust]=$(get_first_path "${LOOKUP[home]}/.cargo/bin/rustup")
LOOKUP[nvm]="nvm"
######################################################################
function head() {
echo "------" | "${LOOKUP[cat]}"
echo "# $1" | "${LOOKUP[cat]}"
echo ""
}
function daeh {
echo ""
}
######################################################################
function usage() {
echo "usage: all|apt|brew|pyenv|poetry|rust|nvm"
}
if [ $# -eq 0 ]; then
usage
exit 0
fi
######################################################################
head "DATE"
/bin/date
daeh
######################################################################
head "LOOKUP"
for key in "${!LOOKUP[@]}"; do
printf "* %-10s : ${LOOKUP[$key]}\n" "${key}" | "${LOOKUP[cat]}"
done
daeh
######################################################################
function run_if_lookup_valid() {
head "$2"
if [ -z "${LOOKUP[${2}]}" ] || [ "${LOOKUP[${2}]}" = "${NOT_FOUND}" ]; then
echo "unknown command: ${2}"
else
$1
ran_something=1
fi
daeh
return 0
}
function run_if_all_or_selected_exit() {
# skip on MacOS ?
if [[ "$OSTYPE" == "darwin"* ]] && [ "${4}" = "skip-macos" ]; then
return 0
fi
# run all
if [ "${1}" = "all" ] || [ "${1}" = "${3}" ]; then
run_if_lookup_valid "${2}" "${3}"
fi
}
######################################################################
# shellcheck disable=SC2317
function cleanup() {
if [ -z "${ran_something}" ]; then
usage
echo "error: no valid commands or unknown key"
fi
}
trap cleanup EXIT
######################################################################
# shellcheck disable=SC2317
function do_update() {
sudo "${LOOKUP[apt]}" update -y && \
sudo "${LOOKUP[apt]}" upgrade -y && \
sudo "${LOOKUP[apt]}" autoremove -y && \
sudo "${LOOKUP[apt]}" clean -y && \
sudo "${LOOKUP[apt]}" autoclean -y
}
run_if_all_or_selected_exit "$1" do_update "apt" "skip-macos"
######################################################################
# shellcheck disable=SC2317
function do_update() {
"${LOOKUP[brew]}" tap --repair
"${LOOKUP[brew]}" update
"${LOOKUP[brew]}" upgrade
"${LOOKUP[brew]}" cleanup
}
run_if_all_or_selected_exit "$1" do_update "brew"
######################################################################
# shellcheck disable=SC2317
function do_update() {
local py
"${LOOKUP[git]}" -C "${LOOKUP[pyenv_root]}" pull
for py in $("${LOOKUP[pyenv]}" versions --bare); do
daeh
head "pyenv: $py"
"${LOOKUP[pyenv]}" local "$py"
"${LOOKUP[pyenv]}" exec python --version
"${LOOKUP[pyenv]}" exec pip cache purge -v
"${LOOKUP[pyenv]}" exec pip install --upgrade pip
if [ "$("${LOOKUP[pyenv]}" exec pip freeze)" ]; then
"${LOOKUP[pyenv]}" exec pip freeze | xargs "${LOOKUP[pyenv]}" exec pip uninstall -y
fi
"${LOOKUP[pyenv]}" exec pip install virtualenv --upgrade
done
}
run_if_all_or_selected_exit "$1" do_update "pyenv"
######################################################################
# shellcheck disable=SC2317
function do_update() {
if [ -z "${PIPX_REINSTALL_ALL}" ]; then
"${LOOKUP[pipx]}" upgrade-all --skip conan
else
"${LOOKUP[pipx]}" reinstall-all --python "${LOOKUP[python]}" --skip conan
fi
}
run_if_all_or_selected_exit "$1" do_update "pipx"
######################################################################
# shellcheck disable=SC2317
function do_update() {
"${LOOKUP[poetry]}" self update
}
run_if_all_or_selected_exit "$1" do_update "poetry"
######################################################################
# shellcheck disable=SC2317
function do_update() {
"${LOOKUP[rust]}" update
}
run_if_all_or_selected_exit "$1" do_update "rust"
######################################################################
# shellcheck disable=SC2317
function do_update() {
PROFILE=/dev/null bash -c 'curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash'
}
run_if_all_or_selected_exit "$1" do_update "nvm"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment