#!/bin/bash # Safety measures set -o errexit # Leave immediately if a command returns an error set -o nounset # Leave immediately if an unitialized value is used set -o pipefail # Leave immediately if a command fails in a pipe ##################################################################### # Parameters ##################################################################### DOTFILES_REPO="git@github.com:yannrouillard/dotfiles.git" SSH_KEY_PATH="${HOME}/.ssh/id_ed25519" SSH_KEY_NAME="$(scutil --get LocalHostName)" ACHILLES_GITLAB_HOST="git.achilles.systems" ##################################################################### # Package Helper functions ##################################################################### function brew_pkg() { local package="$1" option="${2:-}" brew ls --versions "${package}" >/dev/null || brew install "${package}" [[ ${option} != "--link" ]] || brew link --overwrite "${package}" } function brew_cask_pkg() { local package="$1" brew ls --cask --versions "${package}" >/dev/null || brew install --cask "${package}" } function mas_pkg() { local package="$1" local package_id package_id=$(mas search "${package}" | grep " ${package}" | awk '{ print $1 }' || true) [[ -n "${package_id}" ]] || return 1 mas list | grep "^ ${package_id} " >/dev/null || mas install "${package_id}" } ##################################################################### # Mac Helper functions ##################################################################### function is_mac_m1() { [[ "$(uname -m)" == "arm64" ]] } function is_rosetta_installed() { [[ -n "$(pkgutil --files com.apple.pkg.RosettaUpdateAuto)" ]] } function binary_exist() { local binary="$1" command -v "${binary}" &>/dev/null || return 1 } function user_shell() { local requested_shell="$1" local current_user_shell current_user_shell=$(sudo dscl . -read "/Users/${USER}" UserShell | awk '{ print $2 }') [[ "${requested_shell}" != "${current_user_shell}" ]] || return 0 sudo dscl . -create "/Users/${USER}" UserShell "${requested_shell}" } function install_brew() { binary_exist brew || /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" } ##################################################################### # Git Helper functions ##################################################################### function is_git_folder() { [[ -d "${target_folder}" ]] || return 1 (cd "${target_folder}" && git rev-parse --git-dir 2>/dev/null) || return 1 } function git_source() { local git_url="$1" target_folder="$2" ! is_git_folder || return 0 mkdir -p "${target_folder}" git clone "${git_url}" "${target_folder}" } function has_github_access() { gh auth status --hostname github.com >/dev/null } function configure_github_access() { gh config set git_protocol ssh gh config set prompt disabled gh auth login --scopes "read:public_key" --web gh config set prompt enabled } function has_github_ssh_key() { local key_name="$1" gh ssh-key list | grep --quiet "${key_name}" } function configure_github_ssh_key() { local key_name="$1" key_path="$2" gh auth refresh --scopes "write:public_key" --hostname github.com gh ssh-key add --title "${key_name}" "${key_path}" gh auth refresh --scopes "read:public_key" --hostname github.com } function has_gitlab_access() { glab auth status --hostname git.achilles.systems >/dev/null } function configure_gitlab_access() { glab auth login --hostname git.achilles.systems } function has_gitlab_ssh_key() { local key_name="$1" GITLAB_HOST="${ACHILLES_GITLAB_HOST}" glab ssh-key list | grep --quiet "${SSH_KEY_NAME}" } function configure_gitlab_ssh_key() { local key_name="$1" key_path="$2" glab ssh-key add --title "${key_name}" "${key_path}" } function has_ssh_config() { [[ -f .ssh/config ]] } function create_temporary_ssh_config() { cat >.ssh/config </dev/null || yadm clone "${DOTFILES_REPO}" } ##################################################################### # Main code ##################################################################### available_targets=("cli_env" "mac_desktop" "devel_common") [[ -z "$*" ]] || available_targets=("$@") for target in "${available_targets[@]}"; do eval "bundle_${target}" done