Skip to content

Commit

Permalink
Moved configuration into component.
Browse files Browse the repository at this point in the history
Also added logging component.
  • Loading branch information
ryugi committed May 27, 2017
1 parent f44cb49 commit 1760cfe
Show file tree
Hide file tree
Showing 18 changed files with 371 additions and 225 deletions.
11 changes: 5 additions & 6 deletions dev-resources/src/timi/dev.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,26 @@
[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.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))

(util/set-log-level config :repl)

(def system nil)
(def state :stopped)

;; Set up default logging here because the system hasn't had a chance to
;; it yet; once the configuration is available, the logger will re-configure.
(logger/set-level! 'timi :info)

(defn init []
(if (contains? #{:initialized :started :running} state)
(log/error "System has aready been initialized.")
(do
(alter-var-root #'system
(constantly (timi/get-system config)))
(constantly (timi/get-system)))
(alter-var-root #'state (fn [_] :initialized))))
state)

Expand Down
19 changes: 0 additions & 19 deletions src/clj/timi/config.clj

This file was deleted.

90 changes: 89 additions & 1 deletion src/clj/timi/server/cli/commands/project.clj
Original file line number Diff line number Diff line change
@@ -1 +1,89 @@
(ns timi.server.cli.commands.project)
(ns timi.server.cli.commands.project
(:require
[clojure.pprint :refer [pprint]]
[clojure.string :as string]
[clojure.tools.cli :as cli]
[taoensso.timbre :as log]
[timi.server.cli.parser :as parser]
[timi.server.datasource.sqlite.migrations :as db-migrator]
[timi.server.domain.project :as project]
[timi.server.domain.project-admin-app-svc :as projects]
[timi.server.util :as util]
[trifl.docs :as docs])
(:import
(clojure.lang PersistentHashMap)
(java.lang Object String)))

(def options
;; Note that any options added here need to be named differently than those
;; in timi.server.cli.core/options.
[])

(defn validate-subcommand
[subcommand]
(log/info "Validating subcommand ...")
(log/trace "Command:" subcommand)
(#{:help :list :create} subcommand))

(defn validate-billing-method
[method]
(log/info "Validating billing method ...")
(log/trace "method:" method)
(#{:fixed-price :hourly :overhead} method))

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

(defn handle-unknown-subcommand
[subcommand]
(let [msg (format "The subcommand '%s' is not supported."
(name subcommand))]
(log/error msg)
(format "\nERROR: %s\n\n%s" msg (help))))

(defn list-projects
[config]
(log/info "Listing projects ...")
(projects/get))

(defn create-project
[config project-name billing-method]
(log/info "Creating new project %s with billing method %s ..."
project-name billing-method)
;; XXX use the validate-billing-method function above
(let [cmd {:project-name project-name
:billing-method billing-method}
project-id (projects/new-project! cmd)]
(log/debug "Created project.")
(str ":project-id " project-id)))

(defn dispatch
[config {:keys [options arguments errors data subcommands]}]
(let [subcommand (or (first subcommands) :help)]
(log/infof "Running '%s' subcommand ..." subcommand)
(log/trace "Using config:" config)
(log/debug "dispatch subcommands:" subcommands)
(log/debug "dispatch arguments:" arguments)
(case (validate-subcommand subcommand)
:help (help)
:list (list-projects config)
:create (create-project config
(nth arguments 2 nil)
(nth subcommands 2 :hourly))
(handle-unknown-subcommand subcommand))))

(defn run
"
Usage: `timi project [subcommands [options]]`
Subommands:
```
help Display this help text
list List all projects
create NAME [BILLING-METHOD] Create a new project to track
```
"
[config parsed]
(dispatch config parsed))
4 changes: 3 additions & 1 deletion src/clj/timi/server/cli/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
[taoensso.timbre :as log]
[timi.server.cli.commands.config :as config-cmd]
[timi.server.cli.commands.db :as db-cmd]
[timi.server.cli.commands.project :as project-cmd]
[timi.server.cli.commands.task :as task-cmd]
[timi.server.cli.parser :as parser]
[timi.server.util :as util]
[trifl.docs :as docs]))
Expand Down Expand Up @@ -63,7 +65,7 @@
:help (help)
:config (config-cmd/run config parsed)
:db (db-cmd/run config parsed)
:project "not implemented"
:project (project-cmd/run config parsed)
:task "not implemented")))

;; Note that the option summary and commands are "hard-documented" here due
Expand Down
Loading

0 comments on commit 1760cfe

Please sign in to comment.