a7bd1f99ae54ff2e2081c455f1b34900059ccc71

Author
Marc Cornellà <marc.cornella@live.com>
Committer
Marc Cornellà <marc.cornella@live.com>
Date

Message

installer: extract most code into functions

Diff

  1diff --git a/tools/install.sh b/tools/install.sh
  2index f7eee4daba48e28ab0c71b9afcf929382a665316..3ed8584aabb1d31a0de6367169dd4ad48fcadca0 100755
  3--- a/tools/install.sh
  4+++ b/tools/install.sh
  5@@ -17,14 +17,16 @@ command_exists() {
  6 	command -v "$@" >/dev/null 2>&1
  7 }
  8 
  9-main() {
 10-	# Use colors, but only if connected to a terminal, and that terminal
 11-	# supports them.
 12+# Set up color sequences
 13+setup_color() {
 14 	if command_exists tput; then
 15 		ncolors=$(tput colors)
 16+	else
 17+		ncolors=0
 18 	fi
 19 
 20-	if [ -t 1 ] && [ -n "$ncolors" ] && [ "$ncolors" -ge 8 ]; then
 21+	# Only use colors if connected to a terminal that supports them
 22+	if [ -t 1 ] && [ $ncolors -ge 8 ]; then
 23 		RED="$(tput setaf 1)"
 24 		GREEN="$(tput setaf 2)"
 25 		YELLOW="$(tput setaf 3)"
 26@@ -39,20 +41,9 @@ main() {
 27 		BOLD=""
 28 		NORMAL=""
 29 	fi
 30+}
 31 
 32-	if ! command_exists zsh; then
 33-		echo "${YELLOW}Zsh is not installed!${NORMAL} Please install zsh first!"
 34-		exit 1
 35-	fi
 36-
 37-	if [ -d "$ZSH" ]; then
 38-		cat <<-EOF
 39-			${YELLOW}You already have Oh My Zsh installed.${NORMAL}
 40-			You'll need to remove $ZSH if you want to re-install.
 41-		EOF
 42-		exit 1
 43-	fi
 44-
 45+setup_ohmyzsh() {
 46 	# Prevent the cloned repository from having insecure permissions. Failing to do
 47 	# so causes compinit() calls to fail with "command not found: compdef" errors
 48 	# for users with insecure umasks (e.g., "002", allowing group writability). Note
 49@@ -79,11 +70,13 @@ main() {
 50 		echo "Error: git clone of oh-my-zsh repo failed"
 51 		exit 1
 52 	}
 53+}
 54 
 55+setup_zshrc() {
 56 	echo "${BLUE}Looking for an existing zsh config...${NORMAL}"
 57 	if [ -f ~/.zshrc ] || [ -h ~/.zshrc ]; then
 58 		echo "${YELLOW}Found ~/.zshrc.${GREEN} Backing up to ~/.zshrc.pre-oh-my-zsh.${NORMAL}"
 59-		mv ~/.zshrc ~/.zshrc.pre-oh-my-zsh;
 60+		mv ~/.zshrc ~/.zshrc.pre-oh-my-zsh
 61 	fi
 62 
 63 	echo "${BLUE}Using the Oh My Zsh template file and adding it to ~/.zshrc.${NORMAL}"
 64@@ -93,7 +86,9 @@ main() {
 65 export ZSH=\"$ZSH\"
 66 " ~/.zshrc > ~/.zshrc-omztemp
 67 	mv -f ~/.zshrc-omztemp ~/.zshrc
 68+}
 69 
 70+setup_shell() {
 71 	# If this user's login shell is not already "zsh", attempt to switch.
 72 	TEST_CURRENT_SHELL=$(basename "$SHELL")
 73 	if [ "$TEST_CURRENT_SHELL" != "zsh" ]; then
 74@@ -109,6 +104,27 @@ export ZSH=\"$ZSH\"
 75 			EOF
 76 		fi
 77 	fi
 78+}
 79+
 80+main() {
 81+	setup_color
 82+
 83+	if ! command_exists zsh; then
 84+		echo "${YELLOW}Zsh is not installed.${NORMAL} Please install zsh first."
 85+		exit 1
 86+	fi
 87+
 88+	if [ -d "$ZSH" ]; then
 89+		cat <<-EOF
 90+			${YELLOW}You already have Oh My Zsh installed.${NORMAL}
 91+			You'll need to remove $ZSH if you want to reinstall.
 92+		EOF
 93+		exit 1
 94+	fi
 95+
 96+	setup_ohmyzsh
 97+	setup_zshrc
 98+	setup_shell
 99 
100 	printf "$GREEN"
101 	cat <<-'EOF'