bootstrap/bootstrap-macOS.sh

83 lines
2.0 KiB
Bash
Executable File

#!/bin/bash
set -e
show_usage() {
echo "usage: $0 [-h] [-y]"
}
show_help() {
show_usage
echo
echo "Bootstrap a macOS instance with:"
echo
echo "* Install xcode command line developer tools"
echo "* Install homebrew"
echo "* Update packages"
echo "* Install python, pip"
echo "* Install ansible"
echo "* Clone configuration repository"
echo "* Install 1password"
echo
echo "If any already exist they will not be reinstalled."
echo
echo "optional arguments:"
echo " -h show this help message and exit"
echo " -y assume yes when prompted"
}
yes=0
while getopts 'hy' opt; do
case $opt in
h) show_help; exit 0 ;;
y) yes=1 ;;
*) show_usage; exit 1 ;;
esac
done
missing() {
which $1 &> /dev/null && return 1 || return 0
}
agree() {
local check=^[Nn]$
[ $yes -eq 1 ] && [[ ! "$2" =~ $check ]] && return 0
[[ "$2" =~ $check ]] && local default="[y/N]" || local default="[Y/n]"
read -p "$1 $default? " answer
case "$answer" in
y|Y|yes) return 0 ;;
n|N|no) return 1 ;;
'') [[ "$2" =~ $check ]] && return 1 || return 0 ;;
*) echo "invalid input: $answer" && return `agree "$1"` ;;
esac
}
export PATH=~/.local/bin:$PATH
export PYTHONUSERBASE=~/.local
if ! xcode-select --print-path &> /dev/null; then
agree "Install Xcode command line developer tools" && xcode-select --install
fi
missing brew && agree "Install homebrew" && /usr/bin/ruby -e \
"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
agree "Update homebrew packages" && brew update
brew install python
# TODO: Update PATH for pip?
echo "Install ansible"
pip install --user --break-system-packages ansible jmespath
if [ ! -d ~/.config/local ]; then
echo "Clone configuration repository"
git clone https://git.infektor.net/config/local.git ~/.config/local
fi
agree "Install 1password" && \
~/.local/bin/ansible-playbook ~/.config/local/playbooks/1password.yaml
[ -f $0 ] && agree "Remove $0" "N" && rm $0 || exit 0