Skip to content

Commit 8d8fcc6

Browse files
authored
Merge pull request #53 from hlship/hls/20250926-tool-name-default
Don't NPE if :tool-name option omitted
2 parents 5e1b766 + 30c9b83 commit 8d8fcc6

4 files changed

Lines changed: 34 additions & 35 deletions

File tree

.github/workflows/clojure.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
- name: Install clojure tools
2727
uses: DeLaGuardo/setup-clojure@13.4
2828
with:
29-
cli: 1.12.2.1565
29+
cli: 1.12.3.1577
3030

3131
- name: Cache clojure dependencies
3232
uses: actions/cache@v4

deps.edn

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{:paths ["src"
22
"resources"]
33

4-
:deps {org.clj-commons/pretty {:mvn/version "3.6.4"}
4+
:deps {org.clj-commons/pretty {:mvn/version "3.6.5"}
55
org.clj-commons/humanize {:mvn/version "1.1"}}
66

77
:net.lewisship.build/scm
@@ -15,7 +15,7 @@
1515
:extra-deps {io.github.cognitect-labs/test-runner {:git/tag "v0.5.1"
1616
:git/sha "dfb30dd"}
1717
io.github.hlship/trace {:mvn/version "1.4"}
18-
io.github.tonsky/clj-reload {:mvn/version "0.9.8"}
18+
io.github.tonsky/clj-reload {:mvn/version "1.0.0"}
1919
nubank/matcher-combinators {:mvn/version "3.9.2"}
2020
babashka/babashka {:mvn/version "1.12.208"}}
2121
:exec-fn cognitect.test-runner.api/test
@@ -34,7 +34,7 @@
3434
{:override-deps {org.clojure/clojure ^:antq/exclude {:mvn/version "1.11.4"}}}
3535

3636
:lint
37-
{:deps {clj-kondo/clj-kondo {:mvn/version "2025.07.28"}}
37+
{:deps {clj-kondo/clj-kondo {:mvn/version "2025.09.22"}}
3838
:main-opts ["-m" "clj-kondo.main" "--lint" "src" "test"]}
3939

4040
:build

src/net/lewisship/cli_tools.clj

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -201,22 +201,26 @@
201201
"Expand dispatch options into tool options, leveraging a cache."
202202
[options]
203203
(let [{:keys [tool-name cache-dir cache-digest]} options
204-
cache-dir' (when cache-dir
205-
(fs/expand-home cache-dir))
206-
digest (when cache-dir'
207-
(or cache-digest
208-
(cache/classpath-digest options)))
209-
cached (when digest
210-
(cache/read-from-cache cache-dir' tool-name digest))
211-
result (if cached
212-
cached
213-
(let [expanded (impl/expand-tool-options options)]
214-
(when cache-dir'
215-
(cache/write-to-cache cache-dir' tool-name digest expanded))
216-
expanded))]
217-
(merge result
218-
{:cache-digest digest}
219-
(select-keys options [:tool-name :doc :arguments :tool-summary :pre-dispatch]))))
204+
tool-name' (or tool-name
205+
(impl/default-tool-name)
206+
(throw (ex-info "No :tool-name specified" {:options options})))
207+
cache-dir' (when cache-dir
208+
(fs/expand-home cache-dir))
209+
digest (when cache-dir'
210+
(or cache-digest
211+
(cache/classpath-digest options)))
212+
cached-command-root (when digest
213+
(cache/read-from-cache cache-dir' tool-name' digest))
214+
command-root (if cached-command-root
215+
cached-command-root
216+
(let [built-command-root (impl/build-command-root tool-name' options)]
217+
(when cache-dir'
218+
(cache/write-to-cache cache-dir' tool-name' digest built-command-root))
219+
built-command-root))]
220+
(merge {:tool-name tool-name'
221+
:cache-digest digest
222+
:command-root command-root}
223+
(select-keys options [:doc :arguments :tool-summary :pre-dispatch]))))
220224

221225
(defn- dispatch*
222226
"Called (indirectly/anonymously) from a tool handler to process remaining command line arguments."

src/net/lewisship/cli_tools/impl.clj

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -959,7 +959,7 @@
959959
(and (= 1 (count arguments))
960960
(-> arguments first map?)))
961961

962-
(defn- default-tool-name
962+
(defn default-tool-name
963963
[]
964964
(when-let [path (System/getProperty "babashka.file")]
965965
(-> path io/file .getName)))
@@ -1042,18 +1042,13 @@
10421042
; groups have just :group-doc, no :title
10431043
doc' (assoc :group-doc doc'))))
10441044

1045-
(defn expand-tool-options
1046-
[dispatch-options]
1047-
(let [{:keys [tool-name transformer]} dispatch-options
1048-
tool-name' (or tool-name
1049-
(default-tool-name)
1050-
(throw (ex-info "No :tool-name specified" {:options dispatch-options})))
1045+
(defn build-command-root
1046+
[tool-name dispatch-options]
1047+
(let [{:keys [transformer]} dispatch-options
10511048
;; options are also the root descriptor for the built-in namespace
1052-
root (-> dispatch-options
1053-
(update :namespaces conj 'net.lewisship.cli-tools.builtins)
1054-
(build-command-group nil tool-name)
1055-
:subs)
1056-
root' (cond->> root
1057-
transformer (transformer dispatch-options))]
1058-
{:tool-name tool-name'
1059-
:command-root root'}))
1049+
root (-> dispatch-options
1050+
(update :namespaces conj 'net.lewisship.cli-tools.builtins)
1051+
(build-command-group nil tool-name)
1052+
:subs)]
1053+
(cond->> root
1054+
transformer (transformer dispatch-options))))

0 commit comments

Comments
 (0)