Kaynağa Gözat

config debug tool warnUnused

Wolfgang Ortmann 9 yıl önce
ebeveyn
işleme
277ca886ea
3 değiştirilmiş dosya ile 32 ekleme ve 13 silme
  1. 16 1
      src/KindConfig.cpp
  2. 3 0
      src/KindConfig.h
  3. 13 12
      src/kind.ag

+ 16 - 1
src/KindConfig.cpp

@@ -55,7 +55,10 @@ bool KindConfig::hasKey(std::string key) const
 {
   key = trim(key);
   auto it = settings.find(key);
-  return it != settings.end();
+  bool found = it != settings.end();
+  if (found)
+    used[key]=true;
+  return found;
 }
 
 std::string KindConfig::getString(std::string key) const
@@ -64,6 +67,7 @@ std::string KindConfig::getString(std::string key) const
   auto it = settings.find(key);
   if (it == settings.end())
     throw Exception("get config key", std::string("No key \"") + key + "\"");
+  used[key]=true;
   if (it->second.size() != 1)
     throw Exception("get config key", std::string("Key \"") + key + "\" is no single value");
   return it->second[0];
@@ -75,6 +79,7 @@ Strings KindConfig::getStrings(std::string key) const
   auto it = settings.find(key);
   if (it == settings.end())
     throw Exception("get config key", std::string("No key \"") + key + "\"");
+  used[key]=true;
   return it->second;
 }
 
@@ -84,6 +89,7 @@ bool KindConfig::getBool(std::string key) const
   auto it = settings.find(key);
   if (it == settings.end())
     return false;
+  used[key]=true;
   if (it->second.size() != 1)
     throw Exception("get config key", std::string("Key \"") + key + "\" is no single value");
   std::string val = it->second[0];
@@ -108,6 +114,15 @@ void KindConfig::print(const string& prefix) const
     }
 }
 
+void KindConfig::warnUnused(const string& prefix) const
+{
+  for (auto it = settings.begin(); it != settings.end(); ++it)
+    {
+      if (used.count(it->first) == 0 || ! used[it->first])
+	cout << "Warning: setting " << it->first << " unused" << endl;
+    }
+}
+
 void KindConfig::split(const string& line,
                        string& key,
                        string& del,

+ 3 - 0
src/KindConfig.h

@@ -41,6 +41,8 @@ public:
   bool getBool(std::string key) const;
 
   void print(const std::string& prefix = "") const;
+  void warnUnused(const std::string& prefix = "") const;
+
 private:
   void split(const std::string& line,
              std::string& key,
@@ -48,6 +50,7 @@ private:
              std::string& value);
 
   std::map<std::string, Strings> settings;
+  mutable std::map<std::string, bool> used;
 };
 
 #endif

+ 13 - 12
src/kind.ag

@@ -153,7 +153,7 @@ string findVault(const string& v)
     }
   if (!found)
     throw Exception("find vault", v + " not found");
-  verbosePrint("using vault " + fn.getFileName());
+  verbosePrint("found vault " + fn.getFileName());
   return fn.getFileName();
 }
 
@@ -202,7 +202,6 @@ void readVaultConfig(const string& vault, KindConfig& conf)
 }
 
 string getImageName(const KindConfig& conf,
-                    const string& vaultPath,
                     const DateTime& imageTime)
 {
   bool nonPortable = false;
@@ -219,7 +218,7 @@ string getImageName(const KindConfig& conf,
   if (!imageName.empty())
     imageName += '-';
 
-  string imageFullName =  vaultPath + "/" + imageName ;
+  string imageFullName =  conf.getString("vaultPath") + "/" + imageName ;
 
   if (conf.getBool("longImageName"))
     imageFullName += imageTime.getString('m');
@@ -260,7 +259,7 @@ void listImageInfo(const string& vault,
                    const string& backupSet)
 {
   readVaultConfig(vault, conf);
-  string vaultPath = findVault(vault);
+  string vaultPath = conf.getString("vaultPath");
   Images imageList = findImages(vaultPath, conf, true);
   cout << "== " << vault << " ==" << endl;
   for (auto img : imageList)
@@ -422,10 +421,10 @@ bool backupVault(const string& vault,
       readVaultConfig(vault, conf);
 
       // where to store
-      string vaultPath = findVault(vault);
+      string vaultPath = conf.getString("vaultPath");
 
       // image path
-      string imageFullName = getImageName(conf, vaultPath, imageTime);
+      string imageFullName = getImageName(conf, imageTime);
 
       bool backupNow = true;
 
@@ -439,7 +438,7 @@ bool backupVault(const string& vault,
       vector<SetRule> backupSetRule;
       int setRuleIdx = -1;
 
-      if (conf.hasKey("setRule"))
+      if (conf.getBool("useBackupSet"))
         {
           readSetRules(conf, setIdx, backupSetRule);
           if (!setIdx.empty())
@@ -531,7 +530,7 @@ bool backupVault(const string& vault,
               string lastPath = vaultPath + "/last";
               struct stat fstat;
 
-              // remove last (dir or symlink)
+              // remove "last" (dir or symlink)
               if (lstat(lastPath.c_str(), &fstat) == 0) // last exists
                 {
                   if (S_ISDIR(fstat.st_mode))
@@ -553,7 +552,7 @@ bool backupVault(const string& vault,
                   symlink(imageFullName.c_str(), lastPath.c_str());
                 }
               else if (linkType != "null")
-                cerr << "invalid Value in \"lastLink\"" << endl;
+                cerr << "invalid value in \"lastLink\"" << endl;
 
               // write expire date to file
               DateTime expireTime;
@@ -570,6 +569,7 @@ bool backupVault(const string& vault,
               expireFile << currentSet << "-" << expireTime.getString('m') << endl;
               expireFile << rule << endl;
             }
+	  //  conf.warnUnused();
         }
       return backupNow;
     }
@@ -587,9 +587,9 @@ void expireVault(const string& vault, KindConfig conf, DateTime now)
 
   readVaultConfig(vault, conf);
 
-  string vaultpath = findVault(vault);
+  string vaultPath = conf.getString("vaultPath");
 
-  Images imagelist = findImages(vaultpath, conf, true);
+  Images imagelist = findImages(vaultPath, conf, true);
 
   string lastValidImage;
   for (Image image : imagelist)
@@ -616,7 +616,7 @@ void expireVault(const string& vault, KindConfig conf, DateTime now)
               time_t expPeriod = stot(conf.getString("expireFailedImage"));
               if (expPeriod < 0)
                 throw Exception("expireFailedImage", "Time period must be positive");
-              expireTime = imageTime + stot(conf.getString("expireFailedImage"));
+              expireTime = imageTime + expPeriod;
               expireRule = "invalid image: " + conf.getString("expireFailedImage");
               debugPrint("- invalid image");
             }
@@ -645,6 +645,7 @@ void expireVault(const string& vault, KindConfig conf, DateTime now)
       else
         debugPrint("- current image - ignored");
     }
+  // conf.warnUnused();
 }
 
 /*AppGen:Main*/