simplify the input_box function

This commit is contained in:
Joshua Tauberer 2014-08-21 15:52:19 +00:00
parent 980b83b124
commit 7ea4d33e06
2 changed files with 99 additions and 91 deletions

View file

@ -134,21 +134,11 @@ function message_box {
} }
function input_box { function input_box {
TMP=`mktemp` # input_box "title" "prompt" "defaultvalue" VARIABLE
dialog --title "$1" --inputbox "$2" 0 0 "$3" 2>$TMP # The user's input will be stored in the variable VARIABLE.
# The exit code from dialog will be stored in VARIABLE_EXITCODE.
respose=$? declare -n result=$4
declare -n result_code=$4_EXITCODE
case $respose in result=$(dialog --stdout --title "$1" --inputbox "$2" 0 0 "$3")
0) result_code=$?
result=$(<$TMP)
;;
1)
exit
;;
255)
exit
esac
rm $TMP
} }

View file

@ -38,11 +38,14 @@ fi
if [ -t 0 ]; then if [ -t 0 ]; then
# In an interactive shell... # In an interactive shell...
# Install dialog # Install 'dialog' so we can ask the user questions. The original motivation for
echo "Preparing installation ... " # this was being able to ask the user for input even if stdin has been redirected,
# e.g. if we piped a bootstrapping install script to bash to get started.
apt_install dialog apt_install dialog
message_box "Hello and thanks for deploying a Mail-in-a-Box!" \ message_box "Mail-in-a-Box Installation" \
"I'm going to ask you a few questions. To change your answers later, just re-run this script." "Hello and thanks for deploying a Mail-in-a-Box!
\n\nI'm going to ask you a few questions.
\n\nTo change your answers later, just re-run this script."
fi fi
# Recall the last settings used if we're running this a second time. # Recall the last settings used if we're running this a second time.
@ -63,26 +66,32 @@ if [ -z "$PRIMARY_HOSTNAME" ]; then
if [ -z "$DEFAULT_PRIMARY_HOSTNAME" ]; then if [ -z "$DEFAULT_PRIMARY_HOSTNAME" ]; then
# This is the first run. Ask the user for his email address so we can # This is the first run. Ask the user for his email address so we can
# provide the best default for the box's hostname. # provide the best default for the box's hostname.
input_box "What email address are you setting this box up to manage?" \ input_box "Your Email Address" \
"The part after the @-sign must be a domain name or subdomain "What email address are you setting this box up to manage?
\nthat you control. You can add other email addresses to this \n\nThe part after the @-sign must be a domain name or subdomain
\nbox later (including email addresses on other domain names that you control. You can add other email addresses to this
\nor subdomains you control).\n box later (including email addresses on other domain names
\nWe've guessed an email address. Backspace it and type in what or subdomains you control).
\nyou really want.\n \n\nWe've guessed an email address. Backspace it and type in what
\nEmail Address (me@`get_default_hostname`): " you really want.
\n\nEmail Address:" \
me@`get_default_hostname` \
EMAIL_ADDR
if [ -z "$result" ]; then if [ -z "$EMAIL_ADDR" ]; then
EMAIL_ADDR=me@`get_default_hostname` # user hit ESC/cancel
else exit
EMAIL_ADDR=$result
fi fi
while ! management/mailconfig.py validate-email "$EMAIL_ADDR" while ! management/mailconfig.py validate-email "$EMAIL_ADDR"
do do
input_box "What email address are you setting this box up to manage?" \ input_box "Your Email Address" \
"That's not a valid email address." $EMAIL_ADDR "That's not a valid email address.\n\nWhat email address are you setting this box up to manage?" \
EMAIL_ADDR=$result $EMAIL_ADDR \
EMAIL_ADDR
if [ -z "$EMAIL_ADDR" ]; then
# user hit ESC/cancel
exit
fi
done done
# Take the part after the @-sign as the user's domain name, and add # Take the part after the @-sign as the user's domain name, and add
@ -91,17 +100,17 @@ if [ -z "$PRIMARY_HOSTNAME" ]; then
fi fi
input_box "Hostname" \ input_box "Hostname" \
"This box needs a name, called a 'hostname'. The name will form a part "This box needs a name, called a 'hostname'. The name will form a part of the box's web address.
\nof the box's web address.\n \n\nWe recommend that the name be a subdomain of the domain in your email
\nWe recommend that the name be a subdomain of the domain in your email address, so we're suggesting $DEFAULT_PRIMARY_HOSTNAME.
\naddress, so we're suggesting $DEFAULT_PRIMARY_HOSTNAME. \n\nYou can change it, but we recommend you don't.
\nYou can change it, but we recommend you don't.\n \n\nHostname:" \
\nHostname ($DEFAULT_PRIMARY_HOSTNAME): " $DEFAULT_PRIMARY_HOSTNAME \
PRIMARY_HOSTNAME
if [ -z "$result" ]; then if [ -z "$PRIMARY_HOSTNAME" ]; then
PRIMARY_HOSTNAME=$DEFAULT_PRIMARY_HOSTNAME # user hit ESC/cancel
else exit
PRIMARY_HOSTNAME=$result
fi fi
fi fi
@ -128,15 +137,15 @@ if [ -z "$PUBLIC_IP" ]; then
fi fi
if [ -z "$PUBLIC_IP" ]; then if [ -z "$PUBLIC_IP" ]; then
input_box "Your public IP address" \ input_box "Public IP Address" \
"Enter the public IP address of this machine, "Enter the public IP address of this machine, as given to you by your ISP.
as given to you by your ISP.\n \n\nPublic IP address:" \
\nPublic IP ($DEFAULT_PUBLIC_IP): " $DEFAULT_PUBLIC_IP \
PUBLIC_IP
if [ -z "$result" ]; then if [ -z "$PUBLIC_IP" ]; then
PUBLIC_IP=$DEFAULT_PUBLIC_IP # user hit ESC/cancel
else exit
PUBLIC_IP=$result
fi fi
fi fi
fi fi
@ -159,15 +168,16 @@ if [ -z "$PUBLIC_IPV6" ]; then
fi fi
if [[ -z "$PUBLIC_IPV6" && $MATCHED == 0 ]]; then if [[ -z "$PUBLIC_IPV6" && $MATCHED == 0 ]]; then
input_box "IPv6 Optional" \ input_box "IPv6 Address (Optional)" \
"Enter the public IPv6 address of this machine, as given to you by your ISP. "Enter the public IPv6 address of this machine, as given to you by your ISP.
\nLeave blank if the machine does not have an IPv6 address.\n \n\nLeave blank if the machine does not have an IPv6 address.
\nPublic IPv6 ($DEFAULT_PUBLIC_IPV6): " \n\nPublic IPv6 address:"
$DEFAULT_PUBLIC_IPV6 \
PUBLIC_IPV6
if [ -z "$result" ]; then if [ ! $PUBLIC_IPV6_EXITCODE ]; then
PUBLIC_IPV6=$DEFAULT_PUBLIC_IPV6 # user hit ESC/cancel
else exit
PUBLIC_IPV6=$result
fi fi
fi fi
fi fi
@ -182,11 +192,13 @@ if [ -z "$PRIVATE_IPV6" ]; then
PRIVATE_IPV6=$(get_default_privateip 6) PRIVATE_IPV6=$(get_default_privateip 6)
fi fi
if [[ -z "$PRIVATE_IP" && -z "$PRIVATE_IPV6" ]]; then if [[ -z "$PRIVATE_IP" && -z "$PRIVATE_IPV6" ]]; then
message_box "Error" \ echo
"I could not determine the IP or IPv6 address of the network inteface echo "I could not determine the IP or IPv6 address of the network inteface"
\nfor connecting to the Internet. Setup must stop.\n echo "for connecting to the Internet. Setup must stop."
`hostname -I`\n echo
`route`" hostname -I
route
echo
exit exit
fi fi
@ -201,23 +213,22 @@ if [ ! -z "$DEFAULT_STORAGE_ROOT" ] && [ ! -z "$DEFAULT_CSR_COUNTRY" ] && [ -f $
fi fi
if [ -z "$CSR_COUNTRY" ]; then if [ -z "$CSR_COUNTRY" ]; then
input_box "Country Code" \
"Enter the two-letter, uppercase country code for where you
\nlive or where your organization is based. (This is used to
\ncreate an SSL certificate.)\n
\nCountry Code ($DEFAULT_CSR_COUNTRY): "
if [ -z "$result" ]; then
CSR_COUNTRY=$DEFAULT_CSR_COUNTRY
else
CSR_COUNTRY=$result
fi
#if [ -z "$DEFAULT_CSR_COUNTRY" ]; then #if [ -z "$DEFAULT_CSR_COUNTRY" ]; then
# # set a default on first run # # set a default on first run
# DEFAULT_CSR_COUNTRY=...? # DEFAULT_CSR_COUNTRY=...?
#fi #fi
input_box "Country Code" \
"Enter the two-letter, uppercase country code for where you live or where your
organization is based. (This is used to create an SSL certificate.)
\n\nCountry Code:" \
$DEFAULT_CSR_COUNTRY \
CSR_COUNTRY
if [ -z "$CSR_COUNTRY" ]; then
# user hit ESC/cancel
exit
fi
fi fi
# Automatic configuration, e.g. as used in our Vagrant configuration. # Automatic configuration, e.g. as used in our Vagrant configuration.
@ -307,22 +318,29 @@ if [ -z "`tools/mail.py user`" ]; then
if [ -z "$EMAIL_ADDR" ]; then if [ -z "$EMAIL_ADDR" ]; then
# In an interactive shell, ask the user for an email address. # In an interactive shell, ask the user for an email address.
if [ -t 0 ]; then if [ -t 0 ]; then
input_box "Create your first mail user" \ input_box "Mail Account" \
"Let's create your first mail user.\n "Let's create your first mail account.
\nEmail Address (user@$PRIMARY_HOSTNAME): " \n\nWhat email address do you want?" \
me@`get_default_hostname` \
EMAIL_ADDR
if [ -z "$result" ]; then if [ -z "$EMAIL_ADDR" ]; then
EMAIL_ADDR=me@`get_default_hostname` # user hit ESC/cancel
else exit
EMAIL_ADDR=$result
fi fi
while ! management/mailconfig.py validate-email "$EMAIL_ADDR" while ! management/mailconfig.py validate-email "$EMAIL_ADDR"
do do
input_box "What email address are you setting this box up to manage?"\ input_box "Mail Account" \
"That's not a valid email address." $EMAIL_ADDR "That's not a valid email address.
EMAIL_ADDR=$result \n\nWhat email address do you want?" \
$EMAIL_ADDR \
EMAIL_ADDR
if [ -z "$EMAIL_ADDR" ]; then
# user hit ESC/cancel
exit
fi
done done
# But in a non-interactive shell, just make something up. # But in a non-interactive shell, just make something up.
# This is normally for testing. # This is normally for testing.
else else