Skip to content

Commit

Permalink
Fix command line usage and uberjar generation.
Browse files Browse the repository at this point in the history
  • Loading branch information
wardle committed Sep 6, 2021
1 parent 7f78096 commit e8f21aa
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 50 deletions.
29 changes: 14 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ Download the main dm+d distribution; you can optionally also download the 'extra
ATC BNF codes. Once downloaded, unzip one or both into a common directory and run:

```shell
clj -M:install --db dmd-2021-07-01.db ~/Downloads/dmd-2021-07-01
clj -M:run install --db dmd-2021-07-01.db ~/Downloads/dmd-2021-07-01
```
or
```shell
java -jar dmd.jar --db dmd-2021-07-01.db ~/Downloads/dmd-2021-07-01
```

This will look for files in the directory specified and create a new file-based
Expand Down Expand Up @@ -109,13 +113,16 @@ or running:

```shell
clj -M:run list --api-key /var/local/trud/api-key.txt
# or java -jar dmd.jar list --api-key /var/local/trud/api-key.txt
```
or
```shell
java -jar dmd.jar list --api-key /var/local/trud/api-key.txt
```


Result:
```shell
➜ dmd git:(main) ✗ clj -M:run --api-key /var/local/trud/api-key.txt list
➜ dmd git:(main) ✗ clj -M:run list --api-key /var/local/trud/api-key.txt

| :id | :releaseDate | :name |
|--------------------------------------+--------------+----------------|
Expand All @@ -129,7 +136,7 @@ To download a specific edition, you can download manually, unzip and then
directly import from that distribution.

```shell
clj -M:run --db dmd.db install /tmp/downloads/trud
clj -M:run install --db dmd.db /tmp/downloads/trud
```

Or you can specify the version and let `dmd` download for you.
Expand All @@ -139,17 +146,16 @@ Specify which release you want by date according to ISO-8601 standard:
clj -M:run --api-key /var/local/trud/api-key.txt download 2021-04-05
```


#### 4. Run a HTTP server

Once you have downloaded a distribution, you can use it to run a very fast HTTP server.

```shell
clj -M:serve dmd-2021-04-12.db 8080
clj -M:run serve --db dmd-2021-04-12.db --port 8080
```
or
```shell
java -jar dmd-server.jar dmd-2021-04-12.db 8080
java -jar dmd.jar --db dmd-2021-04-12.db --port 8080
```

As it is very likely that the complete dm+d dataset will fit into the memory
Expand Down Expand Up @@ -526,16 +532,9 @@ clj -M:lint/kondo
clj -X:jar
```

#### Build and run command-line utility uberjar
#### Build and run an uberjar

```shell
clj -X:uberjar
java -jar target/dmd.jar --help
```

#### Build and run server uberjar

```shell
clj -X:server-uberjar
java -jar target/dmd-server.jar dmd-2021-04-12.db 8080
```
20 changes: 17 additions & 3 deletions src/com/eldrix/dmd/cli.clj → cmd/com/eldrix/dmd/cli.clj
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
(ns com.eldrix.dmd.cli
(:gen-class)
(:require [clojure.tools.cli :as cli]
(:require [clojure.string :as str]
[clojure.tools.cli :as cli]
[com.eldrix.dmd.core :as dmd]
[clojure.string :as str]))
[com.eldrix.dmd.serve :as serve]
[clojure.tools.logging.readable :as log]))

(def cli-options
[[nil "--api-key PATH" "Path to file containing TRUD API key"]
[nil "--cache-dir PATH" "Path to cache directory for TRUD downloads"]
[nil "--db FILENAME" "Name of database; usually optional. A default will be chosen if omitted"]
["-p" "--port PORT" "Port to use"]
["-h" "--help"]])

(defn usage [options-summary]
Expand All @@ -18,6 +21,7 @@
" - list : list available distributions from TRUD"
" - download : download and install a specific dm+d release (e.g. 2021-04-05)"
" - install : install from a manually downloaded/unzipped distribution"
" - serve : run a HTTP server"
"Options:"
options-summary]
(str/join \newline)))
Expand Down Expand Up @@ -52,6 +56,15 @@
:else
(dmd/install-from-dirs db params)))

(defn run-server [{:keys [db port] :or {port "8080"}}]
(if db
(let [st (dmd/open-store db)]
(log/info "starting server using " db " on port" port)
(try (serve/start-server st (Integer/parseInt port))
(catch NumberFormatException e
(log/error "invalid port:" port))))
(log/error "You must specify a database using --db")))

(defn -main [& args]
(let [{:keys [options arguments summary errors]} (cli/parse-opts args cli-options)]
(cond
Expand All @@ -72,7 +85,8 @@
(= "latest" command) (download options nil)
(= "list" command) (list-available options)
(= "download" command) (download options params)
(= "install" command) (install options params))))))
(= "install" command) (install options params)
(= "serve" command) (run-server options))))))

(comment
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,6 @@
(stop-dev)
(start-dev svc port))

(defn -main [& args]
(if-not (= 2 (count args))
(println "Usage: clj -M:serve <database> <port>\n or java -jar dmd-server.jar <database> <port>")
(let [[filename port] args
store (com.eldrix.dmd.store2/open-store filename)
port' (Integer/parseInt port)]
(log/info "starting NHS dm+d server" {:port port' :filename filename})
(start-server store port'))))

(comment
(do
(require '[io.pedestal.test])
Expand Down
File renamed without changes.
32 changes: 9 additions & 23 deletions deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
org.clojure/data.json {:mvn/version "2.4.0"}
org.clojure/data.xml {:mvn/version "0.2.0-alpha6"}
org.clojure/data.zip {:mvn/version "1.0.0"}
org.clojure/tools.cli {:mvn/version "1.0.206"}
org.clojure/tools.logging {:mvn/version "1.1.0"}

com.eldrix/trud {:git/url "https://github.com/wardle/trud.git"
Expand All @@ -17,20 +16,15 @@
datalevin/datalevin {:mvn/version "0.4.40"}}

:aliases {:dev
{:extra-paths ["serve" "test/src" "test/resources"]}
{:extra-paths ["cmd" "test/src" "test/resources"]}

:run
{:main-opts ["-m" "com.eldrix.dmd.cli"]
:jvm-opts ["--add-opens" "java.base/java.nio=ALL-UNNAMED"
"--add-opens" "java.base/sun.nio.ch=ALL-UNNAMED"
"--illegal-access=permit"]}

:serve
{:extra-paths ["serve"]
:extra-deps {io.pedestal/pedestal.service {:mvn/version "0.5.9"}
{:extra-paths ["cmd"]
:extra-deps {org.clojure/tools.cli {:mvn/version "1.0.206"}
io.pedestal/pedestal.service {:mvn/version "0.5.9"}
io.pedestal/pedestal.jetty {:mvn/version "0.5.9"}
ch.qos.logback/logback-classic {:mvn/version "1.2.5"}}
:main-opts ["-m" "com.eldrix.dmd.serve"]
:main-opts ["-m" "com.eldrix.dmd.cli"]
:jvm-opts ["--add-opens" "java.base/java.nio=ALL-UNNAMED"
"--add-opens" "java.base/sun.nio.ch=ALL-UNNAMED"
"--illegal-access=permit"]}
Expand Down Expand Up @@ -83,22 +77,14 @@
:extra-deps {jonase/eastwood {:mvn/version "RELEASE"}}}

:jar
{:replace-deps {com.github.seancorfield/depstar {:mvn/version "2.1.278"}}
{:replace-deps {com.github.seancorfield/depstar {:mvn/version "2.1.297"}}
:exec-fn hf.depstar/jar
:exec-args {:jar "target/dmd-lib.jar"}}

:uberjar
{:replace-deps {com.github.seancorfield/depstar {:mvn/version "2.1.278"}}
{:replace-deps {com.github.seancorfield/depstar {:mvn/version "2.1.297"}}
:exec-fn hf.depstar/uberjar
:exec-args {:jar "target/dmd.jar"
:aot true
:main-class "com.eldrix.dmd.cli"}}

:server-uberjar
{:replace-deps {com.github.seancorfield/depstar {:mvn/version "2.1.278"}}
:exec-fn hf.depstar/uberjar
:exec-args {:jar "target/dmd-server.jar"
:aliases [:serve]
:aot true
:main-class com.eldrix.dmd.serve}}
}}
:aliases [:run]
:main-class "com.eldrix.dmd.cli"}}}}

0 comments on commit e8f21aa

Please sign in to comment.