Skip to content

Commit

Permalink
[patch] ✨ Add edn opt (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
rinx authored Dec 21, 2019
1 parent 2f1d4c1 commit 5c6e87a
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 33 deletions.
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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"]}
Expand Down
13 changes: 10 additions & 3 deletions src/gitwerk/command/clone.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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"}))
10 changes: 7 additions & 3 deletions src/gitwerk/command/log.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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"})))
13 changes: 10 additions & 3 deletions src/gitwerk/command/semver.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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"])
Expand Down
21 changes: 18 additions & 3 deletions src/gitwerk/command/semver_auto.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {} []))
10 changes: 7 additions & 3 deletions src/gitwerk/command/tag.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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"})))
43 changes: 26 additions & 17 deletions src/gitwerk/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,41 @@
[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]")
(def cli-options
[["-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}]
Expand All @@ -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))))
7 changes: 7 additions & 0 deletions src/gitwerk/external/github.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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})))
29 changes: 29 additions & 0 deletions src/gitwerk/service/runner.clj
Original file line number Diff line number Diff line change
@@ -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)))

0 comments on commit 5c6e87a

Please sign in to comment.