Commit 1d6b99e1 authored by Dom Sekotill's avatar Dom Sekotill
Browse files

Refactor runner iteration into a function

parent 3e08f8be
Loading
Loading
Loading
Loading
+27 −8
Original line number Diff line number Diff line
@@ -128,6 +128,30 @@ set_name() {
}

find_name() {
	iter_configs && return
	case $? in
		1)
			echo >&2 "No free config slots found with ${NAME}"
			;;
		2)
			echo >&2 "Config already exists: ${RUNNER_CONFIG}"
			echo >&2 "(with name: ${NAME})"
			;;
	esac
	exit 1
}

# Iterate through runners until MAX_INDEX is reached or a runner is missing,
# eval-ing any code from arguments on each one.
#
# After return the values set by set_name will be for the most recent runner 
# tried: either the runner specified by MAX_INDEX, the first missing runner, or 
# any runner that causes eval-ed code to error or return.
#
# Unless eval-ed code causes it to be otherwise: returns 0 if a non-existant 
# runner config is tried; returns 1 if MAX_INDEX is passed; returns 2 if NAME 
# has no formatting mark for an index and the runner it refers to exists.
iter_configs() {
	local index=0

	[[ -v NAME ]] || NAME=$DEFAULT_NAME
@@ -135,20 +159,15 @@ find_name() {
	if [[ $(printf "${NAME}" 1) = $(printf "${NAME}" 2) ]]; then
		# No formatting marks in NAME
		set_name
		if [[ -e ${RUNNER_CONFIG} ]]; then
			echo >&2 "Config already exists: ${RUNNER_CONFIG}"
			echo >&2 "(with name: ${NAME})"
			exit 1
		fi
		return
		[[ -e ${RUNNER_CONFIG} ]] && return 2 || return 0
	fi

	while let "++index <= ${MAX_INDEX-99}" && set_name $index; do
		[[ -e ${RUNNER_CONFIG} ]] || return 0
		eval "$*"
	done

	echo >&2 "No free config slots found with ${NAME}"
	exit 1
	return 1
}

container_new() {