From 854b17c44957cec120e818ee552e3e13459f8e11 Mon Sep 17 00:00:00 2001
From: Wouter Deconinck <wdconinc@gmail.com>
Date: Sun, 23 Jan 2022 16:51:12 +0000
Subject: [PATCH] Resolve "Allow for FileLoader to parse environment variables
 as part of the cache path"

---
 compact/tracking_config_deathvalley.xml |  1 +
 src/FileLoader.cpp                      | 19 +++++++++++++++++++
 2 files changed, 20 insertions(+)

diff --git a/compact/tracking_config_deathvalley.xml b/compact/tracking_config_deathvalley.xml
index bd6d93ed..d3276b6f 100644
--- a/compact/tracking_config_deathvalley.xml
+++ b/compact/tracking_config_deathvalley.xml
@@ -100,6 +100,7 @@
   </documentation>
   <plugins>
     <plugin name="FileLoader">
+      <arg value="cache:$DETECTOR_PATH"/>
       <arg value="file:calibrations/materials-map.cbor"/>
       <arg value="url:https://eicweb.phy.anl.gov/EIC/detectors/athena/uploads/4a4e7c8eb6089b634d762d112c89bd5d/material-maps.cbor"/>
     </plugin>
diff --git a/src/FileLoader.cpp b/src/FileLoader.cpp
index 618f50aa..917dc873 100644
--- a/src/FileLoader.cpp
+++ b/src/FileLoader.cpp
@@ -8,6 +8,7 @@
 
 #include <filesystem>
 #include <iostream>
+#include <cstdlib>
 #include <string>
 
 namespace fs = std::filesystem;
@@ -57,6 +58,24 @@ long load_file(
     return 0;
   }
 
+  // parse cache for environment variables
+  auto pos = std::string::npos;
+  while ((pos = cache.find('$')) != std::string::npos) {
+    auto after = cache.find_first_not_of(
+      "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+      "abcdefghijklmnopqrstuvwxyz"
+      "0123456789"
+      "_",
+      pos + 1);
+    if (after == std::string::npos) after = cache.size(); // cache ends on env var
+    auto env_name = cache.substr(pos + 1, after - pos - 1);
+    auto env_value = std::getenv(env_name.c_str());
+    if (env_value == nullptr) env_value = "";
+    cache.erase(pos, after - pos);
+    cache.insert(pos, env_value);
+    printout(INFO, "FileLoader", "$" + env_name + " -> " + env_value);
+  }
+
   // create file path
   fs::path file_path(file);
 
-- 
GitLab