Skip to content

Instantly share code, notes, and snippets.

@Weiyuan-Lane
Last active July 2, 2023 07:19
Show Gist options
  • Save Weiyuan-Lane/2ecc247e39614b6be69574e30de39178 to your computer and use it in GitHub Desktop.
Save Weiyuan-Lane/2ecc247e39614b6be69574e30de39178 to your computer and use it in GitHub Desktop.
#!/bin/sh
TMP_DIR=".tmp"
PROMPT_DIR="prompt"
mkdir -p $TMP_DIR
mkdir -p $TMP_DIR/$PROMPT_DIR
promptUserEntry(){
local _outputVar=$1
local _inputPrompt=$2
local _reuseCacheValuePrompt=$3
local _reuseCacheValueMessage=$4
local _validInputMessage=$5
local _invalidInputMessage=$6
local _promptCacheFilename="$TMP_DIR/$PROMPT_DIR/$_outputVar"
local _reuseCacheDecision=""
local _userPrompt=""
if [ -e $_promptCacheFilename ]; then
_userPrompt=`cat $_promptCacheFilename`
echo "$_reuseCacheValueMessage \"$_userPrompt\""
while true; do
read -p "$_reuseCacheValuePrompt " _reuseCacheDecision
case $_reuseCacheDecision in
Y|y ) echo ""; eval $_outputVar="'$_userPrompt'"; return 0;;
N|n ) break;;
* ) echo $_invalidInputMessage;;
esac
done
fi
while true; do
read -p "$_inputPrompt " _userPrompt
case $_userPrompt in
"") echo $_invalidInputMessage;;
* ) echo $_userPrompt > $_promptCacheFilename; break;;
esac
done
echo "$_validInputMessage \"$_userPrompt\"\n"
eval $_outputVar="'$_userPrompt'"
}
promptUserEntry \
'gcpProjectID' \
'Please input your Google Cloud Platform Project ID:' \
'Use the same project id? (Y/n)' \
'You previously inputted GCP Project ID as' \
'You inputted GCP Project ID as' \
'Please input a valid value.'
# Pretend this this doing something productive for GCP
echo "Running some GCP commands for \"$gcpProjectID\"\n"
sleep 2
promptUserEntry \
'awsProjectID' \
'Please input your AWS Project ID:' \
'Use the same project id? (Y/n)' \
'You previously inputted AWS Project ID as' \
'You inputted AWS Project ID as ' \
'Please input a valid value.'
# Pretend this this doing something productive for AWS
echo "Running some AWS commands for \"$awsProjectID\"\n"
sleep 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment