Move cast funcktion to yaml namespace

This commit is contained in:
Jan Krebs 2021-05-27 15:02:05 +02:00
parent cf5c337b14
commit e4b0e26fd5
2 changed files with 10 additions and 42 deletions

View file

@ -1,13 +1,20 @@
(ns dda.k8s-keycloak.yaml (ns dda.k8s-keycloak.yaml
(:require (:require
[clojure.java.io :as io] [clojure.java.io :as io]
[clj-yaml.core :as yaml])) [clj-yaml.core :as yaml]
[clojure.walk]))
(defn cast-lazy-seq-to-vec
[lazy-seq]
(clojure.walk/postwalk #(if (instance? clojure.lang.LazySeq %)
(into [] %)
%) lazy-seq))
(defn load-resource [resource-name] (defn load-resource [resource-name]
(slurp (io/resource resource-name))) (slurp (io/resource resource-name)))
(defn from-string [input] (defn from-string [input]
(yaml/parse-string input)) (cast-lazy-seq-to-vec (yaml/parse-string input)))
(defn to-string [edn] (defn to-string [edn]
(yaml/generate-string edn :dumper-options {:flow-style :block})) (yaml/generate-string edn :dumper-options {:flow-style :block}))

View file

@ -27,14 +27,6 @@
(def auth? (s/keys :req-un [::user-name ::user-password])) (def auth? (s/keys :req-un [::user-name ::user-password]))
; This needs to be checked:
; Are LazySeq only lists on our case?
(defn cast-lazy-seq-to-vec
[lazy-seq]
(clojure.walk/postwalk #(if (instance? clojure.lang.LazySeq %)
(do (into [] %))
%) lazy-seq))
(defn replace-all-matching-values-by-new-value (defn replace-all-matching-values-by-new-value
[coll value-to-match value-to-replace] [coll value-to-match value-to-replace]
(clojure.walk/postwalk #(if (and (= (type value-to-match) (type %)) (clojure.walk/postwalk #(if (and (= (type value-to-match) (type %))
@ -42,32 +34,6 @@
value-to-replace value-to-replace
%) coll)) %) coll))
(declare assoc-in-nested)
(declare assoc-in-nested-seq)
(declare assoc-in-nested-map)
(defn assoc-in-nested-seq [s path n]
(map #(if (sequential? %)
(assoc-in-nested-seq % path n)
(assoc-in-nested-map % path n)) s))
(defn assoc-in-nested-map [m path n]
(into (empty m)
(let [p1 (first path)]
(for [[k v] m]
(if (= k p1)
[k (assoc-in-nested v (rest path) n)]
[k (assoc-in-nested v path n)])))))
(defn assoc-in-nested [data path n]
(if (empty? path)
n
(if (sequential? data)
(assoc-in-nested-seq data path n)
(if (map? data)
(assoc-in-nested-map data path n)
data))))
(defn generate-config [my-config my-auth] (defn generate-config [my-config my-auth]
(-> (->
(yaml/from-string (yaml/load-resource "config.yaml")) (yaml/from-string (yaml/load-resource "config.yaml"))
@ -78,7 +44,6 @@
(let [{:keys [user-name user-password]} my-auth] (let [{:keys [user-name user-password]} my-auth]
(-> (->
(yaml/from-string (yaml/load-resource "deployment.yaml")) (yaml/from-string (yaml/load-resource "deployment.yaml"))
(cast-lazy-seq-to-vec)
(assoc-in [:spec :template :spec :containers 0 :env 0 :value] user-name) (assoc-in [:spec :template :spec :containers 0 :env 0 :value] user-name)
(assoc-in [:spec :template :spec :containers 0 :env 1 :value] user-password)))) (assoc-in [:spec :template :spec :containers 0 :env 1 :value] user-password))))
@ -101,8 +66,6 @@
(assoc-in [:metadata :annotations :cert-manager.io/cluster-issuer] letsencrypt-issuer) (assoc-in [:metadata :annotations :cert-manager.io/cluster-issuer] letsencrypt-issuer)
(replace-all-matching-values-by-new-value "fqdn" fqdn)))) (replace-all-matching-values-by-new-value "fqdn" fqdn))))
(defn generate-service [] (defn generate-service []
(yaml/from-string (yaml/load-resource "service.yaml"))) (yaml/from-string (yaml/load-resource "service.yaml")))
@ -119,5 +82,3 @@
(yaml/to-string (generate-service)) (yaml/to-string (generate-service))
"---" "---"
(yaml/to-string (generate-deployment my-auth))])) (yaml/to-string (generate-deployment my-auth))]))