Preview:
#!/bin/bash

# Once you have inputted your projects directory, it gives you the option to choose which project to run.

# - It will start Docker if it's available
# - It will start Vue.js if it's available
# - It will just simply open VSCode if neither of the above is available

# Commands to run (assuming the script is in a file called 'go')

# Run the script: ./go
# Re-run the setup ./go setup
# Reset the config: ./go reset

DefaultProjectsLocation="/var/www/sites/" # Default working path
SaveFile=~/bin/.go_config # Config file name

# Colours
Reset='\033[0m'
Black='\033[0;30m'
Red='\033[0;31m' 
Green='\033[0;32m'
Blue='\033[0;34m'
BRed='\033[41m'
BGreen='\033[42m'

#() readFile - Function to make a file readable and use its variables
readFile() {
	set -a
		source <(cat $1 | sed -e '/^#/d;/^\s*$/d' -e "s/'/'\\\''/g" -e "s/=\(.*\)/='\1'/g")
	set +a
}

#() reset - Delete the config file
reset() {
	if [ -f $SaveFile ]; then
		# Delete the config file
		rm $SaveFile
	fi
	echo -e "\n${Blue}Configuration has been reset.${Reset}\n"
	exit 0
}

#() configCheck - Checking if the config exists
configCheck() {
	# Create the bin directory if it doesn't exist
	if [ ! -d ~/bin/ ]; then
		mkdir ~/bin/
		exit 0
	fi

	# Create the file or skip
	if [ ! -f ${SaveFile} ]; then
		# Create a new config file
		touch ${SaveFile}
	fi
}

#() setProjectsPath - User input for setting the projects path
setProjectsPath() {
	
	echo -e "\n${Green}Where are your projects located?${Reset}\n"
	echo -e "----------------------------------------------------------\n"
	# User Input: Enter a directory path
	read -p "Projects directory path [press Enter for ${DefaultProjectsLocation}]: " I_ProjectLocation

	# Check for user input an d assign a variable
	if [ $I_ProjectLocation ]; then
		NewProjectLocation=$I_ProjectLocation
	else
		NewProjectLocation=$DefaultProjectsLocation
	fi

	# Function for user feedback
	SaveFileUpdates() {
		echo -e "${Green}Great.${Reset}"
		echo -e "\n${Blue}Projects directory path saved as${Reset} ${Red}${NewProjectLocation}${Reset}"
		echo -e "\nIf that's incorrect, run ${Green}'./go setup'${Reset} to change the directory path.\n"
	}

	configCheck

	# Make the file readable
	readFile $SaveFile

	# Check if the config file exists and create or update it
	if [ ! ${PROJECT_LOCATION} ]; then
		# Save the location to the config file
		echo "PROJECT_LOCATION=${NewProjectLocation}" >> ${SaveFile}
		SaveFileUpdates
	else
		# Update the config file with the new location
		sed -i "s!PROJECT_LOCATION=${PROJECT_LOCATION}!PROJECT_LOCATION=${NewProjectLocation}!" ${SaveFile}
		SaveFileUpdates
	fi
}

#() setName - User input for setting their name
setName() {
	echo -e "\n"
	read -p "What's your name? [press Enter to skip]: " I_name
	echo -e "\n"

	# Check for user input an d assign a variable
	if [ $I_name ]; then
		NewName=$I_name
	else
		NewName=
	fi

	if [ $I_name ]; then
		echo -e "${Green}Hi, ${I_name}!${Reset}"

		configCheck

		readFile $SaveFile

		if [ ${USER_NAME} ]; then
			sed -i "s!USER_NAME=${USER_NAME}!USER_NAME=${NewName}!" ${SaveFile}
		else
			echo "USER_NAME=${I_name}" >> ${SaveFile}
		fi
		
		echo -e "\n${Blue}Your name has been saved.${Reset}\n"
	else
		configCheck
		readFile $SaveFile
		if [ ${USER_NAME} ]; then
			sed -i "s!USER_NAME=${USER_NAME}!!" ${SaveFile}
		fi
		echo -e "${Blue}No problem, We don't have to be friends.${Reset}\n"
	fi
}

#() setup - Setup menu
setup() {
	clear
	PS3=$'\nWhat would you like to do? '
	select options in "Set your name" "Set directory path" "Reset configuration" "Exit"
	do
			if [ "$REPLY" == "1" ]; then
				echo -e "\n${Green}$options${Reset}\n"
				setName
				exit 0
			elif [ "$REPLY" == "2" ]; then
				echo -e "\n${Green}$options${Reset}\n"
				setProjectsPath
				exit 0
			elif [ "$REPLY" == "3" ]; then
				echo -e "\n${Green}$options${Reset}\n"
				reset
			else
				echo -e "\n${Blue}$options${Reset}"
				echo -e "\n${Green}Goodbye.${Reset}"
				exit 0
			fi
	done
}

#() fullSetup - Full setup that runs through steps
fullSetup() {
	clear
	echo -e "\n${Black}${BGreen} 1. Hi there, what do you want to be called? ${Reset}"
	setName
	echo -e "\n${Black}${BGreen} 2. Enter your working directory path. ${Reset}"
	setProjectsPath
	echo -e "\nYou're all setup! Run ${Green}'./go'${Reset} to get started."
	exit 0
}

#() go - Startup script
go() {

	# Check if config file exists then run the script or setup
	if [ -f ${SaveFile} ]; then
		# Make variables readable in bash
		readFile $SaveFile
		# Get path location
		ProjectsPath=${PROJECT_LOCATION}
		# Clear screen
		clear

		if [ -d $ProjectsPath ]; then

			readFile $SaveFile

			# Custom greeting
			if [ ${USER_NAME} ]; then
				Greet="Hi ${USER_NAME}, "
			else
				Greet=
			fi

			echo -e "\n${Green}${Greet}Choose which project you want to start:${Reset}"
			echo -e "\n----------------------------------------------------------\n"

			# Get a list of projects to select
			cd ${ProjectsPath}
			IFS= mapfile -t files < <(ls -t -d */)
			PS3=$'\nEnter the project number: '

			select dir in "${files[@]}"
			do
				test -n "$dir" && break
				echo -e "\n${Red}Whoops, that input isn't quite right. Put your glasses on and try again, perhaps?${Reset}"
				exit 0
			done

			# Set working path
			WorkPath=${ProjectsPath}${dir}/

			echo -e "\n${Green}Hold your horses, checking for ${Red}${dir}${Reset}\n"

			# Check if the inputted directory exists
			if [ -d ${WorkPath} ]; then

					echo -e "${Green}Looks good, starting ${Red}${dir}${Reset}\n"

					# Change directory
					cd ${ProjectsPath}${dir}

					# Check if a yml file exists
					if [ -f *"docker"* ]; then
						echo -e "${Blue}Docker detected${Reset}\n"

						# Run Docker
						docker-compose start
						echo -e "\n"

						# Open in browser if .env file exists
						ENVFILE=./.env
						if test -f "$ENVFILE"; then

							# Make env variables readable in bash
							readFile ".env"
							
							# Check if port exist
							if [ ! -z ${PORT} ]; then
								LocalPort=":${PORT}"
							else
								LocalPort=""
							fi
							echo -e "Opening http://localhost${LocalPort}"

							# Open in default browser
							xdg-open http://localhost${LocalPort}
							echo -e "${Green}done${Reset}\n"
						fi

					# Check if it's Vue
					elif [ -f ${WorkPath}/*/*.vue ]; then
						echo -e "${Blue}Vue.js detected${Reset}\n"

						# Open in VSCode
						echo -e "Opening VSCode..."
						code .
						echo -e "${Green}done${Reset}"

						echo -e "\n${Green}Starting Vue.js${Reset}"
						# Run Vue
						npm run serve
					else
						echo -e "${Blue}Nothing to run${Reset}\n"
					fi

				# Open in VSCode
				echo -e "Opening VSCode..."
				code .
				echo -e "${Green}done${Reset}"

			fi

			echo -e "\n----------------------------------------------------------\n"
		else
			echo -e "\n\n${Black}${BRed} Argh. ${Reset}"
			echo -e "\n${Red}It's all gone to pot. Your projects directory doesn't seem to exist.${Reset}\n"
			
			read -r -p "Would you like to re-enter your path? [Y/n]: " I_answer

			# Check user's input
			if [ "$I_answer" == "y" ] || [ "$I_answer" == "Y" ]; then
				setProjectsPath
			else
				echo -e "\n${Green}Fair enough, safe travels."
				exit 0
			fi
		fi

	else
		fullSetup
	fi

}

# Run setup if the user has inputted the setup variable
if [ "$1" == "setup" ]; then
	setup
elif [ "$1" == "name" ]; then
	clear
	setName
	exit 0
elif [ "$1" == "reset" ]; then
	clear
	reset
fi

go # Run the script
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter