Skip to content

Commit

Permalink
Move CLI functionality into separate namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniils Petrovs committed Oct 1, 2021
1 parent c3d6e42 commit 146b878
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 49 deletions.
48 changes: 48 additions & 0 deletions src/atoss_cli/cli.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
(ns atoss-cli.cli
(:require [atoss-cli.atoss :refer [valid-day-codes]])
(:import (java.text SimpleDateFormat)
(java.util Date)))

(def today-date (.format
(SimpleDateFormat. "dd.MM.yyyy")
(new Date)))

(def desc "ATOSS CLI by Platogo Interactive Entertainment Gmbh.
Work seamlessly with ATOSS time sheets.")

(def help-header "
\033[1;37mUSAGE\u001b[0m
atoss-cli <command> [args]
\033[1;37mCOMMANDS\u001b[0m
log: Log time pair for today or a specific date
view: View month overview of logged time")

(def options
;; An option with a required argument
[["-d" "--date DATE" "Date in the format DD.MM.YYYY"
:default today-date] ;; FIXME: Add validation
["-c" "--day-code CODE" "Valid ATOSS day code (e.g. wh for WFH) can also be left blank."
:default nil
:validate [#(contains? valid-day-codes %) "Must be a valid ATOSS time code."]]
["-s" "--start-time TIME" "Work start time in the format HH:MM"
:default "9:00"]
["-e" "--end-time TIME" "Work end time in the format HH:MM"
:default "17:00"]
;; A non-idempotent option (:default is applied first)
["-v" nil "Verbosity level"
:id :verbosity
:default 0
:update-fn inc]
;; A boolean option defaulting to nil
["-h" "--help" "Show this help printout."
:default false]])

(defn print-help
[args-summary]
(print desc)
(newline)
(print help-header)
(newline)
(newline)
(print args-summary))
55 changes: 6 additions & 49 deletions src/atoss_cli/core.clj
Original file line number Diff line number Diff line change
@@ -1,54 +1,11 @@
(ns atoss-cli.core
"Entrypoint module for the ATOSS CLI."
(:require [clojure.tools.cli :refer [parse-opts]]
[atoss-cli.atoss :as atoss])
(:import (java.util Date Collection) [java.text SimpleDateFormat])
[atoss-cli.atoss :as atoss]
[atoss-cli.cli :as cli])
(:import (java.util Collection))
(:gen-class))

(def today-date (.format
(SimpleDateFormat. "dd.MM.yyyy")
(new Date)))

(def desc "ATOSS CLI by Platogo Interactive Entertainment Gmbh.
Work seamlessly with ATOSS time sheets.")

(def help-header "
\033[1;37mUSAGE\u001b[0m
atoss-cli <command> [args]
\033[1;37mCOMMANDS\u001b[0m
log: Log time pair for today or a specific date
view: View month overview of logged time")

(def cli-options
;; An option with a required argument
[["-d" "--date DATE" "Date in the format DD.MM.YYYY"
:default today-date] ;; FIXME: Add validation
["-c" "--day-code CODE" "Valid ATOSS day code (e.g. wh for WFH) can also be left blank."
:default nil
:validate [#(contains? atoss/valid-day-codes %) "Must be a valid ATOSS time code."]]
["-s" "--start-time TIME" "Work start time in the format HH:MM"
:default "9:00"]
["-e" "--end-time TIME" "Work end time in the format HH:MM"
:default "17:00"]
;; A non-idempotent option (:default is applied first)
["-v" nil "Verbosity level"
:id :verbosity
:default 0
:update-fn inc]
;; A boolean option defaulting to nil
["-h" "--help" "Show this help printout."
:default false]])

(defn -print-help
[args-summary]
(print desc)
(newline)
(print help-header)
(newline)
(newline)
(print args-summary))

(defn log-time
"Log a time pair for a given day."
[{opts :options}]
Expand Down Expand Up @@ -84,10 +41,10 @@ Work seamlessly with ATOSS time sheets.")
(let [{^Collection arguments :arguments
summary :summary,
options :options,
:as opts} (parse-opts args cli-options)]
:as opts} (parse-opts args cli/options)]
(cond
(options :help) (-print-help summary)
(options :help) (cli/print-help summary)
(= (first arguments) "view") (show-month-overview)
(= (first arguments) "log") (log-time opts)
:else (-print-help summary))
:else (cli/print-help summary))
(flush)))

0 comments on commit 146b878

Please sign in to comment.