Do not lookup namespaces to compute cli subcommands

Cherry-pick an upstream change removing introspection to figure out the
supported subcommmands. This is necessary in our case, as introspection
seems to be broken with Clojure 1.9 and/or JRE 11.
parent e5f19e7c
From 5573db0ae5faec3741451f9ed8811f9a03c7973a Mon Sep 17 00:00:00 2001
From: Rob Browning <rlb@puppet.com>
Date: Fri, 10 Aug 2018 11:43:06 -0500
Subject: [PATCH] (PDB-3993) Don't lookup namespaces to compute cli subcommand
list
Simplify subcommand dispatching to make future changes easier, given
that we haven't needed the dynamic lookup yet.
---
src/puppetlabs/puppetdb/cli/benchmark.clj | 2 -
src/puppetlabs/puppetdb/cli/services.clj | 1 -
src/puppetlabs/puppetdb/cli/version.clj | 2 -
src/puppetlabs/puppetdb/core.clj | 113 +++++++---------------
4 files changed, 36 insertions(+), 82 deletions(-)
diff --git a/src/puppetlabs/puppetdb/cli/benchmark.clj b/src/puppetlabs/puppetdb/cli/benchmark.clj
index af96ebce..eb15ba17 100644
--- a/src/puppetlabs/puppetdb/cli/benchmark.clj
+++ b/src/puppetlabs/puppetdb/cli/benchmark.clj
@@ -65,8 +65,6 @@
[java.nio.file Files OpenOption]
[java.util ArrayDeque]))
-(def cli-description "Development-only benchmarking tool")
-
(defn try-load-file
"Attempt to read and parse the JSON in `file`. If this failed, an error is
logged, and nil is returned."
diff --git a/src/puppetlabs/puppetdb/cli/services.clj b/src/puppetlabs/puppetdb/cli/services.clj
index 433e65ff..ec38d282 100644
--- a/src/puppetlabs/puppetdb/cli/services.clj
+++ b/src/puppetlabs/puppetdb/cli/services.clj
@@ -82,7 +82,6 @@
[java.util.concurrent.locks ReentrantLock]
[org.joda.time Period]))
-(def cli-description "Main PuppetDB daemon")
(def database-metrics-registry (get-in metrics/metrics-registries [:database :registry]))
;; ## Wiring
diff --git a/src/puppetlabs/puppetdb/cli/version.clj b/src/puppetlabs/puppetdb/cli/version.clj
index 7ef5e031..81056bd4 100644
--- a/src/puppetlabs/puppetdb/cli/version.clj
+++ b/src/puppetlabs/puppetdb/cli/version.clj
@@ -12,8 +12,6 @@
(:require [puppetlabs.puppetdb.meta.version :refer [version]]
[puppetlabs.puppetdb.scf.migrate :refer [desired-schema-version]]))
-(def cli-description "Print info about the current version of PuppetDB")
-
;; TODO: Would like to add database info and some other things here, but that
;; will require us to have access to the configuration info. At present, the
;; configuration parsing code is scattered throughout services.clj and not
diff --git a/src/puppetlabs/puppetdb/core.clj b/src/puppetlabs/puppetdb/core.clj
index d4e9b497..1b81f064 100644
--- a/src/puppetlabs/puppetdb/core.clj
+++ b/src/puppetlabs/puppetdb/core.clj
@@ -1,90 +1,49 @@
(ns puppetlabs.puppetdb.core
- "CLI invokation
-
- This is a tiny shim that delegates the command-line arguments to an
- appropriate handler.
-
- If the user executes the program with arguments like so:
-
- ./this-program foo arg1 arg2 arg3
-
- ...then we'll look for a namespace called
- `puppetlabs.puppetdb.cli.foo` and invoke its `-main` method with
- `[arg1 arg2 arg3]`."
- (:require [puppetlabs.trapperkeeper.logging :as logging-utils]
- [clojure.tools.namespace :as ns]
- [puppetlabs.puppetdb.utils :as utils]
- [clojure.string :as str]))
-
-(def ns-prefix "puppetlabs.puppetdb.cli.")
-
-(defn cli-namespaces
- "Return a set of namespaces underneath the .cli parent"
- []
- {:post [(set? %)]}
- (set (for [namespace (ns/find-namespaces-on-classpath)
- :let [ns-str (name namespace)]
- :when (.startsWith ns-str ns-prefix)]
- namespace)))
-
-(defn var-from-ns
- "Resolve a var, by name, from a (potentially un-required)
- namespace. If no matching var is found, returns nil.
-
- Example:
-
- (var-from-ns \"clojure.string\" \"split\")"
- [ns v]
- {:pre [(string? ns)
- (string? v)]}
- (require (symbol ns))
- (if-let [var (resolve (symbol ns v))]
- (var-get var)))
-
-(defn available-subcommands
- "Return the set of available subcommands for this application"
- []
- {:post [(set? %)]}
- (set (for [namespace (cli-namespaces)
- :let [ns-str (name namespace)
- subcmd (last (str/split ns-str #"\."))]]
- subcmd)))
+ "Handles command line arguments."
+ (:require
+ [clojure.string :as str]
+ [puppetlabs.puppetdb.cli.benchmark :as benchmark]
+ [puppetlabs.puppetdb.cli.fact-storage-benchmark :as fstore-bench]
+ [puppetlabs.puppetdb.cli.services :as svcs]
+ [puppetlabs.puppetdb.cli.version :as cver]
+ [puppetlabs.puppetdb.utils :as utils]
+ [puppetlabs.trapperkeeper.logging :as logging-utils]))
+
+(def usage-lines
+ ["Available subcommands:"
+ " version Display version information"
+ " services Run PuppetDB"
+ " benchmark Run development-only benchmarking tool"
+ " fact-storage-benchmark"
+ "For help on a given subcommand, invoke it with -h"])
(defn usage
- "Display help text to the user"
- []
- (let [cmds (sort (for [cmd (available-subcommands)]
- [cmd (var-from-ns (str ns-prefix cmd) "cli-description")]))]
- (println "Available subcommands:\n")
- (doseq [[subcommand description] cmds]
- (println subcommand "\t" (or description "")))
- (println "\nFor help on a given subcommand, invoke it with -h")))
+ [stream]
+ (binding [*out* stream]
+ (println (str/join "\n" usage-lines))))
(defn run-command
"Does the real work of invoking a command by attempting to result it and
passing in args. `success-fn` is a no-arg function that is called when the
command successfully executes. `fail-fn` is called when a bad command is given
or a failure executing a command."
- [success-fn fail-fn args]
- (let [subcommand (first args)
- allowed? (available-subcommands)]
-
+ [success-fn fail-fn [subcommand & args]]
+ (let [run (case subcommand
+ "version" #(apply cver/-main args)
+ "services" #(apply svcs/-main args)
+ "benchmark" #(apply benchmark/-main args)
+ "fact-storage-benchmark" #(apply fstore-bench/-main args)
+ (do
+ ;; FIXME: this should be *err*
+ (usage *out*)
+ (fail-fn)))]
(utils/fail-unsupported-jdk fail-fn)
-
- ;; Bad invokation
- (when-not (allowed? subcommand)
- (usage)
- (fail-fn))
-
- (let [module (str ns-prefix subcommand)
- args (rest args)]
- (try
- (require (symbol module))
- (apply (resolve (symbol module "-main")) args)
- (success-fn)
- (catch Throwable e
- (logging-utils/catch-all-logger e)
- (fail-fn))))))
+ (try
+ (run)
+ (success-fn)
+ (catch Throwable e
+ (logging-utils/catch-all-logger e)
+ (fail-fn)))))
(defn -main
[& args]
--
2.20.1
......@@ -2,3 +2,4 @@
compojure-1.6-compat.patch
disable-update-checks.patch
honeysql-0.7-compat.patch
0001-PDB-3993-Don-t-lookup-namespaces-to-compute-cli-subc.patch
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment