forked from infi-nl/alibi
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
166 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
(ns timi.server.cli.commands.config) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
(ns timi.server.cli.commands.db) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
(ns timi.server.cli.commands.project) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
(ns timi.server.cli.commands.task) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)}))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters