You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by be...@apache.org on 2011/06/05 07:41:19 UTC

svn commit: r1131856 - in /incubator/mesos/trunk/src: configuration.cpp configuration.hpp tests/test_configuration.cpp

Author: benh
Date: Sun Jun  5 05:41:18 2011
New Revision: 1131856

URL: http://svn.apache.org/viewvc?rev=1131856&view=rev
Log:
loading of env, cmd, and conf must be done manually now, which makes more sense. that way, a Configuration is created, default params added, and then it's used. some wrapper methods can then call the different loaders...

Modified:
    incubator/mesos/trunk/src/configuration.cpp
    incubator/mesos/trunk/src/configuration.hpp
    incubator/mesos/trunk/src/tests/test_configuration.cpp

Modified: incubator/mesos/trunk/src/configuration.cpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/src/configuration.cpp?rev=1131856&r1=1131855&r2=1131856&view=diff
==============================================================================
--- incubator/mesos/trunk/src/configuration.cpp (original)
+++ incubator/mesos/trunk/src/configuration.cpp Sun Jun  5 05:41:18 2011
@@ -21,23 +21,6 @@ const char* Configuration::CONFIG_FILE_N
 const char* Configuration::ENV_VAR_PREFIX = "MESOS_";
 
 
-Configuration::Configuration() 
-{
-  loadEnv();
-  loadConfigFileIfGiven();
-}
-
-
-Configuration::Configuration(int argc,
-                             char** argv,
-                             bool inferMesosHomeFromArg0) 
-{
-  loadEnv();
-  loadCommandLine(argc, argv, inferMesosHomeFromArg0);
-  loadConfigFileIfGiven();
-}
-
-
 Configuration::Configuration(const map<string, string>& _params) 
 {
   loadEnv();
@@ -46,18 +29,18 @@ Configuration::Configuration(const map<s
 }
 
 
-void Configuration::loadConfigFileIfGiven() {
+void Configuration::loadConfigFileIfGiven(bool overwrite) {
   string confDir = "";
   if (params.contains("conf"))
     confDir = params["conf"];
   else if (params.contains("home")) // find conf dir relative to MESOS_HOME
     confDir = params["home"] + "/" + DEFAULT_CONFIG_DIR;
   if (confDir != "")
-    loadConfigFile(confDir + "/" + CONFIG_FILE_NAME);
+    loadConfigFile(confDir + "/" + CONFIG_FILE_NAME, overwrite);
 }
 
 
-void Configuration::loadEnv()
+void Configuration::loadEnv(bool overwrite)
 {
   int i = 0;
   while (environ[i] != NULL) {
@@ -70,7 +53,9 @@ void Configuration::loadEnv()
       key = line.substr(strlen(ENV_VAR_PREFIX), eq - strlen(ENV_VAR_PREFIX));
       std::transform(key.begin(), key.end(), key.begin(), ::tolower);
       val = line.substr(eq + 1);
-      params[key] = val;
+      if (overwrite || !params.contains(key)) {
+        params[key] = val;
+      }
     }
     i++;
   }
@@ -79,7 +64,8 @@ void Configuration::loadEnv()
 
 void Configuration::loadCommandLine(int argc,
                                     char** argv,
-                                    bool inferMesosHomeFromArg0)
+                                    bool inferMesosHomeFromArg0,
+                                    bool overwrite)
 {
   // Set home based on argument 0 if asked to do so
   if (inferMesosHomeFromArg0) {
@@ -131,14 +117,14 @@ void Configuration::loadCommandLine(int 
         i++;  // we've consumed next parameter as a "value"-parameter
       }
     }
-    if (set) {
+    if (set && (overwrite || !params.contains(key))) {
       params[key] = val;
     }
   }
 }
 
 
-void Configuration::loadConfigFile(const string& fname) 
+void Configuration::loadConfigFile(const string& fname, bool overwrite) 
 {
   ifstream cfg(fname.c_str(), std::ios::in);
   if (!cfg.is_open()) {
@@ -159,12 +145,13 @@ void Configuration::loadConfigFile(const
   fileParams.loadString(buf); // Parse key=value pairs using Params's code
 
   foreachpair (const string& key, const string& value, fileParams.getMap()) {
-    if (!params.contains(key)) {
+    if (overwrite || !params.contains(key)) {
       params[key] = value;
     }
   }
 }
 
+
 string Configuration::getUsage() const 
 {
   const int PAD = 10;
@@ -214,6 +201,7 @@ int Configuration::addOption(string optN
   return 0;
 }
 
+
 string Configuration::getOptionDefault(string optName) const
 {
   std::transform(optName.begin(), optName.end(), optName.begin(), ::tolower);
@@ -222,6 +210,7 @@ string Configuration::getOptionDefault(s
   return options.find(optName)->second.defaultValue;
 }
 
+
 vector<string> Configuration::getOptions() const 
 {
   vector<string> ret;

Modified: incubator/mesos/trunk/src/configuration.hpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/src/configuration.hpp?rev=1131856&r1=1131855&r2=1131856&view=diff
==============================================================================
--- incubator/mesos/trunk/src/configuration.hpp (original)
+++ incubator/mesos/trunk/src/configuration.hpp Sun Jun  5 05:41:18 2011
@@ -68,22 +68,11 @@ private:
   map<string, Option> options;
 
 public:
-  /** 
-   * Constructor that populates Params from environment and any config file
-   * located through the environment (through MESOS_HOME or MESOS_CONF).
-   **/
-  Configuration();
 
   /** 
-   * Constructor that populates Params from environment, command line,
-   * and any config file specified through these.
-   *
-   * @param argc number of paramters in argv
-   * @param argv array of c-strings from the command line
-   * @param inferMesosHomeFromArg0 whether to set mesos home to directory
-   *                               containing argv[0] (the program being run)
+   * Constructor that initializes an empty Params
    **/
-  Configuration(int argc, char** argv, bool inferMesosHomeFromArg0);
+  Configuration() {}
 
   /** 
    * Constructor that populates Params from environment, a map,
@@ -157,13 +146,14 @@ public:
    **/
   vector<string> getOptions() const;
 
-private:
   /**
    * Parses the environment variables and populates a Params.
    * It picks all environment variables that start with MESOS_.
    * The environment variable MESOS_HOME=/dir would lead to key=HOME val=/dir
+   * @param overwrite whether to overwrite keys that already have values 
+   *         in the internal params (true by default)
    **/
-  void loadEnv();
+  void loadEnv(bool overwrite=true);
 
   /**
    * Populates its internal Params with key/value params from command line.
@@ -175,8 +165,11 @@ private:
    * @param argv is an array of c-strings containing params
    * @param inferMesosHomeFromArg0 whether to set mesos home to directory
    *                               containing argv[0] (the program being run)
+   * @param overwrite whether to overwrite keys that already have values 
+   *         in the internal params (true by default)
    **/
-  void loadCommandLine(int argc, char** argv, bool inferMesosHomeFromArg0);
+  void loadCommandLine(int argc, char** argv, bool inferMesosHomeFromArg0, 
+                       bool overwrite=true);
 
   /**
    * Populates its internal Params with key/value params from a config file.
@@ -188,13 +181,17 @@ private:
    * Comments, which should start with #, are ignored.
    *
    * @param fname is the name of the config file to open
+   * @param overwrite whether to overwrite keys that already have values 
+   *         in the internal params (false by default)
    **/
-  void loadConfigFile(const string& fname);
+  void loadConfigFile(const string& fname, bool overwrite=false);
 
   /**
    * Load the config file set through the command line or environment, if any.
+   * @param overwrite whether to overwrite keys that already have values 
+   *         in the internal params (false by default)
    */
-  void loadConfigFileIfGiven();
+  void loadConfigFileIfGiven(bool overwrite=false);
 
 };
 

Modified: incubator/mesos/trunk/src/tests/test_configuration.cpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/src/tests/test_configuration.cpp?rev=1131856&r1=1131855&r2=1131856&view=diff
==============================================================================
--- incubator/mesos/trunk/src/tests/test_configuration.cpp (original)
+++ incubator/mesos/trunk/src/tests/test_configuration.cpp Sun Jun  5 05:41:18 2011
@@ -23,6 +23,8 @@ TEST(ConfigurationTest, Environment)
 {
   setenv("MESOS_TEST", "working", true);
   Configuration conf;
+  conf.loadEnv();
+  conf.loadConfigFileIfGiven();
   unsetenv("MESOS_TEST");
 
   EXPECT_EQ("working", conf.getParams()["test"]);
@@ -37,18 +39,24 @@ TEST(ConfigurationTest, DefaultOptions)
   argv[1] = (char*) "--test1=text1";
   argv[2] = (char*) "--test2";
 
-  Configuration conf(ARGC, argv, false);
+  Configuration conf;
+
   conf.addOption("test1", "Testing option", 500);
   conf.addOption("test2", "Another tester", 0);
   conf.addOption("test3", "Tests the default\noption.", 2010);
   conf.addOption("test4", "Option without default\noption.");
 
+  conf.loadEnv();
+  conf.loadCommandLine(ARGC, argv, false);
+  conf.loadConfigFileIfGiven();
+
   EXPECT_EQ("text1",  conf.getParams()["test1"]);
   EXPECT_EQ("1",      conf.getParams()["test2"]);
   EXPECT_EQ("2010",   conf.getParams()["test3"]);
   EXPECT_EQ("",       conf.getParams()["test4"]);
 }
 
+
 TEST(ConfigurationTest, CommandLine)
 {
   const int ARGC = 10;
@@ -64,7 +72,10 @@ TEST(ConfigurationTest, CommandLine)
   argv[8] = (char*) "--cAsE=4";
   argv[9] = (char*) "--space=Long String";
 
-  Configuration conf(ARGC, argv, false);
+  Configuration conf;
+  conf.loadEnv();
+  conf.loadCommandLine(ARGC, argv, false);
+  conf.loadConfigFileIfGiven();
 
   EXPECT_EQ("text1",       conf.getParams()["test1"]);
   EXPECT_EQ("1",           conf.getParams()["test2"]);
@@ -90,6 +101,8 @@ TEST_WITH_WORKDIR(ConfigurationTest, Con
 
   setenv("MESOS_HOME", ".", 1);
   Configuration conf;
+  conf.loadEnv();
+  conf.loadConfigFileIfGiven();
   unsetenv("MESOS_HOME");
 
   EXPECT_EQ("coffee", conf.getParams()["test1"]);
@@ -109,6 +122,8 @@ TEST_WITH_WORKDIR(ConfigurationTest, Con
   file.close();
   setenv("MESOS_CONF", "conf2", 1);
   Configuration conf;
+  conf.loadEnv();
+  conf.loadConfigFileIfGiven();
   unsetenv("MESOS_CONF");
 
   EXPECT_EQ("shake", conf.getParams()["test3"]);
@@ -130,6 +145,8 @@ TEST_WITH_WORKDIR(ConfigurationTest, Con
   setenv("MESOS_HOME", ".", 1);
   setenv("MESOS_CONF", "conf2", 1);
   Configuration conf;
+  conf.loadEnv();
+  conf.loadConfigFileIfGiven();
   unsetenv("MESOS_CONF");
   unsetenv("MESOS_HOME");
 
@@ -157,7 +174,10 @@ TEST_WITH_WORKDIR(ConfigurationTest, Com
   argv[2] = (char*) "--b=overridden";
   argv[3] = (char*) "--d=fromCmdLine";
 
-  Configuration conf(ARGC, argv, false);
+  Configuration conf;
+  conf.loadEnv();
+  conf.loadCommandLine(ARGC, argv, false);
+  conf.loadConfigFileIfGiven();
 
   EXPECT_EQ("1",           conf.getParams()["a"]);
   EXPECT_EQ("overridden",  conf.getParams()["b"]);
@@ -194,7 +214,10 @@ TEST_WITH_WORKDIR(ConfigurationTest, Loa
   argv[1] = (char*) "--a=fromCmdLine";
   argv[2] = (char*) "--c=fromCmdLine";
 
-  Configuration conf(ARGC, argv, false);
+  Configuration conf;
+  conf.loadEnv();
+  conf.loadCommandLine(ARGC, argv, false);
+  conf.loadConfigFileIfGiven();
 
   // Clear the environment vars set above
   unsetenv("MESOS_HOME");