Skip to content

Instantly share code, notes, and snippets.

@WhittlesJr
Created April 1, 2019 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WhittlesJr/96fc4bf13804b07854e62f23eb5cc487 to your computer and use it in GitHub Desktop.
Save WhittlesJr/96fc4bf13804b07854e62f23eb5cc487 to your computer and use it in GitHub Desktop.
;; My IP address data is shaped like this:
(def data {"host-a" {:ip "1.1.1.1"}
"host-b" {:ip "2.2.2.2"}
"container" {"host-c" {:ip "3.3.3.3"}
"host-d" {:ip "4.4.4.4"}}})
;; This logic turns that data into a map ("set" in nix) that would work for `networking.hosts`:
(defn build-hosts [ip-data & [name-prefix]]
(reduce-kv (fn [result hostname {:keys [ip] :as info}]
(let [full-hostname (if name-prefix
(str name-prefix "-" hostname)
hostname)]
(if ip
(assoc result ip [full-hostname])
(merge result (build-hosts info hostname)))))
{}
ip-data))
(build-hosts data)
=> {"1.1.1.1" ["host-a"],
"2.2.2.2" ["host-b"],
"3.3.3.3" ["container-host-c"],
"4.4.4.4" ["container-host-d"]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment