Skip to content

Commit

Permalink
v0.3.2: Fixes #60 / Fixes #61 / Fixes #63
Browse files Browse the repository at this point in the history
  • Loading branch information
lenz committed Jan 1, 2019
1 parent dd35ec8 commit 469f029
Show file tree
Hide file tree
Showing 15 changed files with 186 additions and 24 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# Change Log
All notable changes to this project will be documented in this file. This change log follows the conventions of [keepachangelog.com](http://keepachangelog.com/).

## 0.3.2 - 2019-01-01
### Changed
* Fixes #63 - Script return value is set in Planck, though it is currently unsigned 8-bit
* Fixes #61 - Reading EDN works in Planck, also as HTTP/S
* Fixes #60 - Reading JSON works in Planck, also as HTTP/S
* Added some example scripts for Planck

## 0.3.1 - 2018-12-30
### Changed
* Initial support for Planck - now you can use CLI-matic with ClojureScript!
Expand Down
4 changes: 4 additions & 0 deletions examples-cljs-planck/data/edn-example.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{:server-type [:cloud :or :not]
:region "<your AWS Region>" ;; e.g. us-east-1
;; :creds-profile "<AWS Profile Name>" ;; Optional
:proxy-port "<local-port>"}
28 changes: 28 additions & 0 deletions examples-cljs-planck/data/json-example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"firstName": "John",
"lastName": "Smith",
"isAlive": true,
"age": 27,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021-3100"
},
"phoneNumbers": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "office",
"number": "646 555-4567"
},
{
"type": "mobile",
"number": "123 456-7890"
}
],
"children": [],
"spouse": null
}
5 changes: 5 additions & 0 deletions examples-cljs-planck/data/multiline-text.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
The limerick packs laughs anatomical
Into space that is quite economical.
But the good ones I've seen
So seldom are clean
And the clean ones so seldom are comical.
48 changes: 48 additions & 0 deletions examples-cljs-planck/exit-status.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env bash
"exec" "plk" "-Sdeps" "{:deps {cli-matic {:mvn/version \"0.3.2\"}}}" "-Ksf" "$0" "$@"

(ns exit-code
(:require [cli-matic.core :refer [run-cmd]]))

;; To run this, try from the project root:
;;
;; $ ./exit-status.cljs exit --mode NONE
;; echo $?
;; 0
;; $ ./exit-status.cljs exit --mode ONE
;; $ echo $?
;; 1
;; $ ./exit-status.cljs exit --mode XXX
;; ** ERROR: **
;; JVM Exception: #object[Error Error: No matching clause: XXX]
;; $ echo $?
;; 255


(defn exiter
"Just exits with an exit code.
Unknown values cause an exception."
[{:keys [mode]}]
(condp = mode
"NONE" nil
"ONE" 1
))


(def CONFIGURATION
{:app {:command "exit-code"
:description "Return the exit code"
:version "0.0.1"}
:global-opts []
:commands [{:command "exit" :short "x"
:description ["Forces an exit code."]
:opts [{:option "mode" :as "NONE|ONE|ERROR" :type :string :default :present}]
:runs exiter}
]})

(defn -main
[& args]
(run-cmd args CONFIGURATION))

(set! *main-cli-fn* -main)

43 changes: 43 additions & 0 deletions examples-cljs-planck/read-file.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env bash
"exec" "plk" "-Sdeps" "{:deps {cli-matic {:mvn/version \"0.3.2\"}}}" "-Ksf" "$0" "$@"

(ns read-file
(:require [cli-matic.core :refer [run-cmd]]))

;; To run this, try from the project root:
;;
;; ./read-file.cljs p --text data/multiline-text.txt --lines data/multiline-text.txt --json data/json-example.json --edn data/edn-example.edn
;;
;; You can also use URLs:
;;
;; ./read-file.cljs p --json https://jsonplaceholder.typicode.com/todos/1

(defn printer
"Just prints read values."
[{:keys [text json edn lines]}]
(prn {:text text
:lines lines
:json json
:edn edn}))


(def CONFIGURATION
{:app {:command "read-file"
:description "Reads all parameters as files."
:version "0.0.1"}
:global-opts []
:commands [{:command "printer" :short "p"
:description ["Prints parameters. All files can be local or http/https"]
:opts [{:option "text" :as "A text file as single string" :type :slurp}
{:option "lines" :as "A text file as lines" :type :slurplines}
{:option "json" :as "A JSON file" :type :jsonfile}
{:option "edn" :as "An EDN file" :type :ednfile}]
:runs printer}
]})

(defn -main
[& args]
(run-cmd args CONFIGURATION))

(set! *main-cli-fn* -main)

2 changes: 1 addition & 1 deletion examples-cljs-planck/toycalc-spec.cljs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env bash
"exec" "plk" "-Sdeps" "{:deps {cli-matic {:mvn/version \"0.3.1\"}}}" "-Ksf" "$0" "$@"
"exec" "plk" "-Sdeps" "{:deps {cli-matic {:mvn/version \"0.3.2\"}}}" "-Ksf" "$0" "$@"

(ns toycalc-spec
(:require [cli-matic.core :refer [run-cmd]]
Expand Down
2 changes: 1 addition & 1 deletion examples-cljs-planck/toycalc.cljs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env bash
"exec" "plk" "-Sdeps" "{:deps {cli-matic {:mvn/version \"0.3.0\"}}}" "-Ksf" "$0" "$@"
"exec" "plk" "-Sdeps" "{:deps {cli-matic {:mvn/version \"0.3.2\"}}}" "-Ksf" "$0" "$@"

(ns toycalc
(:require [cli-matic.core :refer [run-cmd]]
Expand Down
13 changes: 8 additions & 5 deletions planck.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,18 @@ When debugging, it is sometimes useful to:

* Help generation
* Simple parameters
* clojure.spec parameter validation
* Slurping of files and HTTP resources
* `clojure.spec` parameter validation
* Slurping of files and HTTP resources (see `read-file.cljs`)
* Environment variables
* Script return value is set, though it is currently unsigned 8-bit [#63] (see `exit-status.cljs`)
* Reading EDN works, also as HTTP/S [#61] (see `read-file.cljs`)
* Reading JSON works, also as HTTP/S [#60] (see `read-file.cljs`)

## What does not work

* Expound does not work with ClojureScript 1.10.439 in defining custom specs.
* No automatic reading of JSON [#60] / EDN / YAML parameters (at the moment, but it's easily doable)
* Scripot return value is not set #63
* Dates - what to do with dates?
* No automatic reading of YAML [#62] parameters
* Dates - what to do with dates? [#59] - at the moment we throw an exception.



2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject cli-matic "0.3.1"
(defproject cli-matic "0.3.2"
:description "Compact [sub]command line parsing library, for Clojure"
:url "https://github.com/l3nz/cli-matic"
:license {:name "Eclipse Public License, v2"
Expand Down
2 changes: 1 addition & 1 deletion src/cli_matic/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
CLI-matic has one main entry-point: [[run-cmd]].
As an end-user, you need nothing else, but the documentation
that explains how parameters are to bhe run.
that explains how parameters are to be run.
See `examples/` to get started quickly.
Expand Down
5 changes: 5 additions & 0 deletions src/cli_matic/optionals.clj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
{:pre [with-cheshire?]}
(apply (ns-resolve (symbol "cheshire.core") (symbol "decode")) args))

(defn json-decode
"Decodes a JSON string, without keywordizing."
[json]
(json-decode-cheshire json))

;; YAML

(def with-yaml?
Expand Down
16 changes: 11 additions & 5 deletions src/cli_matic/optionals.cljs
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
(ns cli-matic.optionals
"### This namespace contains optional dependencies for CLJS.
JSON is always available in CLJS.
"
(:require [clojure.string :as str]
[expound.alpha :as expound]
[clojure.spec.alpha :as s]))

;; CHESHIRE

(defn json-decode
"
We should use Transit if present, but this should be enough to get us started.
(defn json-decode-cheshire
""
[& args]
nil)
https://cljs.github.io/api/cljs.core/js-GTclj
"
[json]
(let [a (.parse js/JSON json)]
(js->clj a)))

;; YAML


(defn yaml-decode
""
[& args]
nil)
(throw (ex-info "No YAML decoding in CLJS." args)))

;; ORCHESTRA

Expand Down
29 changes: 21 additions & 8 deletions src/cli_matic/platform.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"
(:require [planck.core :as plk]
[planck.environ :as plkenv]
[cljs.reader :as csrdr]
[clojure.string :as str]))

(defn read-env
Expand All @@ -19,21 +20,31 @@
(get plkenv/env kw nil)))

(defn exit-script
"Terminates execution with a return value."
"Terminates execution with a return value.
Please note that in Planck, return codes seem to be 8-bit unsigned ints.
"
[retval]
nil)
(plk/exit retval))

(defn add-shutdown-hook
"Add a shutdown hook.
Does not work (?) on CLJS.
Does not work (?) on CLJS and we will throw an exception.
It might be conceivable that in JS-land, we save this locally in this namespace
and then call it on `exit-script`.
"
[fnToCallOnShutdown]

nil)
(if (ifn? fnToCallOnShutdown)
(throw (ex-info "Shutdown hooks not supported outside the JVM" nil))))

(defn slurp-file
"
Luckily, Planck implements slurp for us.
No slurping in Node-land.
See https://github.com/pkpkpk/cljs-node-io
Expand Down Expand Up @@ -61,12 +72,14 @@
Date object; if conversion
fails, returns nil."
[s]
nil)
(throw (ex-info "Dates not supported in CLJS." s)))

(defn parseEdn
"
See https://stackoverflow.com/questions/44661385/how-do-i-read-an-edn-file-from-clojurescript-running-on-nodejs
"
This is actually a piece of ClojureScript, though it lives in a different NS.
See https://cljs.github.io/api/cljs.reader/read-string
"
[edn-in]
nil)
(csrdr/read-string edn-in))

4 changes: 2 additions & 2 deletions src/cli_matic/presets.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@
(defn asDecodedJsonValue
"Decodes the value as a JSON object."
[s]
(OPT/json-decode-cheshire s))
(OPT/json-decode s))

(defn asDecodedJsonFile
"Decodes the contents of a file as a JSON object."
[filename]
(OPT/json-decode-cheshire (asSingleString filename)))
(OPT/json-decode (asSingleString filename)))

(defn asDecodedYamlValue
"Decodes the value as a YAML object."
Expand Down

0 comments on commit 469f029

Please sign in to comment.