From 6b70a2a15d7d795f6140c0d1e5bb23d6706e9379 Mon Sep 17 00:00:00 2001 From: xtreme-steve-elliott Date: Fri, 3 Sep 2021 17:31:07 -0400 Subject: [PATCH] Adding interactive setup menu and updating readme --- README.md | 8 ++++ setup.sh | 134 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 134 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 6d59e789..04d9f71c 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,12 @@ To install any of these, add them as arguments to `$> setup.sh`. Examples: ./setup.sh golang docker kubernetes cloud-foundry terraform concourse ``` +The tool also supports a menu for selecting which packages you'd like to include. Specifying no opt-ins will present you with the selection menu instead: + +``` +./setup.sh +``` + ## Analytics The tool will send anonymous user data to our Google Analytics account, so we can see what command line arguments are popular. You can disable this: @@ -91,6 +97,8 @@ If you're having problems using the setup script, please let us know by [opening If you see errors from `brew`, try running `brew doctor` and include the diagnostic output in your issue submission. +Using the `--help` or `-h` flags will also present you with a help page for the command syntax. + ## Customizing If you'd like to customize this project for a project's use: diff --git a/setup.sh b/setup.sh index e5ae7e69..866dfac2 100755 --- a/setup.sh +++ b/setup.sh @@ -17,15 +17,133 @@ sudo -K sudo true; clear +LIGHT_CYAN="\033[1;36m" +GREEN="\033[0;32m" +RED="\033[0;31m" +NO_COLOUR="\033[0m" +UNDERLINE="\033[4m" +NO_STYLE=$NO_COLOUR +bold=$(tput bold) +normal=$(tput sgr0) + MY_DIR="$(dirname "$0")" -SKIP_ANALYTICS=${SKIP_ANALYTICS:-0} -if (( SKIP_ANALYTICS == 0 )); then - clientID=$(od -vAn -N4 -tx < /dev/urandom) - source ${MY_DIR}/scripts/helpers/google-analytics.sh ${clientID} start $@ +OPT_IN_FOLDER="${MY_DIR}/scripts/opt-in" + +SELECTED_OPT_INS=() + +present_options () { + shopt -s nullglob + local optionsArray=($OPT_IN_FOLDER/*) + shopt -u nullglob # Turn off nullglob to make sure it doesn't interfere with anything later + + local optionNamesArray=() + for index in ${!optionsArray[@]}; do + optionNamesArray+=(`echo ${optionsArray[index]} | sed -e "s:\.sh$::" -e "s:${OPT_IN_FOLDER}/::"`) + done + + while true; do + clear + echo "Available Opt-Ins:" + for index in ${!optionsArray[@]}; do + local offsetIndex=$(expr $index + 1) + printf "%d.\t${LIGHT_CYAN}%s${NO_COLOUR}\n" ${offsetIndex} ${optionNamesArray[index]} + done + IFS=',' + local chosenOptInsString chosenOptIns + read -p "Selected (comma-separated):" chosenOptInsString + read -a chosenOptIns <<< "$chosenOptInsString" + echo "---------------------------------------------------" + echo "You have selected the following Opt-Ins to install:" + for index in ${chosenOptIns[@]}; do + local optionName=${optionNamesArray[index - 1]} + printf "${GREEN}%s${NO_COLOUR}\n" $optionName + SELECTED_OPT_INS+=($optionName) + done + local optInsAreCorrect + read -p "Are these correct (y/n)? " optInsAreCorrect + if [ "$optInsAreCorrect" == "y" ]; then + return 0; + else + SELECTED_OPT_INS=() + fi + done +} + +prompt_for_options () { + local wantToInstallOptIns + read -p "Would you like to install opt-ins (y/n)? " wantToInstallOptIns + if [ "$wantToInstallOptIns" != "y" ]; then + return 0 + fi + present_options +} + +has_param () { + local terms="$1" + shift + for term in $terms; do + for arg; do + if [[ $arg == "$term" ]]; then + return 0 + fi + done + done + return 1 +} + +# If there are no args +if [ -z $@ ]; then + prompt_for_options +elif has_param "-h --help" "$@"; then + printf "\n${bold}NAME${normal}\n" + printf "\t${0} - install commonly needed workstation tools\n" + printf "\n${bold}SYNOPSIS${normal}\n" + printf "\t${bold}${0}${normal} [${UNDERLINE}FLAG${NO_STYLE}]... [${UNDERLINE}OPT-IN${NO_STYLE}]...\n" + printf "\n${bold}DESCRIPTION${normal}\n" + printf "\tSetup behaves in two different ways depending on whether any OPT-INs have been passed in. Passing in OPT-INS will be referred to as ${bold}PASSIVE${normal} mode and passing ${UNDERLINE}no${NO_STYLE} OPT-INS will be referred to as ${bold}INTERACTIVE${normal} mode. Both modes also take in a small set of optional FLAGs:\n" + printf "\n\t${bold}-h${normal}, ${bold}--help${normal}\n" + printf "\t\tdisplay this help and exit\n" + printf "\n\t${bold}--skip-analytics${normal}\n" + printf "\t\tskips google analytics statistics capture and upload\n" + printf "\t\t${bold}Note${normal}: If not set, the setup will default to whether the environment variable ${bold}SKIP_ANALYTICS${normal} is set. If the environment variable is not set and you are running in ${bold}INTERACTIVE${normal} mode, you will be prompted by the interface as to your decision." + exit 0 +elif [ "$#" -eq 1 ] && has_param "--skip-analtyics" "$@"; then + SKIP_ANALYTICS=1 + prompt_for_options else - export HOMEBREW_NO_ANALYTICS=1 + echo "You passed in the following opt-ins:" + for var in "$@"; do + printf "${GREEN}%s${NO_COLOUR}\n" $var + done + SELECTED_OPT_INS=$@ fi +echo $SELECTED_OPT_INS + +init_analytics () { + # If SKIP_ANALYTICS wasn't defined and there were no args + if [ -z $SKIP_ANALYTICS ] && [ $1 == 0 ]; then + local wantToShareAnalytics + read -p "Share usage analytics (y/n)? " wantToShareAnalytics + if [ "$wantToShareAnalytics" != "y" ]; then + SKIP_ANALYTICS=1 + printf "${LIGHT_CYAN}Analytics will not be shared${NO_COLOUR}\n" + else + SKIP_ANALYTICS=0 + printf "${GREEN}Analytics will be shared${NO_COLOUR}\n" + fi + fi + SKIP_ANALYTICS=${SKIP_ANALYTICS:-0} + if (( SKIP_ANALYTICS == 0 )); then + clientID=$(od -vAn -N4 -tx < /dev/urandom) + source ${MY_DIR}/scripts/helpers/google-analytics.sh ${clientID} start $SELECTED_OPT_INS + else + export HOMEBREW_NO_ANALYTICS=1 + fi +} + +init_analytics $# + # Note: Homebrew needs to be set up first source ${MY_DIR}/scripts/common/homebrew.sh @@ -40,10 +158,10 @@ source ${MY_DIR}/scripts/common/unix.sh source ${MY_DIR}/scripts/common/configuration-osx.sh # For each command line argument, try executing the corresponding script in opt-in/ -for var in "$@" +for var in "$SELECTED_OPT_INS" do echo "$var" - FILE=${MY_DIR}/scripts/opt-in/${var}.sh + FILE=${OPT_IN_FOLDER}/${var}.sh echo "$FILE" if [ -f $FILE ]; then source ${FILE} @@ -54,5 +172,5 @@ done source ${MY_DIR}/scripts/common/finished.sh if (( SKIP_ANALYTICS == 0 )); then - source ${MY_DIR}/scripts/helpers/google-analytics.sh ${clientID} finish $@ + source ${MY_DIR}/scripts/helpers/google-analytics.sh ${clientID} finish $SELECTED_OPT_INS fi