[Pki-devel] [PATCH] 624 Fixed pki help CLI.

Endi Sukma Dewata edewata at redhat.com
Wed Jul 1 04:18:45 UTC 2015


A new findModules() method has been added to the CLI class to find
the list of modules handling a command. The list will be used by the
pki help CLI to find the proper man page for the specified command.

-- 
Endi S. Dewata
-------------- next part --------------
From e4aa78e3abfd5f00795cb88ca36a4c0c04cd7a1f Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata at redhat.com>
Date: Tue, 30 Jun 2015 22:49:11 -0400
Subject: [PATCH] Fixed pki help CLI.

A new findModules() method has been added to the CLI class to find
the list of modules handling a command. The list will be used by the
pki help CLI to find the proper man page for the specified command.
---
 .../src/com/netscape/cmstools/cert/CertCLI.java    |  4 ++
 .../src/com/netscape/cmstools/cli/CLI.java         | 72 ++++++++++++++++++++++
 .../src/com/netscape/cmstools/cli/HelpCLI.java     | 27 ++++++--
 .../src/com/netscape/cmstools/cli/MainCLI.java     |  4 ++
 .../com/netscape/cmstools/client/ClientCLI.java    |  4 ++
 .../src/com/netscape/cmstools/group/GroupCLI.java  |  4 ++
 .../netscape/cmstools/group/GroupMemberCLI.java    |  4 ++
 .../src/com/netscape/cmstools/key/KeyCLI.java      |  4 ++
 .../com/netscape/cmstools/logging/AuditCLI.java    |  4 ++
 .../com/netscape/cmstools/profile/ProfileCLI.java  |  4 ++
 .../cmstools/system/SecurityDomainCLI.java         |  4 ++
 .../src/com/netscape/cmstools/user/UserCLI.java    |  4 ++
 .../com/netscape/cmstools/user/UserCertCLI.java    |  4 ++
 13 files changed, 137 insertions(+), 6 deletions(-)

diff --git a/base/java-tools/src/com/netscape/cmstools/cert/CertCLI.java b/base/java-tools/src/com/netscape/cmstools/cert/CertCLI.java
index 9ffa3ad4501bed4fc6102e5b80ab11113883d74e..ee8e63e4ce9dadea665f6c4fc2e406ec15d1852c 100644
--- a/base/java-tools/src/com/netscape/cmstools/cert/CertCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/cert/CertCLI.java
@@ -67,6 +67,10 @@ public class CertCLI extends CLI {
         }
     }
 
+    public String getManPage() {
+        return "pki-cert";
+    }
+
     public void execute(String[] args) throws Exception {
 
         client = parent.getClient();
diff --git a/base/java-tools/src/com/netscape/cmstools/cli/CLI.java b/base/java-tools/src/com/netscape/cmstools/cli/CLI.java
index ed01edc9ad0b2d2ffbdd1c4fb706b5077f7283c0..13387495b72afa71a85e9ce74157603b8bf54b68 100644
--- a/base/java-tools/src/com/netscape/cmstools/cli/CLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/cli/CLI.java
@@ -21,6 +21,7 @@ package com.netscape.cmstools.cli;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.LinkedHashMap;
+import java.util.List;
 import java.util.Map;
 
 import org.apache.commons.cli.CommandLineParser;
@@ -113,6 +114,75 @@ public class CLI {
         return modules.remove(name);
     }
 
+    /**
+     * Find the list of modules that handle the specified command.
+     */
+    public List<CLI> findModules(String command) throws Exception {
+
+        List<CLI> results = new ArrayList<CLI>();
+
+        // split command into list of names:
+        // <names[0]>-<names[1]>-<names[2]>-...-<names[n-1]>
+        String[] names = command.split("-");
+
+        CLI current = this;
+        int i = 0;
+
+        // translate all names into modules starting from the beginning
+        while (i < names.length) {
+
+            String moduleName = null;
+            CLI module = null;
+            int j = i;
+
+            // find module that matches the shortest sequence of names
+            while (j < names.length) {
+
+                // construct module name
+                if (moduleName == null) {
+                    moduleName = names[j];
+                } else {
+                    moduleName = moduleName + "-" + names[j];
+                }
+
+                // find module with name <names[i]>-...-<names[j]>
+                module = current.getModule(moduleName);
+
+                if (module != null) {
+                    // module found, stop
+                    break;
+                }
+
+                // try again with longer sequence
+                j++;
+            }
+
+            if (module == null)
+                throw new Error("Invalid module \"" + moduleName + "\".");
+
+            // module found
+            results.add(module);
+
+            // repeat for the remaining parts
+            current = module;
+            i = j + 1;
+        }
+
+        return results;
+    }
+
+    /**
+     * Find the last module that handles the specified command.
+     */
+    public CLI findModule(String command) throws Exception {
+        List<CLI> modules = findModules(command);
+        return modules.get(modules.size() - 1);
+    }
+
+    public String getManPage() {
+        return null;
+    }
+
     public PKIClient getClient() {
         return client;
     }
@@ -182,6 +252,8 @@ public class CLI {
             System.exit(0);
         }
 
+        // TODO: Rewrite using findModules().
+
         // A command consists of parts joined by dashes: <part 1>-<part 2>-...-<part N>.
         // For example: cert-request-find
         String command = args[0];
diff --git a/base/java-tools/src/com/netscape/cmstools/cli/HelpCLI.java b/base/java-tools/src/com/netscape/cmstools/cli/HelpCLI.java
index 6b2a123d004dc71449fc7dae051f941911bae87c..b348ffc2042cf02bc01005fe7e02f8ae7409ad82 100644
--- a/base/java-tools/src/com/netscape/cmstools/cli/HelpCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/cli/HelpCLI.java
@@ -18,6 +18,8 @@
 
 package com.netscape.cmstools.cli;
 
+import java.util.List;
+
 import org.apache.commons.cli.CommandLine;
 
 /**
@@ -51,19 +53,32 @@ public class HelpCLI extends CLI {
 
         String[] cmdArgs = cmd.getArgs();
 
-        String command;
+        String manPage = null;
         if (cmdArgs.length == 0) {
-            command = "pki";
+            // no command specified, show the pki man page
+            manPage = parent.getManPage();
 
         } else {
-            command = "pki-" + cmdArgs[0];
+            // find all modules handling the specified command
+            List<CLI> modules = parent.findModules(cmdArgs[0]);
+
+            // find the module that has a man page starting from the last one
+            for (int i = modules.size() - 1; i >= 0; i--) {
+                CLI module = modules.get(i);
+                manPage = module.getManPage();
+                if (manPage != null) break;
+            }
+
+            // if no module has a man page, show the pki man page
+            if (manPage == null)
+                manPage = parent.getManPage();
         }
 
         while (true) {
             // display man page for the command
             ProcessBuilder pb = new ProcessBuilder(
                     "/bin/man",
-                    command);
+                    manPage);
 
             pb.inheritIO();
             Process p = pb.start();
@@ -71,10 +86,10 @@ public class HelpCLI extends CLI {
 
             if (rc == 16) {
                 // man page not found, find the parent command
-                int i = command.lastIndexOf('-');
+                int i = manPage.lastIndexOf('-');
                 if (i >= 0) {
                     // parent command exists, try again
-                    command = command.substring(0, i);
+                    manPage = manPage.substring(0, i);
                     continue;
 
                 } else {
diff --git a/base/java-tools/src/com/netscape/cmstools/cli/MainCLI.java b/base/java-tools/src/com/netscape/cmstools/cli/MainCLI.java
index 17929221bd927454459da907bd2ad5d53ee4b507..8f655a0449e359012f28646ed4042491420b1c8a 100644
--- a/base/java-tools/src/com/netscape/cmstools/cli/MainCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/cli/MainCLI.java
@@ -89,6 +89,10 @@ public class MainCLI extends CLI {
         return moduleName;
     }
 
+    public String getManPage() {
+        return "pki";
+    }
+
     public void printVersion() {
         Package pkg = MainCLI.class.getPackage();
         System.out.println("PKI Command-Line Interface "+pkg.getImplementationVersion());
diff --git a/base/java-tools/src/com/netscape/cmstools/client/ClientCLI.java b/base/java-tools/src/com/netscape/cmstools/client/ClientCLI.java
index c9c71521ab631a02d7c386bd4e1b38f8ecbc1e7f..3764c3d23d32da4deba568ffac67736d0944c12c 100644
--- a/base/java-tools/src/com/netscape/cmstools/client/ClientCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/client/ClientCLI.java
@@ -50,6 +50,10 @@ public class ClientCLI extends CLI {
         }
     }
 
+    public String getManPage() {
+        return "pki-client";
+    }
+
     public void execute(String[] args) throws Exception {
 
         client = parent.getClient();
diff --git a/base/java-tools/src/com/netscape/cmstools/group/GroupCLI.java b/base/java-tools/src/com/netscape/cmstools/group/GroupCLI.java
index 973e0bae6d0652923c1f9f3b5ced78682e534730..22263dce110d717f02c7ec860c24351ce1064b1d 100644
--- a/base/java-tools/src/com/netscape/cmstools/group/GroupCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/group/GroupCLI.java
@@ -54,6 +54,10 @@ public class GroupCLI extends CLI {
         }
     }
 
+    public String getManPage() {
+        return "pki-group";
+    }
+
     public void execute(String[] args) throws Exception {
 
         client = parent.getClient();
diff --git a/base/java-tools/src/com/netscape/cmstools/group/GroupMemberCLI.java b/base/java-tools/src/com/netscape/cmstools/group/GroupMemberCLI.java
index e21d8175b7dcfa9160ffeb6a9be9d0e5a29d8908..ecafac1dc78a7a26c7a1ab42f04a40284ea69837 100644
--- a/base/java-tools/src/com/netscape/cmstools/group/GroupMemberCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/group/GroupMemberCLI.java
@@ -40,6 +40,10 @@ public class GroupMemberCLI extends CLI {
         addModule(new GroupMemberRemoveCLI(this));
     }
 
+    public String getManPage() {
+        return "pki-group-member";
+    }
+
     public void execute(String[] args) throws Exception {
 
         client = parent.getClient();
diff --git a/base/java-tools/src/com/netscape/cmstools/key/KeyCLI.java b/base/java-tools/src/com/netscape/cmstools/key/KeyCLI.java
index fb324be25bb1680ebe987cefbcaa41f2ae54182e..955369e2df7f5a98170b580b77245e4cf97e22c9 100644
--- a/base/java-tools/src/com/netscape/cmstools/key/KeyCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/key/KeyCLI.java
@@ -65,6 +65,10 @@ public class KeyCLI extends CLI {
         }
     }
 
+    public String getManPage() {
+        return "pki-key";
+    }
+
     public void execute(String[] args) throws Exception {
 
         client = parent.getClient();
diff --git a/base/java-tools/src/com/netscape/cmstools/logging/AuditCLI.java b/base/java-tools/src/com/netscape/cmstools/logging/AuditCLI.java
index 11e530066026043992e07ceb0dbfad5cd452933e..b911a13b6811073d996166d7f70e62931fa30c23 100644
--- a/base/java-tools/src/com/netscape/cmstools/logging/AuditCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/logging/AuditCLI.java
@@ -41,6 +41,10 @@ public class AuditCLI extends CLI {
         addModule(new AuditShowCLI(this));
     }
 
+    public String getManPage() {
+        return "pki-audit";
+    }
+
     public void execute(String[] args) throws Exception {
 
         client = parent.getClient();
diff --git a/base/java-tools/src/com/netscape/cmstools/profile/ProfileCLI.java b/base/java-tools/src/com/netscape/cmstools/profile/ProfileCLI.java
index e9e21596a0593d63b417d6d8560f3ff9b54caaf0..2ac54f5445cd4f4fa6259950a203206cb5cbd2f8 100644
--- a/base/java-tools/src/com/netscape/cmstools/profile/ProfileCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/profile/ProfileCLI.java
@@ -51,6 +51,10 @@ public class ProfileCLI extends CLI {
         }
     }
 
+    public String getManPage() {
+        return "pki-ca-profile";
+    }
+
     public void execute(String[] args) throws Exception {
 
         client = parent.getClient();
diff --git a/base/java-tools/src/com/netscape/cmstools/system/SecurityDomainCLI.java b/base/java-tools/src/com/netscape/cmstools/system/SecurityDomainCLI.java
index b1a35978800f71ac180e0e715532ac9ceb59f205..86d3ade967a46e405cbefb9484ed83f282493ce9 100644
--- a/base/java-tools/src/com/netscape/cmstools/system/SecurityDomainCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/system/SecurityDomainCLI.java
@@ -47,6 +47,10 @@ public class SecurityDomainCLI extends CLI {
         }
     }
 
+    public String getManPage() {
+        return "pki-securitydomain";
+    }
+
     public void execute(String[] args) throws Exception {
 
         client = parent.getClient();
diff --git a/base/java-tools/src/com/netscape/cmstools/user/UserCLI.java b/base/java-tools/src/com/netscape/cmstools/user/UserCLI.java
index 7a03d3384fbe3cc47276442461b8b959702afcf9..657dd22914a682e9f60817b7b7a63bf583f7421a 100644
--- a/base/java-tools/src/com/netscape/cmstools/user/UserCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/user/UserCLI.java
@@ -57,6 +57,10 @@ public class UserCLI extends CLI {
         }
     }
 
+    public String getManPage() {
+        return "pki-user";
+    }
+
     public void execute(String[] args) throws Exception {
 
         client = parent.getClient();
diff --git a/base/java-tools/src/com/netscape/cmstools/user/UserCertCLI.java b/base/java-tools/src/com/netscape/cmstools/user/UserCertCLI.java
index ead915a6697a1df636242360a917c8051781b8e0..3081931b9ffbf5ca0f8cc3c80fac77b25e13e321 100644
--- a/base/java-tools/src/com/netscape/cmstools/user/UserCertCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/user/UserCertCLI.java
@@ -40,6 +40,10 @@ public class UserCertCLI extends CLI {
         addModule(new UserCertRemoveCLI(this));
     }
 
+    public String getManPage() {
+        return "pki-user-cert";
+    }
+
     public void execute(String[] args) throws Exception {
 
         client = parent.getClient();
-- 
1.9.3



More information about the Pki-devel mailing list