Skip to content

Commit

Permalink
Got output to CLI client working.
Browse files Browse the repository at this point in the history
Also added support for the logging option.
  • Loading branch information
ryugi committed May 25, 2017
1 parent 1600c0d commit fba0544
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 48 deletions.
4 changes: 2 additions & 2 deletions dev-resources/src/timi/dev.clj
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
[timi.server.cli.tcp :as cli-server]
[timi.server.components.core :as components]
[timi.server.core :as timi]
[timi.server.util :as util]
[trifl.java :refer [show-methods]]))

(def config (config/read-config))

(logger/set-level! (get-in config [:repl :log :ns])
(get-in config [:repl :log :level]))
(util/set-log-level config :repl)

(def system nil)
(def state :stopped)
Expand Down
52 changes: 28 additions & 24 deletions src/clj/timi/server/cli/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
(:require
[clojure.string :as string]
[clojure.tools.cli :as cli]
[taoensso.timbre :as log]
[timi.server.cli.parser :as parser]
[timi.server.util :as util]
[trifl.docs :as docs]))
Expand All @@ -11,35 +12,37 @@
["-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]})
:config [
:show]
:db [
:dump
:init]
: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))
(docs/get-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)
[{:keys [command options data]}]
(log/debug "dispatch command:" command)
(log/debug "dispatch options:" options)
(log/debug "dispatch data:" data)
(or
data
(case command
:help (do
(help)
(util/exit 0))
(help))
:config "not implemented"
:db "not implemented"
:project "not implemented"
Expand All @@ -51,10 +54,10 @@
Options:
```
-h, --help
-v, --version
-s, --summary
-l, --log-level LOG-LEVEL :warn Log level for CLI
-h, --help Display this help text
-v, --version Current version of Tímı
-s, --summary Get the generated summary
-l, --log-level LOG-LEVEL Set the server-side CLI log level
```
Commands:
```
Expand All @@ -67,14 +70,15 @@
For more information on any available options or subcommands for a given
command, use the `--help` option, e.g.:
```
timi config --help
$ timi config --help
```
"
[msg]
[config 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))
(parser/validate-args
(partial util/set-log-level config :cli-server)
#'help
(keys valid-commands))
(dispatch)))
25 changes: 16 additions & 9 deletions src/clj/timi/server/cli/parser.clj
Original file line number Diff line number Diff line change
@@ -1,31 +1,38 @@
(ns timi.server.cli.parser
(:require
[clojure.string :as string]
[taoensso.timbre :as log]
[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)))
(->> errors
(string/join \newline)
(str "The following error(s) occurred while attempting "
"to parse your command:\n\n")))

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

(defn validate-args
[{:keys [options arguments errors summary]} help-fn valid-commands]
[{:keys [options arguments errors summary]} log-fn help-fn valid-commands]
(let [command (keyword (first arguments))]
(cond
(:help options)
{:exit-message (help-fn) :ok? true}
{:data (help-fn) :options options}
(:summary options)
{:exit-message (println summary) :ok? true}
{:data summary :options options}
(:version options)
{:exit-message (util/get-version) :ok? true}
{:data (util/get-version) :options options}
(:log-level options)
(let [log-level (:log-level options)]
(log/warn "Setting log level to " log-level)
(log-fn log-level)
{:data :ok :options options})
errors
{:exit-message (error-msg errors)}
{:data (error-msg errors)}
;; custom validation on arguments
(validate-command valid-commands command)
{:command command :options options}
:else
{:exit-message (help-fn)})))
{:data (help-fn) :options options})))
8 changes: 4 additions & 4 deletions src/clj/timi/server/cli/tcp.clj
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
(ns timi.server.cli.tcp
(:require
[clojure.tools.logging :as log]
[net.tcp :as tcp]
[net.ty.channel :as channel]
[net.ty.pipeline :as pipeline]
[taoensso.timbre :as log]
[timi.config :as config]
[timi.server.cli.core :as cli]))

(def config (config/read-config))

(defn pipeline
[]
[config]
(pipeline/channel-initializer
[(pipeline/line-based-frame-decoder)
pipeline/string-decoder
pipeline/string-encoder
pipeline/line-frame-encoder
(pipeline/with-input [ctx msg]
(channel/write-and-flush! ctx (cli/run msg)))]))
(channel/write-and-flush! ctx (cli/run config msg)))]))

(defn serve
([]
(serve config))
([config]
(tcp/server
{:handler (pipeline)}
{:handler (pipeline config)}
(get-in config [:cli :server :host])
(get-in config [:cli :server :port]))))

Expand Down
11 changes: 5 additions & 6 deletions src/clj/timi/server/components/cli.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
(:require
[com.stuartsierra.component :as component]
[taoensso.timbre :as log]
[timi.server.cli.tcp :as cli-server]))
[timi.server.cli.tcp :as cli-server]
[trifl.java :as java]))

(defrecord CLIServer []
component/Lifecycle
Expand All @@ -19,11 +20,9 @@
(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
))
(when-let [server (:cli component)]
(log/debug "Using server object:" server)
(server))
(assoc component :cli nil)))

(defn new-server []
Expand Down
4 changes: 2 additions & 2 deletions src/clj/timi/server/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
[timi.server.components.core :as components]
[timi.server.datasource.core :as datasource]
[timi.server.infra.jdbc-extensions]
[timi.server.util :as util]
[trifl.java :as java])
(:gen-class))

(def config (config/read-config))

(logger/set-level! (get-in config [:log :ns])
(get-in config [:log :level]))
(util/set-log-level config :app)

(def app
"Used by the ring handler configuration in project.clj."
Expand Down
22 changes: 21 additions & 1 deletion src/clj/timi/server/util.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
(ns timi.server.util)
(ns timi.server.util
(:require
[clojusc.twig :as logger]))

(defn str->decimal
[dec-str]
Expand Down Expand Up @@ -29,3 +31,21 @@
([status msg]
(when msg (println msg))
(System/exit status)))

(defn set-log-level-type
[config config-keys level]
(logger/set-level! (get-in config (conj config-keys :ns))
(or level
(get-in config (conj config-keys :level)))))

(defn set-log-level
([config type]
(set-log-level config type nil))
([config type level]
(case type
:app (set-log-level-type config [:log] level)
:cli-client (set-log-level-type config [:cli :client :log] level)
:cli-server (set-log-level-type config [:cli :server :log] level)
:repl (set-log-level-type config [:repl :log] level))))


0 comments on commit fba0544

Please sign in to comment.