Skip to content

Instantly share code, notes, and snippets.

@ghadishayban
Last active December 21, 2016 03:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ghadishayban/050d0059e60884b95ffbb6f11901d0bb to your computer and use it in GitHub Desktop.
Save ghadishayban/050d0059e60884b95ffbb6f11901d0bb to your computer and use it in GitHub Desktop.
unfold example
(ns docker-tag-paginate
(:require [clj-http.lite.client :as client]
[cheshire.core :as json]
[clojure.pprint :refer [print-table]]))
(def base-url "https://registry.hub.docker.com/v2")
;; (progression pos? dec 15)
(defn progression
[continue? f seed]
(reify
clojure.lang.Sequential
clojure.lang.Seqable
(seq [_]
(let [step (fn step-fn [seed]
(cons seed (lazy-seq
(when (continue? seed)
(step-fn (f seed))))))]
(step seed)))
clojure.lang.IReduceInit
(reduce [_ rf init]
(loop [ret (rf init seed)
seed seed]
(if (reduced? ret)
@ret
(if (continue? seed)
(let [next (f seed)]
(recur (rf ret next) next))
ret))))))
(defn read-response
[response]
(when (= 200 (:status response))
(update response :body json/parse-string keyword)))
(defn get-tags
[image-name]
(let [base-url (str base-url "/repositories/library/" image-name "/tags/")
fetch! (comp read-response client/get)
next-url (comp :next :body)
results (comp :results :body)
pages (progression next-url (comp fetch! next-url) (fetch! base-url))]
(into [] (mapcat results) pages)))
(defn pretty-print-tags
[tags]
(->> tags
(sort-by :last_updated (fn [a b] (compare b a)))
(print-table [:name :full_size :last_updated])))
(defn -main
[image-name]
(-> image-name
get-tags
pretty-print-tags))
@arichiardi
Copy link

arichiardi commented Dec 21, 2016

I have used this, and while I was writing the docstring I thought of coalesce. Maybe unrelated, I thought I might throw it there 😄 Thanks for sharing!

EDIT: for others, here is a more up-to-date, arguably better, approach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment