|
| 1 | +(ns app.mappings |
| 2 | + (:require [backend-shared.service.index :refer [perform fetch]] |
| 3 | + [shared.protocols.actionable :as ac] |
| 4 | + [cljs.core.async :as async] |
| 5 | + [shared.protocols.queryable :as qa] |
| 6 | + [cljs.nodejs :as node] |
| 7 | + [shared.protocols.loggable :as log] |
| 8 | + [shared.protocols.convertible :as cv]) |
| 9 | + (:require-macros [cljs.core.async.macros :refer [go]])) |
| 10 | + |
| 11 | +(def fstream (node/require "fstream")) |
| 12 | +(def unzip (node/require "unzipper")) |
| 13 | +(def fs (node/require "fs")) |
| 14 | + |
| 15 | +(defmethod fetch :artifacts [{:keys [bucket]} {:keys [input-queries credentials] :as query}] |
| 16 | + (go |
| 17 | + (let [{:keys [found errors]} (async/<! (qa/fetch bucket credentials input-queries))] |
| 18 | + {:found (when-not (empty? found) (first found)) |
| 19 | + :error (when-not (empty? errors) errors)}))) |
| 20 | + |
| 21 | +(defmethod perform [:put :pipeline-job] [{:keys [code-pipeline]} action] |
| 22 | + (ac/perform code-pipeline action)) |
| 23 | + |
| 24 | +(defmethod perform [:decode :errors] [{:keys [code-pipeline]} [_ payload]] |
| 25 | + {:error payload}) |
| 26 | + |
| 27 | +(defmethod perform [:decode :zipfile] [{:keys [code-pipeline]} [_ payload :as action]] |
| 28 | + (let [c (async/chan) |
| 29 | + output-path {:path "/tmp/extracted/"} |
| 30 | + read-stream (.Reader fstream (:filename payload)) |
| 31 | + write-stream (.Extract unzip (clj->js output-path))] |
| 32 | + (.on write-stream "close" #(async/put! c output-path)) |
| 33 | + (.pipe read-stream write-stream) |
| 34 | + c)) |
| 35 | + |
| 36 | +(defn read-file [path] |
| 37 | + (let [c (async/chan)] |
| 38 | + (.readFile fs "extracted/buildspec.yml" #(async/put! c %2)) |
| 39 | + c)) |
| 40 | + |
| 41 | +(defmethod perform [:put :file-path] [{:keys [bucket]} [_ payload :as action]] |
| 42 | + (go |
| 43 | + (let [key "buildspec.yml" |
| 44 | + payload (async/<! (read-file (str "/tmp/extracted/" key))) |
| 45 | + items [{:file-name key |
| 46 | + :content payload}]] |
| 47 | + (async/<! (ac/perform bucket [:put items]))))) |
0 commit comments