From 167571f31a6af7a43bba9abcdf37375eff2294cc Mon Sep 17 00:00:00 2001
From: Salt <rehashedsalt@cock.li>
Date: Tue, 28 Aug 2018 13:16:32 -0500
Subject: [PATCH] dtfscript: Move code into functions

---
 .config/dtfscripts/dtfscript | 50 ++++++++++++++++++++++++------------
 1 file changed, 34 insertions(+), 16 deletions(-)

diff --git a/.config/dtfscripts/dtfscript b/.config/dtfscripts/dtfscript
index 7bd2409c..a4f21449 100755
--- a/.config/dtfscripts/dtfscript
+++ b/.config/dtfscripts/dtfscript
@@ -83,9 +83,10 @@ else
 	dtf_log "Performing a subset of all tasks"
 fi
 
-## Run tasks
-for task in $DTF_TASKS; do
-	task_full="$DTF_TASKS_DIR/$task"
+## Define (and export) a task run function
+function dtf_task_run() {
+	[ -z ${1+x} ] && return 1
+	task_full="$DTF_TASKS_DIR/$1"
 	(
 	if ! [ -r "$task_full" ]; then
 		dtf_log "Task is unreadable or missing: $task"
@@ -98,6 +99,12 @@ for task in $DTF_TASKS; do
 	source "$DTF_CONFIG_HOME/common_task.sh"
 	source "$task_full"
 	)
+}
+export -f dtf_task_run
+
+## Run tasks
+for task in $DTF_TASKS; do
+	dtf_task_run $task
 done
 
 ## Determine which services to run
@@ -111,28 +118,39 @@ else
 	dtf_log "Starting a subset of available services"
 fi
 
-## Run startup scripts
-for service in $DTF_SERVICES; do
-	service_full="$DTF_SERVICES_DIR/$service"
-	(
+## Define (and export) service functions
+function dtf_service_validate() {
+	service_full="$DTF_SERVICES_DIR/$1"
 	if ! [ -r "$service_full" ]; then
-		dtf_log "Service is unreadable or missing: $service"
-		exit 1
+		dtf_log "Service is unreadable or missing: $1"
+		return 1
 	fi
 	if ! [ -x "$service_full" ]; then
-		dtf_log "Service is unexecutable: $service"
-		exit 1
+		dtf_log "Service is unexecutable: $1"
+		return 1
 	fi
+	return 0
+}
+export -f dtf_service_validate
+
+function dtf_service_start() {
+	service_full="$DTF_SERVICES_DIR/$1"
+	dtf_service_validate $1 || return 1
+	(
 	source "$DTF_CONFIG_HOME/common_service.sh"
 	source "$service_full"
-	if isup && [[ "$service_kill_on_reload" == "true" ]] ; then
-		stop
-	fi
-	if ! prestart; then exit 1; fi
-	if ! prestart-extra; then exit 1; fi
+	isup && [[ "$service_kill_on_reload" == "true" ]] && stop
+	prestart || exit 1
+	prestart-extra || exit 1
 	start
 	start-extra
 	)
+}
+export -f dtf_service_start
+
+## Run startup scripts
+for service in $DTF_SERVICES; do
+	dtf_service_start $service
 done
 
 dtf_log "Finished initialization"