From 7d7818479353dc1f6a8eec5d71c3a83cdb149fc9 Mon Sep 17 00:00:00 2001 From: Rintaro Okamura Date: Sat, 21 Dec 2019 21:36:59 +0900 Subject: [PATCH] :sparkles: Add edn opt --- project.clj | 2 +- src/gitwerk/command/clone.clj | 13 +++++++-- src/gitwerk/command/log.clj | 10 +++++-- src/gitwerk/command/semver.clj | 13 +++++++-- src/gitwerk/command/semver_auto.clj | 21 ++++++++++++-- src/gitwerk/command/tag.clj | 10 +++++-- src/gitwerk/core.clj | 43 +++++++++++++++++------------ src/gitwerk/external/github.clj | 7 +++++ src/gitwerk/service/runner.clj | 29 +++++++++++++++++++ 9 files changed, 115 insertions(+), 33 deletions(-) create mode 100644 src/gitwerk/service/runner.clj diff --git a/project.clj b/project.clj index 20bf416..c9a2891 100644 --- a/project.clj +++ b/project.clj @@ -9,7 +9,7 @@ [metosin/jsonista "0.2.2"] [camel-snake-kebab "0.4.0"] [io.quarkus/quarkus-jgit "1.1.0.CR1"] - [org.martinklepsch/clj-http-lite "0.4.1"]] + [org.martinklepsch/clj-http-lite "0.4.3"]] :profiles {:dev {:dependencies [[org.clojure/tools.namespace "0.2.11"] [orchestra "2019.02.06-1"]] :source-paths ["dev"]} diff --git a/src/gitwerk/command/clone.clj b/src/gitwerk/command/clone.clj index 1de4a64..4c53b50 100644 --- a/src/gitwerk/command/clone.clj +++ b/src/gitwerk/command/clone.clj @@ -3,8 +3,15 @@ [clojure.spec.alpha :as spec] [gitwerk.external.git :as git])) +(defn clone + [repo] + (let [res (git/clone repo)] + (if res + {:status 0} + {:status 1}))) + (defn run [ctx args] (case (count args) - 1 (git/clone (first args)) - (throw - (Exception. "'clone' takes one argument")))) + 1 (clone (first args)) + {:status 1 + :console-out "'clone' takes one argument"})) diff --git a/src/gitwerk/command/log.clj b/src/gitwerk/command/log.clj index cb6d6a7..b79fcca 100644 --- a/src/gitwerk/command/log.clj +++ b/src/gitwerk/command/log.clj @@ -4,6 +4,10 @@ [gitwerk.external.git :as git])) (defn run [ctx args] - (-> (git/repo ".") - (git/logs) - (println))) + (let [res (-> (git/repo ".") + (git/logs))] + (if res + {:status 0 + :console-out res} + {:status 1 + :console-out "could not fetch logs"}))) diff --git a/src/gitwerk/command/semver.clj b/src/gitwerk/command/semver.clj index 9acf295..ec28af0 100644 --- a/src/gitwerk/command/semver.clj +++ b/src/gitwerk/command/semver.clj @@ -5,7 +5,7 @@ [gitwerk.external.git :as git] [gitwerk.service.semver :as semver])) -(defn run [ctx args] +(defn semver [ctx args] (let [semver-type (-> (first args) (string/lower-case) (keyword)) @@ -18,8 +18,15 @@ (semver/latest-tag) (semver/str->version) (semver-func) - (semver/version->str) - (println)))) + (semver/version->str)))) + +(defn run [ctx args] + (let [res (semver ctx args)] + (if res + {:status 0 + :console-out res} + {:status 1 + :console-out "nothing updated"}))) (comment (run {} ["patch"]) diff --git a/src/gitwerk/command/semver_auto.clj b/src/gitwerk/command/semver_auto.clj index 0dfb873..0c511ae 100644 --- a/src/gitwerk/command/semver_auto.clj +++ b/src/gitwerk/command/semver_auto.clj @@ -6,8 +6,9 @@ [gitwerk.service.semver :as semver] [gitwerk.service.semver-auto :as semver-auto])) -(defn run [ctx args] - (let [repo (git/repo ".") +(defn semver-auto + [repodir] + (let [repo (git/repo repodir) message (-> repo (git/latest-log) :full-message) @@ -17,7 +18,21 @@ (semver/default-version-str)) new-tag (semver-auto/semver-auto message tag)] (when (not (= tag new-tag)) - (git/tag repo new-tag)))) + (git/tag repo new-tag) + {:old tag + :new new-tag}))) + +(defn run [ctx _] + (let [res (semver-auto ".")] + (if res + {:status 0 + :console-out + {:status :updated + :old-version (:old res) + :new-version (:new res)}} + {:status 0 + :console-out + {:status :not-updated}}))) (comment (run {} [])) diff --git a/src/gitwerk/command/tag.clj b/src/gitwerk/command/tag.clj index 534d731..2585ef0 100644 --- a/src/gitwerk/command/tag.clj +++ b/src/gitwerk/command/tag.clj @@ -4,6 +4,10 @@ [gitwerk.external.git :as git])) (defn run [ctx args] - (-> (git/repo ".") - (git/tags) - (println))) + (let [res (-> (git/repo ".") + (git/tags))] + (if res + {:status 0 + :console-out res} + {:status 1 + :console-out "could not fetch tags"}))) diff --git a/src/gitwerk/core.clj b/src/gitwerk/core.clj index 085f504..c7588ba 100644 --- a/src/gitwerk/core.clj +++ b/src/gitwerk/core.clj @@ -4,12 +4,7 @@ [clojure.tools.cli :as cli] [clojure.edn :as edn] [clojure.java.io :as io] - [camel-snake-kebab.core :as csk] - [gitwerk.command.clone :as command.clone] - [gitwerk.command.log :as command.log] - [gitwerk.command.semver :as command.semver] - [gitwerk.command.semver-auto :as command.semver-auto] - [gitwerk.command.tag :as command.tag]) + [gitwerk.service.runner :as runner]) (:gen-class)) (def cli-header "Usage: gitwerk [command] [options]") @@ -17,20 +12,33 @@ [["-f" "--file PATH" "config" :id :config-filename :default "config.edn"] + ["-e" "--edn" :id :edn?] ["-d" "--debug" :id :debug?] ["-h" "--help" :id :help?]]) -(defn run - [{:keys [command args options summary] :as ctx}] - (case (csk/->kebab-case-keyword command) - :clone (command.clone/run ctx args) - :log (command.log/run ctx args) - :semver (command.semver/run ctx args) - :semver-auto (command.semver-auto/run ctx args) - :tag (command.tag/run ctx args) - (do +(defn edn-output + [ctx res] + (println res)) + +(defn std-output + [{:keys [summary] :as ctx} + {:keys [status invalid-arg? console-out]}] + (when console-out + (println console-out)) + (when invalid-arg? (println cli-header) - (println summary)))) + (println summary)) + (if status + (System/exit status) + (System/exit 1))) + +(defn run + [{:keys [options] :as ctx}] + (let [{:keys [edn?]} options + res (runner/run ctx)] + (if edn? + (edn-output ctx res) + (std-output ctx res)))) (defn main [{:keys [options summary arguments] :as parsed-result}] @@ -51,6 +59,7 @@ (cli/parse-opts cli-options) (main)) (catch Exception e - (println (.getMessage e))) + (println (.getMessage e)) + (System/exit 1)) (finally (shutdown-agents)))) diff --git a/src/gitwerk/external/github.clj b/src/gitwerk/external/github.clj index 4e3bf73..d42f764 100644 --- a/src/gitwerk/external/github.clj +++ b/src/gitwerk/external/github.clj @@ -2,3 +2,10 @@ (:require [clojure.spec.alpha :as spec] [clj-http.lite.client :as http])) + +(defn call + ([url] + (call url nil)) + ([url body] + (http/get url + {:body body}))) diff --git a/src/gitwerk/service/runner.clj b/src/gitwerk/service/runner.clj new file mode 100644 index 0000000..46b2519 --- /dev/null +++ b/src/gitwerk/service/runner.clj @@ -0,0 +1,29 @@ +(ns gitwerk.service.runner + (:require + [clojure.spec.alpha :as spec] + [camel-snake-kebab.core :as csk] + [gitwerk.command.clone :as command.clone] + [gitwerk.command.log :as command.log] + [gitwerk.command.semver :as command.semver] + [gitwerk.command.semver-auto :as command.semver-auto] + [gitwerk.command.tag :as command.tag])) + +(defn dispatch + [{:keys [args] :as ctx} cmd] + (let [default (fn [_ _] + {:status 1 + :invalid-arg? true}) + cmd (case cmd + :clone command.clone/run + :log command.log/run + :semver command.semver/run + :semver-auto command.semver-auto/run + :tag command.tag/run + default)] + (cmd ctx args))) + +(defn run + [{:keys [command] :as ctx}] + (->> command + (csk/->kebab-case-keyword) + (dispatch ctx)))