Skip to content

Commit

Permalink
Added args/opts parsing capability.
Browse files Browse the repository at this point in the history
  • Loading branch information
ryugi committed May 25, 2017
1 parent b512dbb commit 1600c0d
Show file tree
Hide file tree
Showing 14 changed files with 166 additions and 18 deletions.
2 changes: 1 addition & 1 deletion dev-resources/src/timi/dev.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
[net.ty.channel :as channel]
[net.ty.pipeline :as pipeline]
[taoensso.timbre :as log]
[timi.config :as config]
[timi.server.cli.core :as cli]
[timi.server.cli.tcp :as cli-server]
[timi.config :as config]
[timi.server.components.core :as components]
[timi.server.core :as timi]
[trifl.java :refer [show-methods]]))
Expand Down
1 change: 1 addition & 0 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
[org.clojure/clojure "1.8.0"]
[org.clojure/data.json "0.2.6"]
[org.clojure/java.jdbc "0.7.0-alpha2"]
[org.clojure/tools.cli "0.3.5"]
[org.webjars/bootstrap-datepicker "1.6.4"]
[org.webjars/bootswatch-superhero "3.3.7"]
[org.webjars/font-awesome "4.7.0"]
Expand Down
1 change: 1 addition & 0 deletions src/clj/timi/server/cli/commands/config.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(ns timi.server.cli.commands.config)
1 change: 1 addition & 0 deletions src/clj/timi/server/cli/commands/db.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(ns timi.server.cli.commands.db)
1 change: 1 addition & 0 deletions src/clj/timi/server/cli/commands/project.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(ns timi.server.cli.commands.project)
1 change: 1 addition & 0 deletions src/clj/timi/server/cli/commands/task.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(ns timi.server.cli.commands.task)
81 changes: 80 additions & 1 deletion src/clj/timi/server/cli/core.clj
Original file line number Diff line number Diff line change
@@ -1 +1,80 @@
(ns timi.server.cli.core)
(ns timi.server.cli.core
(:require
[clojure.string :as string]
[clojure.tools.cli :as cli]
[timi.server.cli.parser :as parser]
[timi.server.util :as util]
[trifl.docs :as docs]))

(def options
[["-h" "--help"]
["-v" "--version"]
["-s" "--summary"]
["-l" "--log-level LOG-LEVEL" "Log level for CLI"
:default :warn
:parse-fn #(read-string %)
:validate [#(contains? #{:debug :info :warn :error :fatal} %)
"Must be one of :debug :info :warn :error :fatal"]]])

(def valid-commands
{:help nil
:config [:show]
:db [:init :dump]
:project [:create]
:task [:create]})

(defn help
"This function generates the output for the `help` options and/or commands."
[]
(docs/print-docstring 'timi.server.cli.core 'run))

(defn dispatch
[{:keys [command options exit-message ok?]}]
(println command)
(println options)
(println exit-message)
(println ok?)
(if exit-message
(util/exit (if ok? 0 1) exit-message)
(case command
:help (do
(help)
(util/exit 0))
:config "not implemented"
:db "not implemented"
:project "not implemented"
:task "not implemented")))

(defn run
"
Usage: `timi [options] command [subcommands [options]]`
Options:
```
-h, --help
-v, --version
-s, --summary
-l, --log-level LOG-LEVEL :warn Log level for CLI
```
Commands:
```
config XXX
db XXX
project XXX
task XXX
```
For more information on any available options or subcommands for a given
command, use the `--help` option, e.g.:
```
timi config --help
```
"
[msg]
(-> msg
(string/split #"\s")
((fn [x] (println x) x))
(cli/parse-opts options :in-order true)
((fn [x] (println x) x))
(parser/validate-args #'help (keys valid-commands))
(dispatch)))
31 changes: 31 additions & 0 deletions src/clj/timi/server/cli/parser.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
(ns timi.server.cli.parser
(:require
[clojure.string :as string]
[timi.server.util :as util]))

(defn error-msg [errors]
(str "The following error(s) occurred while attempting to parse your "
"command:\n\n"
(string/join \newline errors)))

(defn validate-command
[valid-commands command]
((into #{} valid-commands) command))

(defn validate-args
[{:keys [options arguments errors summary]} help-fn valid-commands]
(let [command (keyword (first arguments))]
(cond
(:help options)
{:exit-message (help-fn) :ok? true}
(:summary options)
{:exit-message (println summary) :ok? true}
(:version options)
{:exit-message (util/get-version) :ok? true}
errors
{:exit-message (error-msg errors)}
;; custom validation on arguments
(validate-command valid-commands command)
{:command command :options options}
:else
{:exit-message (help-fn)})))
14 changes: 1 addition & 13 deletions src/clj/timi/server/cli/tcp.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,6 @@

(def config (config/read-config))

(defn parse-commands
[cmds]
(log/info "In parse-commands, got:" cmds)
cmds)

(defn run-commands
[cmds]
(log/info "In run-commands, got:" cmds)
cmds)

(defn pipeline
[]
(pipeline/channel-initializer
Expand All @@ -27,9 +17,7 @@
pipeline/string-encoder
pipeline/line-frame-encoder
(pipeline/with-input [ctx msg]
(channel/write-and-flush! ctx (-> msg
(parse-commands)
(run-commands))))]))
(channel/write-and-flush! ctx (cli/run msg)))]))

(defn serve
([]
Expand Down
30 changes: 30 additions & 0 deletions src/clj/timi/server/components/cli.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
(ns timi.server.components.cli
(:require
[com.stuartsierra.component :as component]
[taoensso.timbre :as log]
[timi.server.cli.tcp :as cli-server]))

(defrecord CLIServer []
component/Lifecycle

(start [component]
(log/info "Starting CLI server ...")
(let [cfg (get-in component [:cfg])
server (cli-server/serve cfg)]
(log/trace "Using config:" cfg)
(log/debug "Component keys:" (keys component))
(log/debug "Successfully created server:" server)
(assoc component :cli server)))

(stop [component]
(log/info "Stopping CLI server ...")
(log/debug "Component keys" (keys component))
(if-let [server (:cli component)]
(do (log/warn "XXX Implement CLI server shutdown function")
(log/debug "Using server object:" server)
;; XXX make shutdown call here
))
(assoc component :cli nil)))

(defn new-server []
(->CLIServer))
4 changes: 4 additions & 0 deletions src/clj/timi/server/components/core.clj
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
(ns timi.server.components.core
(:require
[com.stuartsierra.component :as component]
[timi.server.components.cli :as cli]
[timi.server.components.httpd :as httpd]))

(defn init [app config]
(component/system-map
:cfg config
:cli (component/using
(cli/new-server)
[:cfg])
:httpd (component/using
(httpd/new-server app)
[:cfg])))
Expand Down
11 changes: 11 additions & 0 deletions src/clj/timi/server/util.clj
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,14 @@
[keyword-str]
(when (seq keyword-str)
(keyword (subs keyword-str 1))))

(defn get-version
[]
"[add version info]")

(defn exit
([status]
(exit status nil))
([status msg]
(when msg (println msg))
(System/exit status)))
2 changes: 1 addition & 1 deletion src/cljs/cli/timi/client/cli.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

(defn args->str
[args]
(str (string/join "\n" args) "\n"))
(str (string/join args) "\n"))

;;; Callbacks

Expand Down
4 changes: 2 additions & 2 deletions src/cljs/cli/timi/client/config.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
(read-config "config/local/config.edn"))
([filename]
(->> "utf-8"
(.readFileSync fs filename)
(cljs.reader/read-string))))
(.readFileSync fs filename)
(cljs.reader/read-string))))

(def data (read-config))

0 comments on commit 1600c0d

Please sign in to comment.