oh-my-posh/website/static/install.sh

194 lines
5.2 KiB
Bash
Raw Normal View History

2023-05-10 08:55:36 -07:00
#!/usr/bin/env sh
install_dir=""
error() {
printf "\x1b[31m$1\e[0m\n"
exit 1
}
info() {
printf "$1\n"
}
warn() {
printf "⚠️ \x1b[33m$1\e[0m\n"
}
2023-05-10 08:55:36 -07:00
help() {
# Display Help
echo "Installs Oh My Posh"
echo
echo "Syntax: install.sh [-h|d]"
echo "options:"
echo "h Print this Help."
echo "d Specify the installation directory. Defaults to /usr/local/bin or the directory where oh-my-posh is installed."
echo
}
while getopts ":hd:" option; do
case $option in
h) # display Help
help
exit;;
d) # Enter a name
install_dir=$OPTARG;;
\?) # Invalid option
echo "Invalid option command line option. Use -h for help."
exit 1
esac
done
SUPPORTED_TARGETS="linux-amd64 linux-arm linux-arm64 darwin-amd64 darwin-arm64"
validate_dependency() {
if ! command -v $1 >/dev/null; then
error "$1 is required to install Oh My Posh. Please install $1 and try again.\n"
2023-05-10 08:55:36 -07:00
fi
}
validate_dependencies() {
validate_dependency curl
validate_dependency unzip
validate_dependency realpath
validate_dependency dirname
}
set_install_directory() {
if [ -n "$install_dir" ]; then
# expand directory
install_dir="${install_dir/#\~/$HOME}"
return 0
fi
# check if we have oh-my-posh installed, if so, use the executable directory
# to install into and follow symlinks
if command -v oh-my-posh >/dev/null; then
posh_dir=$(command -v oh-my-posh)
real_dir=$(realpath $posh_dir)
install_dir=$(dirname $real_dir)
info "Oh My Posh is already installed, updating existing installation in:"
info " ${install_dir}"
2023-05-10 08:55:36 -07:00
else
install_dir="/usr/local/bin"
fi
}
validate_install_directory() {
if [ ! -d "$install_dir" ]; then
error "Directory ${install_dir} does not exist, set a different directory and try again."
2023-05-10 08:55:36 -07:00
fi
# check if we can write to the install directory
if [ ! -w $install_dir ]; then
error "Cannot write to ${install_dir}. Please run the script with sudo and try again:\n curl -s https://ohmyposh.dev/install.sh | sudo bash -s\n\nAlternatively, you can set a different directory and try again: \n curl -s https://ohmyposh.dev/install.sh | bash -s -- -d {directory}"
fi
# check if the directory is in the PATH
good=$(
IFS=:
for path in $PATH; do
if [ "${path%/}" = "${install_dir}" ]; then
printf 1
break
fi
done
)
if [ "${good}" != "1" ]; then
warn "Installation directory ${install_dir} is not in your \$PATH"
2023-05-10 08:55:36 -07:00
fi
}
install() {
arch=$(detect_arch)
platform=$(detect_platform)
target="${platform}-${arch}"
good=$(
IFS=" "
for t in $SUPPORTED_TARGETS; do
if [ "${t}" = "${target}" ]; then
printf 1
break
fi
done
)
if [ "${good}" != "1" ]; then
error "${arch} builds for ${platform} are not available for Oh My Posh"
2023-05-10 08:55:36 -07:00
fi
info "\n Installing oh-my-posh for ${target} in ${install_dir}"
2023-05-10 08:55:36 -07:00
executable=${install_dir}/oh-my-posh
url=https://github.com/JanDeDobbeleer/oh-my-posh/releases/latest/download/posh-${target}
info "⬇️ Downloading oh-my-posh from ${url}"
2023-05-10 08:55:36 -07:00
http_response=$(curl -s -f -L $url -o $executable -w "%{http_code}")
if [ $http_response != "200" ]; then
error "Unable to download executable at ${url}\nPlease validate your connection and/or proxy settings"
fi
2023-05-10 08:55:36 -07:00
chmod +x $executable
# install themes in cache
cache_dir=$($executable cache path)
2023-05-10 08:55:36 -07:00
info "🎨 Installing oh-my-posh themes in ${cache_dir}/themes\n"
2023-05-10 08:55:36 -07:00
theme_dir="${cache_dir}/themes"
url="https://github.com/JanDeDobbeleer/oh-my-posh/releases/latest/download/themes.zip"
2023-05-10 08:55:36 -07:00
mkdir -p $theme_dir
http_response=$(curl -s -f -L $url -o ${cache_dir}/themes.zip -w "%{http_code}")
if [ $http_response == "200" ]; then
unzip -o -q ${cache_dir}/themes.zip -d $theme_dir
chmod u+rw ${theme_dir}/*.omp.*
rm ${cache_dir}/themes.zip
else
warn "Unable to download themes at ${url}\nPlease validate your connection and/or proxy settings"
fi
2023-05-10 08:55:36 -07:00
info "🚀 Installation complete.\n\nYou can follow the instructions at https://ohmyposh.dev/docs/installation/prompt"
info "to setup your shell to use oh-my-posh."
if [ $http_response == "200" ]; then
info "\nIf you want to use a built-in theme, you can find them in the ${theme_dir} directory:"
info " oh-my-posh init {shell} --config ${theme_dir}/{theme}.omp.json\n"
fi
2023-05-10 08:55:36 -07:00
}
detect_arch() {
arch="$(uname -m | tr '[:upper:]' '[:lower:]')"
case "${arch}" in
x86_64) arch="amd64" ;;
2023-05-10 08:55:36 -07:00
armv*) arch="arm" ;;
arm64) arch="arm64" ;;
aarch64) arch="arm64" ;;
2023-05-10 08:55:36 -07:00
esac
if [ "${arch}" = "arm64" ] && [ "$(getconf LONG_BIT)" -eq 32 ]; then
arch=arm
fi
printf '%s' "${arch}"
}
detect_platform() {
platform="$(uname -s | awk '{print tolower($0)}')"
2023-05-10 08:55:36 -07:00
case "${platform}" in
linux) platform="linux" ;;
darwin) platform="darwin" ;;
esac
printf '%s' "${platform}"
}
validate_dependencies
set_install_directory
validate_install_directory
install