]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Add a command alias table. 15207/head
authorMiod Vallat <miod.vallat@open-xchange.com>
Thu, 23 Jan 2025 14:28:26 +0000 (15:28 +0100)
committerMiod Vallat <miod.vallat@powerdns.com>
Mon, 24 Feb 2025 09:56:56 +0000 (10:56 +0100)
This replaces "did you mean bar?" when running pdnsutil foo, and instead
invokes bar directly.

pdns/pdnsutil.cc

index c2449d36b523479a63d875c3e69daddaf10842f9..b896e7f6fbdbf6b0da7c68bccc48d589134fc4b8 100644 (file)
@@ -2748,18 +2748,6 @@ static int listMemberZones(vector<string>& cmds, const std::string_view synopsis
   return listMemberZones(cmds.at(1));
 }
 
-static int testZone([[maybe_unused]] vector<string>& cmds, [[maybe_unused]] const std::string_view synopsis)
-{
-  cerr << "Did you mean check-zone?"<<endl;
-  return 0;
-}
-
-static int testAllZones([[maybe_unused]] vector<string>& cmds, [[maybe_unused]] const std::string_view synopsis)
-{
-  cerr << "Did you mean check-all-zones?"<<endl;
-  return 0;
-}
-
 static int testSpeed(vector<string>& cmds, const std::string_view synopsis)
 {
   if(cmds.size() < 3) {
@@ -4702,15 +4690,11 @@ static const std::unordered_map<std::string, commandDispatcher> commands{
    ""}}, // TODO: short help line
   {"test-algorithms", {false, testAlgorithms, GROUP_OTHER,
    "", ""}}, // TODO: synopsis and short help line
-  {"test-all-zones", {true, testAllZones, GROUP_ZONE,
-   "", ""}}, // TODO: synopsis and short help line
   {"test-schema", {true, testSchema, GROUP_OTHER,
    "test-schema ZONE",
    "\tTest DB schema - will create ZONE"}},
   {"test-speed", {true, testSpeed, GROUP_OTHER,
    "test-speed ZONE NUM_CORES", ""}}, // TODO: short help line
-  {"test-zone", {true, testZone, GROUP_ZONE,
-   "", ""}}, // TODO: synopsis and short help line
   {"unpublish-zone-key", {true, unpublishZoneKey, GROUP_ZONEKEY,
    "unpublish-zone-key ZONE KEY_ID",
    "\tUnpublish the zone key with key id KEY_ID in ZONE"}},
@@ -4734,6 +4718,11 @@ static const std::unordered_map<std::string, commandDispatcher> commands{
 };
 // clang-format on
 
+static const std::unordered_map<std::string, std::string> aliases{
+  {"test-zone", "check-zone"},
+  {"test-all-zones", "check-all-zones"}
+};
+
 int main(int argc, char** argv)
 try
 {
@@ -4797,13 +4786,22 @@ try
 
   loadMainConfig(g_vm["config-dir"].as<string>());
 
-  const auto iter = commands.find(cmds.at(0));
-  if (iter != commands.end()) {
-    const auto dispatcher = iter->second;
-    if (dispatcher.requiresInitialization) {
+  const commandDispatcher* dispatcher{nullptr};
+  if (const auto iter = commands.find(cmds.at(0)); iter != commands.end()) {
+    dispatcher = &iter->second;
+  }
+  if (dispatcher == nullptr) {
+    if (const auto alias = aliases.find(cmds.at(0)); alias != aliases.end()) {
+      if (const auto iter = commands.find(alias->second); iter != commands.end()) {
+        dispatcher = &iter->second;
+      }
+    }
+  }
+  if (dispatcher != nullptr) {
+    if (dispatcher->requiresInitialization) {
       reportAllTypes();
     }
-    return dispatcher.handler(cmds, dispatcher.synopsis);
+    return dispatcher->handler(cmds, dispatcher->synopsis);
   }
 
   cerr << "Unknown command '" << cmds.at(0) << "'" << endl;