From 4cbaac8d2555abdac8c796bfc6405fb1f87ca1d6 Mon Sep 17 00:00:00 2001 From: bom Date: Fri, 28 Jun 2024 14:18:15 +0200 Subject: [PATCH 1/3] Add version command line arg Requires "project.clj" to be added in resource-path --- project.clj | 3 ++- src/main/clj/dda/c4k_common/uberjar.clj | 5 ++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/project.clj b/project.clj index 5838fc1..2464600 100644 --- a/project.clj +++ b/project.clj @@ -12,7 +12,8 @@ :target-path "target/%s/" :source-paths ["src/main/cljc" "src/main/clj"] - :resource-paths ["src/main/resources"] + :resource-paths ["src/main/resources" + "project.clj"] :repositories [["snapshots" :clojars] ["releases" :clojars]] :deploy-repositories [["snapshots" {:sign-releases false :url "https://clojars.org/repo"}] diff --git a/src/main/clj/dda/c4k_common/uberjar.clj b/src/main/clj/dda/c4k_common/uberjar.clj index 85c9301..ee35b90 100644 --- a/src/main/clj/dda/c4k_common/uberjar.clj +++ b/src/main/clj/dda/c4k_common/uberjar.clj @@ -4,6 +4,7 @@ [clojure.spec.alpha :as s] [clojure.string :as cs] [clojure.tools.reader.edn :as edn] + [clojure.java.io :as io] [dda.c4k-common.yaml :as yaml] [dda.c4k-common.common :as cm] [dda.c4k-common.core :as core] @@ -15,7 +16,7 @@ " name " {your configuraton file} {your authorization file}")) -(s/def ::options (s/* #{"-h"})) +(s/def ::options (s/* #{"-h" "-v" "--version"})) (s/def ::filename (s/and string? #(not (cs/starts-with? % "-")))) (s/def ::cmd-args (s/cat :options ::options @@ -37,6 +38,8 @@ (cond (some #(= "-h" %) options) (println (usage name)) + (some #(or (= "-v" %) (= "--version" %)) options) + (println (some-> (io/resource "project.clj") slurp edn/read-string (nth 2))) :else (let [config-str (slurp config) auth-str (slurp auth) -- 2.45.2 From 4dfdfdcbb5027aee3c48f4c351d90200e11fba38 Mon Sep 17 00:00:00 2001 From: bom Date: Fri, 28 Jun 2024 14:20:55 +0200 Subject: [PATCH 2/3] Improve help output --- src/main/clj/dda/c4k_common/uberjar.clj | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/clj/dda/c4k_common/uberjar.clj b/src/main/clj/dda/c4k_common/uberjar.clj index ee35b90..eb9ad0f 100644 --- a/src/main/clj/dda/c4k_common/uberjar.clj +++ b/src/main/clj/dda/c4k_common/uberjar.clj @@ -13,10 +13,14 @@ (defn usage [name] (str "usage: - + + -v | --version : Shows project version + -h : Shows help + " name " {your configuraton file} {your authorization file}")) -(s/def ::options (s/* #{"-h" "-v" "--version"})) +(s/def ::options (s/* #{"-h" + "-v" "--version"})) (s/def ::filename (s/and string? #(not (cs/starts-with? % "-")))) (s/def ::cmd-args (s/cat :options ::options -- 2.45.2 From d8ed73cb00d160686f5f2583ffa0dabb86c4c68d Mon Sep 17 00:00:00 2001 From: bom Date: Fri, 28 Jun 2024 15:01:26 +0200 Subject: [PATCH 3/3] Add config/auth only cmd line args --- src/main/clj/dda/c4k_common/uberjar.clj | 60 +++++++++++++++++++----- src/main/cljc/dda/c4k_common/common.cljc | 18 +++++++ 2 files changed, 67 insertions(+), 11 deletions(-) diff --git a/src/main/clj/dda/c4k_common/uberjar.clj b/src/main/clj/dda/c4k_common/uberjar.clj index eb9ad0f..89a4c25 100644 --- a/src/main/clj/dda/c4k_common/uberjar.clj +++ b/src/main/clj/dda/c4k_common/uberjar.clj @@ -16,11 +16,17 @@ -v | --version : Shows project version -h : Shows help + + The following options require the use of main-cm instead of main-common + -c | --config : Only generate the config + -a | --auth : Only generate the auth " name " {your configuraton file} {your authorization file}")) (s/def ::options (s/* #{"-h" - "-v" "--version"})) + "-v" "--version" + "-c" "--config" + "-a" "--auth"})) (s/def ::filename (s/and string? #(not (cs/starts-with? % "-")))) (s/def ::cmd-args (s/cat :options ::options @@ -33,7 +39,39 @@ (s/explain spec args) (println (str "Bad commandline arguments\n" (usage name)))) -(defn main-common [name config-spec? auth-spec? config-defaults k8s-objects cmd-args] +(defn main-cm [name config-spec? auth-spec? config-defaults config-objects auth-objects cmd-args] + (let [parsed-args-cmd (s/conform ::cmd-args cmd-args)] + (if (= ::s/invalid parsed-args-cmd) + (invalid-args-msg name ::cmd-args cmd-args) + (let [{:keys [options args]} parsed-args-cmd + {:keys [config auth]} args] + (cond + (some #(= "-h" %) options) + (println (usage name)) + (some #(or (= "-v" %) (= "--version" %)) options) + (println (some-> (io/resource "project.clj") slurp edn/read-string (nth 2))) + :else + (let [config-str (slurp config) + auth-str (slurp auth) + config-parse-fn (if (yaml/is-yaml? config) yaml/from-string edn/read-string) + auth-parse-fn (if (yaml/is-yaml? auth) yaml/from-string edn/read-string) + config-edn (config-parse-fn config-str) + auth-edn (auth-parse-fn auth-str) + config-valid? (s/valid? config-spec? config-edn) + auth-valid? (s/valid? auth-spec? auth-edn) + only-config (some #(or (= "-c" %) (= "--config" %)) options) + only-auth (some #(or (= "-a" %) (= "--auth" %)) options)] + (if (and config-valid? auth-valid?) + (println (cm/generate-cm config-edn auth-edn config-defaults config-objects auth-objects only-config only-auth)) + (do + (when (not config-valid?) + (println + (expound/expound-str config-spec? config-edn {:print-specs? false}))) + (when (not auth-valid?) + (println + (expound/expound-str auth-spec? auth-edn {:print-specs? false}))))))))))) + +(defn ^{:deprecated "6.3.1"} main-common [name config-spec? auth-spec? config-defaults k8s-objects cmd-args] (let [parsed-args-cmd (s/conform ::cmd-args cmd-args)] (if (= ::s/invalid parsed-args-cmd) (invalid-args-msg name ::cmd-args cmd-args) @@ -53,15 +91,15 @@ auth-edn (auth-parse-fn auth-str) config-valid? (s/valid? config-spec? config-edn) auth-valid? (s/valid? auth-spec? auth-edn)] - (if (and config-valid? auth-valid?) - (println (cm/generate-common config-edn auth-edn config-defaults k8s-objects)) - (do - (when (not config-valid?) - (println - (expound/expound-str config-spec? config-edn {:print-specs? false}))) - (when (not auth-valid?) - (println - (expound/expound-str auth-spec? auth-edn {:print-specs? false}))))))))))) + (if (and config-valid? auth-valid?) + (println (cm/generate-common config-edn auth-edn config-defaults k8s-objects)) + (do + (when (not config-valid?) + (println + (expound/expound-str config-spec? config-edn {:print-specs? false}))) + (when (not auth-valid?) + (println + (expound/expound-str auth-spec? auth-edn {:print-specs? false}))))))))))) (defn -main [& cmd-args] (main-common "c4k-common" diff --git a/src/main/cljc/dda/c4k_common/common.cljc b/src/main/cljc/dda/c4k_common/common.cljc index bc976ce..b0374c6 100644 --- a/src/main/cljc/dda/c4k_common/common.cljc +++ b/src/main/cljc/dda/c4k_common/common.cljc @@ -50,6 +50,24 @@ (into [] (apply concat vs))) +(defn generate-cm + [my-config + my-auth + config-defaults + config-objects + auth-objects + only-config + only-auth] + (let [resulting-config (merge config-defaults my-config) + both (not (or only-config only-auth)) + res-vec (cond + both (concat-vec (config-objects resulting-config) (auth-objects my-auth)) + only-config (config-objects my-config) + only-auth (auth-objects my-auth))] + (cs/join + "\n---\n" + res-vec))) + (defn generate-common [my-config my-auth -- 2.45.2