You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by ji...@apache.org on 2016/11/29 19:41:22 UTC

[3/9] mesos git commit: Added support for building environment variables from flags.

Added support for building environment variables from flags.

This patch added a method to FlagsBase to build a map of environment
variables from the flags. This simplifies the logic when the caller
wants to pass flags as environment variables to the subprocess.

Review: https://reviews.apache.org/r/54097


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/c06c6976
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/c06c6976
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/c06c6976

Branch: refs/heads/master
Commit: c06c697693a5bc78fd210a10b0df1b2ab2780a18
Parents: afab55a
Author: Jie Yu <yu...@gmail.com>
Authored: Thu Nov 24 00:11:02 2016 -0800
Committer: Jie Yu <yu...@gmail.com>
Committed: Tue Nov 29 11:40:50 2016 -0800

----------------------------------------------------------------------
 3rdparty/stout/include/stout/flags/flags.hpp | 24 +++++++++++++++++++++++
 3rdparty/stout/tests/flags_tests.cpp         | 23 ++++++++++++++++++++++
 2 files changed, 47 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/c06c6976/3rdparty/stout/include/stout/flags/flags.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/stout/include/stout/flags/flags.hpp b/3rdparty/stout/include/stout/flags/flags.hpp
index 06aac13..ecadaf8 100644
--- a/3rdparty/stout/include/stout/flags/flags.hpp
+++ b/3rdparty/stout/include/stout/flags/flags.hpp
@@ -298,6 +298,10 @@ public:
   std::map<std::string, Option<std::string>> extract(
       const std::string& prefix) const;
 
+  // Build environment variables from the flags.
+  std::map<std::string, std::string> buildEnvironment(
+      const Option<std::string>& prefix = None()) const;
+
 protected:
   // The program's name, extracted from argv[0] by default;
   // declared 'protected' so that derived classes can alter this
@@ -538,6 +542,26 @@ inline std::map<std::string, Option<std::string>> FlagsBase::extract(
 }
 
 
+inline std::map<std::string, std::string> FlagsBase::buildEnvironment(
+    const Option<std::string>& prefix) const
+{
+  std::map<std::string, std::string> result;
+
+  foreachvalue (const Flag& flag, flags_) {
+    Option<std::string> value = flag.stringify(*this);
+    if (value.isSome()) {
+      const std::string key = prefix.isSome()
+        ? prefix.get() + strings::upper(flag.effective_name().value)
+        : strings::upper(flag.effective_name().value);
+
+      result[key] = value.get();
+    }
+  }
+
+  return result;
+}
+
+
 inline Try<Warnings> FlagsBase::load(const std::string& prefix)
 {
   return load(extract(prefix));

http://git-wip-us.apache.org/repos/asf/mesos/blob/c06c6976/3rdparty/stout/tests/flags_tests.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/stout/tests/flags_tests.cpp b/3rdparty/stout/tests/flags_tests.cpp
index 9ee538b..e2681f8 100644
--- a/3rdparty/stout/tests/flags_tests.cpp
+++ b/3rdparty/stout/tests/flags_tests.cpp
@@ -890,6 +890,29 @@ TEST(FlagsTest, OptionalMessage)
 }
 
 
+TEST(FlagsTest, BuildEnvironment)
+{
+  TestFlagsBase flags;
+  flags.name4 = true;
+
+  map<string, string> environment = flags.buildEnvironment("PREFIX_");
+
+  ASSERT_EQ(1u, environment.count("PREFIX_NAME1"));
+  EXPECT_EQ("ben folds", environment["PREFIX_NAME1"]);
+
+  ASSERT_EQ(1u, environment.count("PREFIX_NAME2"));
+  EXPECT_EQ("42", environment["PREFIX_NAME2"]);
+
+  ASSERT_EQ(1u, environment.count("PREFIX_NAME3"));
+  EXPECT_EQ("false", environment["PREFIX_NAME3"]);
+
+  ASSERT_EQ(1u, environment.count("PREFIX_NAME4"));
+  EXPECT_EQ("true", environment["PREFIX_NAME4"]);
+
+  EXPECT_EQ(0u, environment.count("PREFIX_NAME5"));
+}
+
+
 TEST(FlagsTest, Duration)
 {
   class TestFlags : public virtual FlagsBase