Files
dotfiles/local/bin/dfm

188 lines
5.1 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# Dotfiles manager and install helper
# (c) Ismo Vuorinen <https://github.com/ivuorinen> 2022
# Licensed under MIT, see LICENSE
#
# shellcheck source-path=$HOME/.dotfiles/local/bin
#
# Helper variables, override with ENVs like `VERBOSE=1 dfm help`
: "${VERBOSE:=0}"
: "${DOTFILES:=$HOME/.dotfiles}"
: "${INSTALL_SCRIPT:=$DOTFILES/scripts/install-dotfiles.sh}"
: "${BREWFILE:=$DOTFILES/Brewfile}"
SCRIPT=$(basename "$0")
source "$HOME/.dotfiles/scripts/shared.sh"
function section_install
{
USAGE_PREFIX="$SCRIPT install"
case "$1" in
all)
$0 install antigen && msg_done "New antigen installed"
$0 install macos && msg_done "macOS Defaults set"
$0 install brew && msg_done "Brewfile defined apps has been installed"
$0 install ext_gh && msg_done "Extensions for GitHub CLI have been installed"
$0 install ext_go && msg_done "Go packages have been installed"
;;
antigen)
curl -L git.io/antigen > "$DOTFILES/local/bin/antigen.zsh" && msg_done "🎉 Done!"
;;
brew)
brew bundle install --file="$BREWFILE" && echo "🎉 Done!"
;;
macos)
bash "$DOTFILES/scripts/set-macos-defaults.sh" && msg_done "🎉 Done!"
;;
ext_go)
bash "$DOTFILES/scripts/install-go-packages.sh" && msg_done "🎉 Done!"
;;
ext_gh)
bash "$DOTFILES/scripts/install-gh-extensions.sh" && msg_done "🎉 Done!"
;;
settler)
bash "$DOTFILES/scripts/settler.sh" && msg_done "🎉 Done!"
;;
*)
menu_section "$USAGE_PREFIX" "all | antigen | brew | macos | ext_go | ext_gh | settler"
menu_item "all" "Installs antigen, macos, brew, ext_gh and ext_go"
menu_item "antigen" "Updates the antigen.zsh file"
menu_item "brew" "Install Brewfile contents"
menu_item "ext_gh" "Install GitHub CLI Extensions"
menu_item "ext_go" "Install Go Packages"
menu_item "macos" "Setup nice macOS defaults"
menu_item "settler" "Runs the WIP settler.sh"
;;
esac
}
function section_brew
{
USAGE_PREFIX="$SCRIPT brew"
if ! command -v brew &> /dev/null; then
echo "brew could not be found, please install it first"
exit
fi
case "$1" in
install)
brew bundle install --file="$BREWFILE" && echo "🎉 Done!"
;;
update)
brew update && brew outdated && brew upgrade && brew cleanup
echo "🎉 Done!"
;;
updatebundle)
# Updates .dotfiles/Brewfile with descriptions
brew bundle dump \
--force \
--file="$BREWFILE" \
--describe && msg_ok "🎉 Done!"
;;
autoupdate)
brew autoupdate start 43200 --upgrade --cleanup --immediate
;;
*)
menu_section "$USAGE_PREFIX" "install | update | updatebundle | autoupdate"
menu_item "install" "Installs items defined in Brewfile"
menu_item "update" "Updates and upgrades brew packages"
menu_item "updatebundle" "Updates Brewfile with descriptions"
menu_item "autoupdate" "Setups brew auto-update and runs it immediately"
;;
esac
}
function section_dotfiles
{
USAGE_PREFIX="$SCRIPT dotfiles"
case "$1" in
link)
rcup -B "$HOSTNAME" -f -v && echo "🎉 Done!"
;;
update)
# Updates .dotfiles/scripts/install.sh and formats it
rcup -B 0 -g \
| tee "$INSTALL_SCRIPT" 1> /dev/null \
&& sed -i '' "s|$HOME|\$HOME|g" "$INSTALL_SCRIPT" \
&& sed -i '' "s|install.sh|$(basename $INSTALL_SCRIPT)|g" "$INSTALL_SCRIPT"
$0 dotfiles shfmt
echo "🎉 Done!"
;;
shfmt)
# Format shell scripts according to following rules.
shfmt \
--find \
--list \
--write \
--diff \
--simplify \
--language-dialect bash \
--indent 2 \
--binary-next-line \
--case-indent \
--space-redirects \
--func-next-line \
"$DOTFILES"
;;
*)
menu_section "$USAGE_PREFIX" "link | update | shfmt"
menu_item "link" "Use rcrc to update dotfile links"
menu_item "update" "Updates dotfile links, installs host specific overrides automatically"
menu_item "shfmt" "Run shfmt to all dotfiles"
;;
esac
}
# Secret menu for visual tests
function section_tests
{
USAGE_PREFIX="$SCRIPT tests"
case "$1" in
msg)
msg "msg"
msg_done "msg_done"
msg_prompt "msg_prompt"
msg_prompt_done "msg_prompt_done"
msg_nested "msg_nested"
msg_nested_done "msg_nested_done"
msg_run "msg_run" "second_param"
msg_ok "msg_ok"
msg_warn "msg_warn"
msg_err "msg_err"
;;
*)
menu_section "$USAGE_PREFIX" "msg"
menu_item "msg" "List all log functions from shared.sh"
;;
esac
}
function usage
{
echo ""
menu_section "Usage: $SCRIPT" "install | brew | dotfiles"
echo $" All commands have their own subcommands."
echo ""
section_install
echo ""
section_brew
echo ""
section_dotfiles
}
# The main loop. first keyword after $0 triggers section, or help.
case "$1" in
install) section_install "$2" ;;
brew) section_brew "$2" ;;
dotfiles) section_dotfiles "$2" ;;
tests) section_tests "$2" ;;
*) usage && exit 1 ;;
esac