From 4a0320013039e68081268e7de52d14ad79ed2a10 Mon Sep 17 00:00:00 2001 From: Luka Vandervelden Date: Sat, 9 Nov 2019 14:47:29 +0100 Subject: [PATCH] Improved variable expansion in service definitions. --- src/service/service.cr | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/src/service/service.cr b/src/service/service.cr index 59e5f1a..9f3e2bc 100644 --- a/src/service/service.cr +++ b/src/service/service.cr @@ -190,7 +190,8 @@ class Service end end - private def build_environment + @commands_environment : Hash(String, String)? + private def build_commands_environment! env = {} of String => String env["SERVICE_NAME"] = name @@ -223,7 +224,10 @@ class Service env[variable] = value end - env + @commands_environment = env + end + def commands_environment : Hash(String, String) + @commands_environment || build_commands_environment! end # FIXME: Is working on ${} really a good idea? @@ -231,18 +235,12 @@ class Service # environment, and we should take our variables from that # instead of hardcoding everything. private def evaluate(string) + env = commands_environment + string.gsub /\${[a-zA-Z_]+}/ do |match| match = match[2..match.size-2] - if match.downcase == "environment" - @environment.name - elsif match.downcase == "service_root" - root - elsif match.downcase == "service_name" - name - else - "" - end + env[match.upcase]? || "" end end @@ -283,7 +281,7 @@ class Service Process.exec "sh", ["-c", creation_command], output: Process::Redirect::Inherit, error: Process::Redirect::Inherit, - env: build_environment + env: commands_environment end.wait unless child.success? @@ -315,7 +313,7 @@ class Service Process.exec command, args, chdir: (@reference.directory.try { |x| evaluate x } || root), - env: build_environment + env: commands_environment end self.save_pid context.pid_directory, process.pid @@ -489,7 +487,7 @@ class Service Process.exec "sh", ["-c", command], output: Process::Redirect::Inherit, error: Process::Redirect::Inherit, - env: build_environment + env: commands_environment else FileUtils.rm_rf file_path end