This is an automated email from the ASF dual-hosted git repository.

dragon pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git


The following commit(s) were added to refs/heads/master by this push:
     new 9598ebf  Runroot: make runroot_handler support both ink_args and 
ArgParser
9598ebf is described below

commit 9598ebfe2adc2287b03ed536675801fe0f8787b5
Author: Xavier Chi <[email protected]>
AuthorDate: Mon Nov 12 13:28:31 2018 -0600

    Runroot: make runroot_handler support both ink_args and ArgParser
---
 include/tscore/runroot.h             |   2 +
 src/traffic_layout/traffic_layout.cc |   5 +-
 src/tscore/runroot.cc                | 104 ++++++++++++++++++++++-------------
 3 files changed, 70 insertions(+), 41 deletions(-)

diff --git a/include/tscore/runroot.h b/include/tscore/runroot.h
index 1dd391e..0f2efa1 100644
--- a/include/tscore/runroot.h
+++ b/include/tscore/runroot.h
@@ -54,6 +54,8 @@ typedef std::unordered_map<std::string, std::string> 
RunrootMapType;
 bool exists(const std::string &dir);
 bool is_directory(const std::string &directory);
 
+// argparser_runroot_handler should replace runroot_handler below when all 
program use ArgParser.
+void argparser_runroot_handler(std::string const &value, const char 
*executable, bool json);
 void runroot_handler(const char **argv, bool json = false);
 
 // get a map from default layout
diff --git a/src/traffic_layout/traffic_layout.cc 
b/src/traffic_layout/traffic_layout.cc
index a7bd66d..72edd20 100644
--- a/src/traffic_layout/traffic_layout.cc
+++ b/src/traffic_layout/traffic_layout.cc
@@ -41,7 +41,7 @@ main(int argc, const char **argv)
 
   // global options
   engine.parser.add_option("--help", "-h", "Print usage information")
-    .add_option("--run-root", "", "using TS_RUNROOT as sandbox", "", 1)
+    .add_option("--run-root", "", "using TS_RUNROOT as sandbox", "TS_RUNROOT", 
1)
     .add_option("--version", "-V", "Print version string");
 
   // info command
@@ -68,7 +68,8 @@ main(int argc, const char **argv)
 
   engine.arguments = engine.parser.parse(argv);
 
-  runroot_handler(argv, engine.arguments.get("json"));
+  auto runroot_arg = engine.arguments.get("run-root");
+  argparser_runroot_handler(runroot_arg.value(), argv[0], 
engine.arguments.get("json"));
   Layout::create();
 
   engine.arguments.invoke();
diff --git a/src/tscore/runroot.cc b/src/tscore/runroot.cc
index 5d786d9..f444d99 100644
--- a/src/tscore/runroot.cc
+++ b/src/tscore/runroot.cc
@@ -101,44 +101,10 @@ get_parent_yaml_path(const std::string &path)
   return {};
 }
 
-// handler for ts runroot
-// this function set up runroot_file
-void
-runroot_handler(const char **argv, bool json)
+static void
+runroot_extra_handling(const char *executable, bool json)
 {
-  std::string prefix = "--run-root";
   std::string path;
-
-  // check if we have --run-root...
-  std::string arg = {};
-
-  int i = 0;
-  while (argv[i]) {
-    std::string_view command = argv[i];
-    if (command.substr(0, prefix.size()) == prefix) {
-      arg = command.data();
-      break;
-    }
-    i++;
-  }
-
-  // if --run-root is provided
-  if (!arg.empty() && arg != prefix) {
-    // 1. pass in path
-    prefix += "=";
-    std::string value = arg.substr(prefix.size(), arg.size() - 1);
-    path              = get_yaml_path(value);
-    if (!path.empty()) {
-      if (!json) {
-        ink_notice("using command line path as RUNROOT");
-      }
-      runroot_file = path;
-      return;
-    } else if (!json) {
-      ink_warning("Unable to access runroot: '%s'", value.c_str());
-    }
-  }
-
   // 2. check Environment variable
   char *env_val = getenv("TS_RUNROOT");
   if (env_val) {
@@ -153,7 +119,6 @@ runroot_handler(const char **argv, bool json)
       ink_warning("Unable to access runroot: '%s' from $TS_RUNROOT", env_val);
     }
   }
-
   // 3. find cwd or parent path of cwd to check
   char cwd[PATH_MAX] = {0};
   if (getcwd(cwd, sizeof(cwd)) != nullptr) {
@@ -166,10 +131,9 @@ runroot_handler(const char **argv, bool json)
       return;
     }
   }
-
   // 4. installed executable
   char RealBinPath[PATH_MAX] = {0};
-  if ((argv[0] != nullptr) && realpath(argv[0], RealBinPath) != nullptr) {
+  if ((executable != nullptr) && realpath(executable, RealBinPath) != nullptr) 
{
     std::string bindir = RealBinPath;
     bindir             = bindir.substr(0, bindir.find_last_of("/")); // 
getting the bin dir not executable path
     path               = get_parent_yaml_path(bindir);
@@ -185,6 +149,68 @@ runroot_handler(const char **argv, bool json)
   return;
 }
 
+// This is a temporary approach to handle runroot with migration to ArgParser.
+// When all program use ArgParser, we can remove the runroot_handler below and 
replace it with this one.
+void
+argparser_runroot_handler(std::string const &value, const char *executable, 
bool json)
+{
+  // 1.if --run-root is provided
+  if (!value.empty()) {
+    std::string path = get_yaml_path(value);
+    if (!path.empty()) {
+      if (!json) {
+        ink_notice("using command line path as RUNROOT");
+      }
+      runroot_file = path;
+      return;
+    } else if (!json) {
+      ink_warning("Unable to access runroot: '%s'", value.c_str());
+    }
+  }
+  runroot_extra_handling(executable, json);
+}
+
+// handler for ts runroot
+// this function set up runroot_file
+void
+runroot_handler(const char **argv, bool json)
+{
+  std::string prefix = "--run-root";
+  std::string path;
+
+  // check if we have --run-root...
+  std::string arg = {};
+
+  int i = 0;
+  while (argv[i]) {
+    std::string_view command = argv[i];
+    if (command.substr(0, prefix.size()) == prefix) {
+      arg = command.data();
+      break;
+    }
+    i++;
+  }
+
+  // if --run-root is provided
+  if (!arg.empty() && arg != prefix) {
+    // 1. pass in path
+    prefix += "=";
+    std::string value = arg.substr(prefix.size(), arg.size() - 1);
+    path              = get_yaml_path(value);
+    if (!path.empty()) {
+      if (!json) {
+        ink_notice("using command line path as RUNROOT");
+      }
+      runroot_file = path;
+      return;
+    } else if (!json) {
+      ink_warning("Unable to access runroot: '%s'", value.c_str());
+    }
+  }
+
+  runroot_extra_handling(argv[0], json);
+}
+
 // return a map of all path with default layout
 std::unordered_map<std::string, std::string>
 runroot_map_default()

Reply via email to