Skip to content

Commit 5c6e87a

Browse files
authored
[patch] ✨ Add edn opt (#1)
1 parent 2f1d4c1 commit 5c6e87a

File tree

9 files changed

+115
-33
lines changed

9 files changed

+115
-33
lines changed

project.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
[metosin/jsonista "0.2.2"]
1010
[camel-snake-kebab "0.4.0"]
1111
[io.quarkus/quarkus-jgit "1.1.0.CR1"]
12-
[org.martinklepsch/clj-http-lite "0.4.1"]]
12+
[org.martinklepsch/clj-http-lite "0.4.3"]]
1313
:profiles {:dev {:dependencies [[org.clojure/tools.namespace "0.2.11"]
1414
[orchestra "2019.02.06-1"]]
1515
:source-paths ["dev"]}

src/gitwerk/command/clone.clj

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,15 @@
33
[clojure.spec.alpha :as spec]
44
[gitwerk.external.git :as git]))
55

6+
(defn clone
7+
[repo]
8+
(let [res (git/clone repo)]
9+
(if res
10+
{:status 0}
11+
{:status 1})))
12+
613
(defn run [ctx args]
714
(case (count args)
8-
1 (git/clone (first args))
9-
(throw
10-
(Exception. "'clone' takes one argument"))))
15+
1 (clone (first args))
16+
{:status 1
17+
:console-out "'clone' takes one argument"}))

src/gitwerk/command/log.clj

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
[gitwerk.external.git :as git]))
55

66
(defn run [ctx args]
7-
(-> (git/repo ".")
8-
(git/logs)
9-
(println)))
7+
(let [res (-> (git/repo ".")
8+
(git/logs))]
9+
(if res
10+
{:status 0
11+
:console-out res}
12+
{:status 1
13+
:console-out "could not fetch logs"})))

src/gitwerk/command/semver.clj

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
[gitwerk.external.git :as git]
66
[gitwerk.service.semver :as semver]))
77

8-
(defn run [ctx args]
8+
(defn semver [ctx args]
99
(let [semver-type (-> (first args)
1010
(string/lower-case)
1111
(keyword))
@@ -18,8 +18,15 @@
1818
(semver/latest-tag)
1919
(semver/str->version)
2020
(semver-func)
21-
(semver/version->str)
22-
(println))))
21+
(semver/version->str))))
22+
23+
(defn run [ctx args]
24+
(let [res (semver ctx args)]
25+
(if res
26+
{:status 0
27+
:console-out res}
28+
{:status 1
29+
:console-out "nothing updated"})))
2330

2431
(comment
2532
(run {} ["patch"])

src/gitwerk/command/semver_auto.clj

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
[gitwerk.service.semver :as semver]
77
[gitwerk.service.semver-auto :as semver-auto]))
88

9-
(defn run [ctx args]
10-
(let [repo (git/repo ".")
9+
(defn semver-auto
10+
[repodir]
11+
(let [repo (git/repo repodir)
1112
message (-> repo
1213
(git/latest-log)
1314
:full-message)
@@ -17,7 +18,21 @@
1718
(semver/default-version-str))
1819
new-tag (semver-auto/semver-auto message tag)]
1920
(when (not (= tag new-tag))
20-
(git/tag repo new-tag))))
21+
(git/tag repo new-tag)
22+
{:old tag
23+
:new new-tag})))
24+
25+
(defn run [ctx _]
26+
(let [res (semver-auto ".")]
27+
(if res
28+
{:status 0
29+
:console-out
30+
{:status :updated
31+
:old-version (:old res)
32+
:new-version (:new res)}}
33+
{:status 0
34+
:console-out
35+
{:status :not-updated}})))
2136

2237
(comment
2338
(run {} []))

src/gitwerk/command/tag.clj

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
[gitwerk.external.git :as git]))
55

66
(defn run [ctx args]
7-
(-> (git/repo ".")
8-
(git/tags)
9-
(println)))
7+
(let [res (-> (git/repo ".")
8+
(git/tags))]
9+
(if res
10+
{:status 0
11+
:console-out res}
12+
{:status 1
13+
:console-out "could not fetch tags"})))

src/gitwerk/core.clj

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,41 @@
44
[clojure.tools.cli :as cli]
55
[clojure.edn :as edn]
66
[clojure.java.io :as io]
7-
[camel-snake-kebab.core :as csk]
8-
[gitwerk.command.clone :as command.clone]
9-
[gitwerk.command.log :as command.log]
10-
[gitwerk.command.semver :as command.semver]
11-
[gitwerk.command.semver-auto :as command.semver-auto]
12-
[gitwerk.command.tag :as command.tag])
7+
[gitwerk.service.runner :as runner])
138
(:gen-class))
149

1510
(def cli-header "Usage: gitwerk [command] [options]")
1611
(def cli-options
1712
[["-f" "--file PATH" "config"
1813
:id :config-filename
1914
:default "config.edn"]
15+
["-e" "--edn" :id :edn?]
2016
["-d" "--debug" :id :debug?]
2117
["-h" "--help" :id :help?]])
2218

23-
(defn run
24-
[{:keys [command args options summary] :as ctx}]
25-
(case (csk/->kebab-case-keyword command)
26-
:clone (command.clone/run ctx args)
27-
:log (command.log/run ctx args)
28-
:semver (command.semver/run ctx args)
29-
:semver-auto (command.semver-auto/run ctx args)
30-
:tag (command.tag/run ctx args)
31-
(do
19+
(defn edn-output
20+
[ctx res]
21+
(println res))
22+
23+
(defn std-output
24+
[{:keys [summary] :as ctx}
25+
{:keys [status invalid-arg? console-out]}]
26+
(when console-out
27+
(println console-out))
28+
(when invalid-arg?
3229
(println cli-header)
33-
(println summary))))
30+
(println summary))
31+
(if status
32+
(System/exit status)
33+
(System/exit 1)))
34+
35+
(defn run
36+
[{:keys [options] :as ctx}]
37+
(let [{:keys [edn?]} options
38+
res (runner/run ctx)]
39+
(if edn?
40+
(edn-output ctx res)
41+
(std-output ctx res))))
3442

3543
(defn main
3644
[{:keys [options summary arguments] :as parsed-result}]
@@ -51,6 +59,7 @@
5159
(cli/parse-opts cli-options)
5260
(main))
5361
(catch Exception e
54-
(println (.getMessage e)))
62+
(println (.getMessage e))
63+
(System/exit 1))
5564
(finally
5665
(shutdown-agents))))

src/gitwerk/external/github.clj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,10 @@
22
(:require
33
[clojure.spec.alpha :as spec]
44
[clj-http.lite.client :as http]))
5+
6+
(defn call
7+
([url]
8+
(call url nil))
9+
([url body]
10+
(http/get url
11+
{:body body})))

src/gitwerk/service/runner.clj

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
(ns gitwerk.service.runner
2+
(:require
3+
[clojure.spec.alpha :as spec]
4+
[camel-snake-kebab.core :as csk]
5+
[gitwerk.command.clone :as command.clone]
6+
[gitwerk.command.log :as command.log]
7+
[gitwerk.command.semver :as command.semver]
8+
[gitwerk.command.semver-auto :as command.semver-auto]
9+
[gitwerk.command.tag :as command.tag]))
10+
11+
(defn dispatch
12+
[{:keys [args] :as ctx} cmd]
13+
(let [default (fn [_ _]
14+
{:status 1
15+
:invalid-arg? true})
16+
cmd (case cmd
17+
:clone command.clone/run
18+
:log command.log/run
19+
:semver command.semver/run
20+
:semver-auto command.semver-auto/run
21+
:tag command.tag/run
22+
default)]
23+
(cmd ctx args)))
24+
25+
(defn run
26+
[{:keys [command] :as ctx}]
27+
(->> command
28+
(csk/->kebab-case-keyword)
29+
(dispatch ctx)))

0 commit comments

Comments
 (0)