Skip to content

Instantly share code, notes, and snippets.

@fira42073
Created May 13, 2025 10:25
Show Gist options
  • Save fira42073/fa54214012bbf12549d490ce71f9d84a to your computer and use it in GitHub Desktop.
Save fira42073/fa54214012bbf12549d490ce71f9d84a to your computer and use it in GitHub Desktop.
a small script for chezmoi to install missing dependencies
# Optional colorized output (disabled if NO_COLOR is set)
if [ -z "$NO_COLOR" ]; then
COLOR_INFO="\033[1;34m" # Bright Blue
COLOR_SUCCESS="\033[1;32m" # Bright Green
COLOR_WARN="\033[1;33m" # Bright Yellow
else
COLOR_INFO=""
COLOR_SUCCESS=""
COLOR_WARN=""
fi
# ---------------------------------
# install_if_missing
# ---------------------------------
#
# Usage:
# install_if_missing \
# "<check_command>" \
# "<install_block>" \
# "<description>" \
# [<version_command>] \
# [<expected_version>] \
# [<post_install_block>]
#
# Arguments:
# 1. name: Installable name
# 2. check_command: A command or test that returns 0 if already installed.
# 3. install_block: Quoted shell commands for installation.
# 4. version_command: (Optional) Command to return current version (e.g., `go version | ...`).
# 5. expected_version: (Optional) String to compare to version_command output.
# 6. post_install_block: (Optional) Quoted shell commands to run after install.
#
# Examples:
#
# check_bash_it() { [ -d "$HOME/.bash_it/" ]; }
# install_bash_it() {
# git clone --depth=1 https://github.com/Bash-it/bash-it.git ~/.bash_it
# source $HOME/.bash_it/install.sh -sn
# rm -rf ~/.oh-my-bash
# }
# install_if_missing "oh-my-bash" check_bash_it "" "" install_bash_it ""
#
# GO_VERSION="1.24.1"
# check_go() { command -v go &>/dev/null; }
# version_go() { go version | awk '{print $3}' | sed 's/go//'; }
# install_go() {
# wget https://go.dev/dl/go$GO_VERSION.linux-amd64.tar.gz
# sudo rm -rf /usr/local/go
# sudo tar -C /usr/local -xzf go$GO_VERSION.linux-amd64.tar.gz
# rm -f go$GO_VERSION.linux-amd64.tar.gz
# go install github.com/dkorunic/betteralign/cmd/betteralign@latest
# go install github.com/yoheimuta/protolint/cmd/protolint@latest
# go install github.com/bufbuild/buf/cmd/buf@latest
# go install github.com/go-task/task/v3/cmd/task@latest
# CGO_ENABLED=1 go install -tags extended github.com/gohugoio/hugo@latest
# }
# install_if_missing "go" check_go version_go "$GO_VERSION" install_go ""
install_if_missing() {
local name="$1"
local check_func="$2"
local version_func="$3"
local expected_version="$4"
local install_func="$5"
local post_install_func="$6"
if ! $check_func; then
echo -e "${COLOR_INFO}$name not found. Installing..."
$install_func
[ -n "$post_install_func" ] && $post_install_func
elif [ -n "$expected_version" ] && [ -n "$version_func" ]; then
local actual_version="$($version_func)"
if [ "$actual_version" != "$expected_version" ]; then
echo -e "${COLOR_WARN}$name version mismatch. Reinstalling..."
$install_func
echo -e "${COLOR_SUCCESS}$name installed."
[ -n "$post_install_func" ] && $post_install_func
else
echo -e "${COLOR_SUCCESS}$name is already installed and up-to-date."
fi
else
echo -e "${COLOR_SUCCESS}$name is already installed."
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment