Skip to content

Commit

Permalink
Add colored cli output
Browse files Browse the repository at this point in the history
  • Loading branch information
julienvincent committed Nov 11, 2023
1 parent 9ad3c53 commit cd236ff
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 18 deletions.
1 change: 1 addition & 0 deletions deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
metosin/jsonista {:mvn/version "0.3.7"}
clj-commons/clj-yaml {:mvn/version "1.0.27"}
babashka/process {:mvn/version "0.5.21"}
jansi-clj/jansi-clj {:mvn/version "1.0.1"}
cli-matic/cli-matic {:mvn/version "0.5.4"}
org.clojars.civa86/pretty.cli {:mvn/version "1.0.1"}
funcool/promesa {:mvn/version "11.0.678"}
Expand Down
8 changes: 4 additions & 4 deletions src/k16/kl/api/module/downloader.clj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
:headers {"Accept" "application/vnd.github.raw"}})]

(when (not= 200 (:status res))
(log/info (str "Failed to pull " identifier "@" sha "/" path))
(log/error (str "Failed to pull " identifier "@" sha "/" path))
(cli.util/exit! (:body res) 1))

(slurp (:body res))))
Expand All @@ -31,7 +31,7 @@
:headers {"Accept" "application/vnd.github.raw"}})]

(when (not= 200 (:status res))
(log/info (str "Failed to list files in " identifier "@" sha "/" path))
(log/error (str "Failed to list files in " identifier "@" sha "/" path))
(cli.util/exit! (:body res) 1))

(json/read-value (:body res) json/keyword-keys-object-mapper)))
Expand Down Expand Up @@ -71,14 +71,14 @@
:SHA_SHORT sha-short
:DIR submodule-dir}]

(log/info (str "Downloading " url "@" sha-short))
(log/debug (str "Pulling module " url "@@|cyan " sha-short "|@"))

(let [module (download-module-config module-ref vars)]
@(p/all
(->> (:include module)
(map (fn [file]
(p/vthread
(log/info (str "Downloading " file " [" submodule-name "]"))
(log/debug (str "Downloading " file " from " submodule-name))
(let [contents (->> (read-repo-file url sha (relative-to subdir file))
(replace-vars vars))]
(spit (api.fs/from-submodule-dir module-name submodule-name file) contents)))))))
Expand Down
9 changes: 5 additions & 4 deletions src/k16/kl/api/resolver.clj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

(defn- resolve-module-ref [{:keys [url sha ref subdir]}]
(when-not sha
(log/info (str "Resolving " url (if subdir (str "/" subdir) ""))))
(log/debug (str "Resolving " url (if subdir (str "/" subdir) ""))))

(let [sha (if sha sha (get-commit-for-ref url ref))]
(cond-> {:url url :sha sha :ref ref}
Expand Down Expand Up @@ -152,9 +152,10 @@

(println)
(doseq [[submodule-name {:keys [ref previous-ref]}] updated-modules]
(log/info (str "Module " (name submodule-name)
" updated from "
(subs (:sha previous-ref) 0 7) " -> " (subs (:sha ref) 0 7))))
(let [module-name (str "@|yellow " (name submodule-name) "|@@|white @|@")
from (subs (:sha previous-ref) 0 7)
to (subs (:sha ref) 0 7)]
(log/info (str module-name "@|bold,red " from "|@ => " module-name "@|bold,green " to "|@"))))
(println))

{:modules modules
Expand Down
6 changes: 3 additions & 3 deletions src/k16/kl/commands/modules.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

(defn- pull! [{:keys [update] :as props}]
(let [module-name (prompt.config/get-module-name props)
updated? (api.resolver/pull! module-name {:update-lockfile? update})]
(if updated?
(log/info "Services updated")
{:keys [lockfile-updated?]} (api.resolver/pull! module-name {:update-lockfile? update})]
(if lockfile-updated?
(log/info "@|bold Services updated|@")
(log/info "Services are all up to date"))))

(defn- set-default-module! [props]
Expand Down
7 changes: 3 additions & 4 deletions src/k16/kl/commands/resolver.clj
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
(ns k16.kl.commands.resolver
(:require
[clojure.string :as str]
[k16.kl.log :as log]))
[clojure.string :as str]))

(defn- setup-macos-resolver []
(log/info "sudo mkdir -p /etc/resolver\necho 'nameserver 127.0.0.1' | sudo tee -a /etc/resolver/test > /dev/null"))
(println "sudo mkdir -p /etc/resolver\necho 'nameserver 127.0.0.1' | sudo tee -a /etc/resolver/test > /dev/null"))

(defn- setup-linux-resolver []
(log/info "(echo 'nameserver 127.0.0.1'; sudo cat /etc/resolv.conf) | sudo tee -a /etc/resolv.conf > /dev/null"))
(println "(echo 'nameserver 127.0.0.1'; sudo cat /etc/resolv.conf) | sudo tee -a /etc/resolv.conf > /dev/null"))

(defn- setup-resolver! [_]
(let [os (-> (System/getProperty "os.name") .toLowerCase)]
Expand Down
16 changes: 13 additions & 3 deletions src/k16/kl/log.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
(ns k16.kl.log)
(ns k16.kl.log
(:require
[jansi-clj.core :as color]))

(def ^:private lock (Object.))

(defn info [& args]
(defn info [msg]
(locking lock
(apply println args)))
(println (color/render msg))))

(defn error [msg]
(locking lock
(println (color/render (str "@|red " (color/render msg) "|@")))))

(defn debug [msg]
(locking lock
(println (color/render (str "@|white " (color/render msg) "|@")))))

0 comments on commit cd236ff

Please sign in to comment.