From f76eecc321236889d17a62c7d95dcfea9ab560b3 Mon Sep 17 00:00:00 2001 From: Andrey G Date: Thu, 21 Dec 2023 12:28:13 -0500 Subject: [PATCH 1/3] Create bootstrap-source Add example of bootstrap that can be used to run interactive shell scripts --- contrib/bootstrap/bootstrap-source | 89 ++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 contrib/bootstrap/bootstrap-source diff --git a/contrib/bootstrap/bootstrap-source b/contrib/bootstrap/bootstrap-source new file mode 100644 index 0000000..dee0b66 --- /dev/null +++ b/contrib/bootstrap/bootstrap-source @@ -0,0 +1,89 @@ +#!/bin/bash + +# Save this file as ~/.config/yadm/bootstrap and make it executable. It expects +# environment variable `source` with a name of shell script to execute. +# `source` can be relative to ~/.config/yadm/ or full path. +# +# Usage: +# source=install yadm bootstrap +# or +# source=~/.config/yadm/install yadm bootstrap +# +# where `~/.config/yadm/install` can be like this: +# +# [[ ! $(type -t install) = 'function' ]] && echo "Usage: source=$(basename "$0") yadm bootstrap" && exit 1 +# +# confirm yes 'softwareupdate --agree-to-license --install --all' "$(info 'Install ' warning 'OSX updates')" +# install 'brew' '/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"' +# +# brew bundle --global +# + +TEXT_COLOR_RED=$(tput setaf 1) +TEXT_COLOR_GREEN=$(tput setaf 2) +TEXT_COLOR_BLUE=$(tput setaf 4) +TEXT_COLOR_YELLOW=$(tput setaf 3) +TEXT_DEFAULT=$(tput sgr0) + +info() { + printf "${TEXT_COLOR_BLUE}%s${TEXT_DEFAULT}" "$1"; "${@:2}" +} + +error() { + printf "${TEXT_COLOR_RED}%s${TEXT_DEFAULT}" "$1"; "${@:2}" +} + +success() { + printf "${TEXT_COLOR_GREEN}%s${TEXT_DEFAULT}" "$1"; "${@:2}" +} + +warning() { + printf "${TEXT_COLOR_YELLOW}%s${TEXT_DEFAULT}" "$1"; "${@:2}" +} + +source_if_exists() { + if [[ -f $1 ]]; then source "$1"; else return 1; fi +} + +command_exists () { + command -v "$1" &> /dev/null +} + +# install a 'command' via 'code' if it was not previously installed yet +install() { + local command=$1 + local code=$2 + local message=$(info 'Installing ' warning "$command") + + if command_exists "$command"; then + echo "$message...[$(success 'already')]" + else + echo "$message..." + eval "$code" + fi +} + +# execute a 'code' after confirmation from user +confirm() { + local default_answer="${1:-no}" + local code=$2 + local message=$3 + local options answer + + if [[ "${default_answer:0:1}" == y* ]]; then + options="$(success 'YES')/no" + else + options="yes/$(success 'NO')" + fi + + read -r -p "${message} [${options}]:" answer + answer=${answer:-$default_answer} + + if [[ "${answer:0:1}" == y* ]]; then + eval "$code" + fi +} + +source_if_exists "$(dirname "${0}")/$source" || +source_if_exists $source || +([[ -z $source ]] && echo "Usage: source=FILE yadm bootstrap" || echo "Can't locate file '$source' for bootstrapping") From 5a2ed6c68de3efe823ea3ed7ae59fda76aeffc60 Mon Sep 17 00:00:00 2001 From: Andrey G Date: Thu, 21 Dec 2023 12:40:46 -0500 Subject: [PATCH 2/3] linting --- contrib/bootstrap/bootstrap-source | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/contrib/bootstrap/bootstrap-source b/contrib/bootstrap/bootstrap-source index dee0b66..a37f81c 100644 --- a/contrib/bootstrap/bootstrap-source +++ b/contrib/bootstrap/bootstrap-source @@ -5,9 +5,9 @@ # `source` can be relative to ~/.config/yadm/ or full path. # # Usage: -# source=install yadm bootstrap +# source=install yadm bootstrap # or -# source=~/.config/yadm/install yadm bootstrap +# source=~/.config/yadm/install yadm bootstrap # # where `~/.config/yadm/install` can be like this: # @@ -26,27 +26,27 @@ TEXT_COLOR_YELLOW=$(tput setaf 3) TEXT_DEFAULT=$(tput sgr0) info() { - printf "${TEXT_COLOR_BLUE}%s${TEXT_DEFAULT}" "$1"; "${@:2}" + printf "${TEXT_COLOR_BLUE}%s${TEXT_DEFAULT}" "$1"; "${@:2}" } error() { - printf "${TEXT_COLOR_RED}%s${TEXT_DEFAULT}" "$1"; "${@:2}" + printf "${TEXT_COLOR_RED}%s${TEXT_DEFAULT}" "$1"; "${@:2}" } success() { - printf "${TEXT_COLOR_GREEN}%s${TEXT_DEFAULT}" "$1"; "${@:2}" + printf "${TEXT_COLOR_GREEN}%s${TEXT_DEFAULT}" "$1"; "${@:2}" } warning() { - printf "${TEXT_COLOR_YELLOW}%s${TEXT_DEFAULT}" "$1"; "${@:2}" + printf "${TEXT_COLOR_YELLOW}%s${TEXT_DEFAULT}" "$1"; "${@:2}" } source_if_exists() { - if [[ -f $1 ]]; then source "$1"; else return 1; fi + if [[ -f $1 ]]; then source "$1"; else return 1; fi } command_exists () { - command -v "$1" &> /dev/null + command -v "$1" &> /dev/null } # install a 'command' via 'code' if it was not previously installed yet @@ -68,13 +68,13 @@ confirm() { local default_answer="${1:-no}" local code=$2 local message=$3 - local options answer + local options answer if [[ "${default_answer:0:1}" == y* ]]; then - options="$(success 'YES')/no" - else - options="yes/$(success 'NO')" - fi + options="$(success 'YES')/no" + else + options="yes/$(success 'NO')" + fi read -r -p "${message} [${options}]:" answer answer=${answer:-$default_answer} From fdbb9cfe269207bd1894bae7f8b17c6f43b1d49f Mon Sep 17 00:00:00 2001 From: Andrey G Date: Thu, 21 Dec 2023 21:05:50 -0500 Subject: [PATCH 3/3] added verbose mode for `source_if_exists` and also expecting `file` instead of `source` as env. variable to make that possible without dancing around: ``` #!/bin/bash file=backup && yadm bootstrap; file=install && yadm bootstrap; file=configure &&yadm bootstrap; ``` --- contrib/bootstrap/bootstrap-source | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/contrib/bootstrap/bootstrap-source b/contrib/bootstrap/bootstrap-source index a37f81c..58b2d0e 100644 --- a/contrib/bootstrap/bootstrap-source +++ b/contrib/bootstrap/bootstrap-source @@ -1,17 +1,17 @@ #!/bin/bash # Save this file as ~/.config/yadm/bootstrap and make it executable. It expects -# environment variable `source` with a name of shell script to execute. -# `source` can be relative to ~/.config/yadm/ or full path. +# environment variable `file` with a name of shell script to execute. +# `file` can be relative to ~/.config/yadm/ or has full path. # # Usage: -# source=install yadm bootstrap +# file=install yadm bootstrap # or -# source=~/.config/yadm/install yadm bootstrap +# file=~/.config/yadm/install yadm bootstrap # # where `~/.config/yadm/install` can be like this: # -# [[ ! $(type -t install) = 'function' ]] && echo "Usage: source=$(basename "$0") yadm bootstrap" && exit 1 +# [[ ! $(type -t install) = 'function' ]] && echo "Usage: file=$(basename "$0") yadm bootstrap" && exit 1 # # confirm yes 'softwareupdate --agree-to-license --install --all' "$(info 'Install ' warning 'OSX updates')" # install 'brew' '/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"' @@ -42,7 +42,12 @@ warning() { } source_if_exists() { - if [[ -f $1 ]]; then source "$1"; else return 1; fi + local src=$1 + local verbose="${2:-no}" + + [[ ! -f $src ]] && return 1; + [[ "${verbose:0:1}" == v* ]] && printf "%s\n\n" "$(info "-> $src")"; + source "$src" } command_exists () { @@ -84,6 +89,6 @@ confirm() { fi } -source_if_exists "$(dirname "${0}")/$source" || -source_if_exists $source || -([[ -z $source ]] && echo "Usage: source=FILE yadm bootstrap" || echo "Can't locate file '$source' for bootstrapping") +source_if_exists "$(dirname "${0}")/$file" v || +source_if_exists $file v || +([[ -z $file ]] && echo "Usage: file=FILENAME yadm bootstrap" || echo "Can't locate file '$file' for bootstrapping")