dotfiles/wrappers/hugo
2025-08-06 18:52:33 -06:00

234 lines
5.5 KiB
Bash
Executable file

#!/bin/sh
HUGO_GITHUB="https://github.com/gohugoio/hugo"
HUGO_DIR_PREFIX="${XDG_DATA_HOME:=$HOME/.local/share}"
HUGO_DIR="${HUGO_DIR:=$HUGO_DIR_PREFIX/hugo}"
_prompt() {
printf "[HUGO MANAGER] \033[34m%s\033[0m \033[1m[y/n]:\033[0m " "$1"
read -r opt
case "$opt" in
[Yy]) return 0;;
[Nn]) return 1;;
*) return 1;;
esac
}
_msg() {
printf "[HUGO MANAGER] \033[0m%s\033[0m\n" "$1"
}
_error() {
printf "[HUGO MANAGER] \033[31merror: %s\033[0m\n" "$1"
}
# Check if $HUGO_DIR exists, if it doesn't, create it.
# If there is an error, return 1.
_hugo_dir_check() {
if ! [ -d "$HUGO_DIR" ]; then
return 1
fi
}
# Cleans $HUGO_DIR
_clean_hugo_versions() {
if [ -d "$HUGO_DIR" ]; then
rm -rf "${HUGO_DIR:?}"/*
fi
}
# Echoes list of hugo versions.
_list_hugo_versions() {
preferred="$1"
for v in "$HUGO_DIR"/*; do
if [ -d "$v" ]; then
version="${v##*hugo-}"
if [ "$preferred" = "$version" ]; then
echo "$version*"
else
echo "$version"
fi
fi
done
}
# Echoes latest installed hugo version. e.g. 25.5.31
# If there is an error, return 1.
_latest_local() {
versions="$(_list_hugo_versions)"
for v in $versions ; do
latest_local="$v"
done
echo "$latest_local"
}
# Sets the preferred hugo version
# If there is an error, return 1.
_set_preferred_version() {
version="$1"
if [ -z "$version" ]; then
_error "provide a version to set. e.g. 25.5.31"
return 1
fi
echo "$version" > "$HUGO_DIR"/hugo_version
}
# Echoes current set hugo version. e.g. 25.5.31
# If there is an error, return 1.
_current_local() {
if [ -f "$HUGO_DIR"/hugo_version ]; then
cat "$HUGO_DIR"/hugo_version
else
return 1
fi
}
# Get the latest installed hugo binary path.
# If there is a problem with the binary, return 1.
_hugo_bin() {
preferred="$1"
if [ -z "$preferred" ]; then
preferred="$(_latest_local)"
fi
bin="$HUGO_DIR/hugo-$preferred/hugo"
if [ -x "$bin" ]; then
echo "$bin"
else
return 1
fi
}
# Echoes the latest remote available hugo version. e.g. 25.5.31
# If there is an error, return 1.
_latest_remote() {
# shellcheck disable=SC1083
release_url="$(curl -Ls -o /dev/null -w %{url_effective} "$HUGO_GITHUB"/releases/latest)"
if [ "$?" -eq 1 ]; then
return 1
fi
echo "${release_url##*/v}"
}
# Downloads a given hugo version. e.g. _download_hugo 25.5.31
# If there is an error, return 1.
_download_hugo() {
version="$1"
if [ -z "$version" ]; then
_error "provide a version to download. e.g. 25.5.31 or latest"
return 1
fi
case "$version" in
latest) version="$(_latest_remote)"
if [ "$?" -eq 1 ]; then
_error "failed get latest"
return 1
fi
;;
*) ;;
esac
target_dir="$HUGO_DIR/hugo-$version"
target="$target_dir/hugo-$version.tar.gz"
download_url="$HUGO_GITHUB/releases/download/v$version/hugo_${version}_linux-amd64.tar.gz"
mkdir -p "$target_dir"
curl -sLo "$target" "$download_url"
if [ "$?" -eq 1 ]; then
_error "failed to download"
return 1
fi
if ! [ -f "$target" ]; then
_error "failed to download"
return 1
fi
tar -C "$target_dir" -xf "$target" >/dev/null
if [ "$?" -eq 1 ]; then
_error "failed to extract"
return 1
fi
_set_preferred_version "$version"
rm -f "$target"
}
# Updates to latest remote available version.
# If there is an error, return 1.
_update_hugo() {
latest_remote="$(_latest_remote)"
if [ "$?" -eq 1 ]; then
_error "failed to look up latest version"
return 1
fi
latest_local="$(_latest_local)"
if [ "$?" -eq 1 ]; then
_error "failed to look up local version"
return 1
fi
if [ "$latest_local" != "$latest_remote" ]; then
_msg "updating hugo ${latest_local:-none} -> $latest_remote..."
_download_hugo "$latest_remote"
if [ "$?" -eq 1 ]; then
_error "failed installation"
return 1
fi
_set_preferred_version "$latest_remote"
else
_msg "already up-to-date"
fi
}
if [ -d "$HUGO_DIR" ]; then
HUGO_BIN="$(_hugo_bin "$(_current_local)")"
if [ "$?" -eq 1 ] && [ "$1" != "--manager-download" ] ; then
_prompt "no version of hugo installed, download latest?"
if [ "$?" -eq 1 ]; then
_error "no version of hugo installed, download via '--manager-download x.x.x'"
return 1
fi
_download_hugo "latest"
if [ "$?" -eq 1 ]; then
_error "error downloading latest version"
return 1
fi
fi
# shellcheck disable=SC2068
case "$1" in
--manager-update) _update_hugo ;;
--manager-download) _download_hugo "$2" ;;
--manager-list) _list_hugo_versions "$(_current_local)" ;;
--manager-set) _set_preferred_version "$2" ;;
--manager-clean) _clean_hugo_versions ;;
*) $HUGO_BIN $@ ;;
esac
else
_hugo_dir_check
if [ "$?" -eq 1 ]; then
_prompt "Installation directory '$HUGO_DIR' does not exist, create?"
if [ "$?" -eq 1 ]; then
return 1
fi
mkdir -p "$HUGO_DIR"
fi
_prompt "hugo not installed, download latest?"
if ! [ "$?" -eq 1 ]; then
_update_hugo
fi
fi