diff --git a/project-cljs.clj b/project-cljs.clj index ea8f2de..7e16635 100644 --- a/project-cljs.clj +++ b/project-cljs.clj @@ -1,4 +1,4 @@ -(defproject org.domaindrivenarchitecture/c4k-common-cljs "1.1.1-SNAPSHOT" +(defproject org.domaindrivenarchitecture/c4k-common-cljs "1.2.0" :description "Contains predicates and tools for c4k" :url "https://domaindrivenarchitecture.org" :license {:name "Apache License, Version 2.0" diff --git a/project.clj b/project.clj index a7c5266..8a276ab 100644 --- a/project.clj +++ b/project.clj @@ -1,4 +1,4 @@ -(defproject org.domaindrivenarchitecture/c4k-common-clj "1.1.1-SNAPSHOT" +(defproject org.domaindrivenarchitecture/c4k-common-clj "1.1.1" :description "Contains predicates and tools for c4k" :url "https://domaindrivenarchitecture.org" :license {:name "Apache License, Version 2.0" diff --git a/src/main/cljc/dda/c4k_common/common.cljc b/src/main/cljc/dda/c4k_common/common.cljc index bbf1ffd..9b80e41 100644 --- a/src/main/cljc/dda/c4k_common/common.cljc +++ b/src/main/cljc/dda/c4k_common/common.cljc @@ -2,9 +2,12 @@ (:require [clojure.walk] [clojure.spec.alpha :as s] + [clojure.string :as cs] + [clojure.tools.reader.edn :as edn] #?(:clj [orchestra.core :refer [defn-spec]] :cljs [orchestra.core :refer-macros [defn-spec]]) - [dda.c4k-common.predicate :as cp])) + [dda.c4k-common.predicate :as cp] + [expound.alpha :as expound])) ;; deprecated functions were moved to dda.c4k-common.predicate @@ -56,3 +59,54 @@ [& vs (s/* seq?)] (into [] (apply concat vs))) + +(defn usage [name] + (str + "usage: + + " name "{your configuraton file} {your authorization file}")) + +(s/def ::options (s/* #{"-h"})) +(s/def ::filename (s/and string? + #(not (cs/starts-with? % "-")))) +(s/def ::cmd-args (s/cat :options ::options + :args (s/? + (s/cat :config ::filename + :auth ::filename)))) + +(defn invalid-args-msg + [name spec args] + (s/explain spec args) + (println (str "Bad commandline arguments\n" (usage name)))) + +(defn generate-common [my-config my-auth config-defaults k8s-objects] + (let [resulting-config (merge config-defaults my-config my-auth)] + (cs/join + "\n---\n" + (k8s-objects resulting-config)))) + +(defn 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) + (let [{:keys [options args]} parsed-args-cmd + {:keys [config auth]} args] + (cond + (some #(= "-h" %) options) + (println usage) + :default + (let [config-str (slurp config) + auth-str (slurp auth) + config-edn (edn/read-string config-str) + auth-edn (edn/read-string 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 (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})))))))))))