Add project --history
[candle] / src / main / cli.lisp
1 (in-package #:candle-cli)
2
3 (defun main-options ()
4  '((:name :help :short "h" :long "help" :description "Print this usage.")
5    (:name :port :short "p" :long "port" :takes-argument t :variable-name "PORT"
6     :description "Port on which to listen for commands.  Defaults to 25004")
7    (:positional "<command>" :required t :description "Command to send to candle server")))
8
9 (defun project-options ()
10  '((:name :help :short "h" :long "help" :description "Print this usage.")
11    (:name :add :long "add" :takes-argument t :description
12     "Add a project.  NAME is the name of the project, which must not include colons, while SRC is the location of the repository for cloning.  This location must be accessible by the machine running candle."
13     :variable-name "NAME:SRC")
14    (:name :show :long "show" :takes-argument t :description
15     "Show branch information for a project named by NAME."
16     :variable-name "NAME")
17    (:name :refresh :long "refresh" :takes-argument t :description
18     "Refresh project named by NAME."
19     :variable-name "NAME")
20    (:name :history :long "history" :takes-argument t :description
21     "Show history of project named by NAME."
22     :variable-name "NAME")
23    (:name :delete :long "delete" :takes-argument t :description
24     "Delete a project named by NAME."
25     :variable-name "NAME")))
26
27 (defun job-options ()
28  '((:name :help :short "h" :long "help" :description "Print this usage.")
29    (:name :project-name :long "project" :takes-argument t :description "The project name for the jobs under consideration.")))
30
31 (defun run-options ()
32  '((:name :help :short "h" :long "help" :description "Print this usage.")))
33
34 (defun main-usage ()
35  (format t "~A"
36   (opera:usage
37    "candle"
38    (main-options)
39    "Interacts with candle server.  The available commands are:
40   project   List, show or add projects
41   job       List or show jobs
42   run       Local command.  Run candle in the current working directory")))
43
44 (defgeneric execute-command (command args))
45
46 (defmethod execute-command (command args)
47  (format *error-output* "Unknown command '~(~A~)'.  See 'candle --help'.~%" command))
48
49 (defun add-project (project-definition)
50  (let
51   ((pos (position #\: project-definition)))
52   (cond
53    ((not pos) (format *error-output* "Project definition ~A is not valid.  See 'candle project --help'.~%" project-definition))
54    (t
55     (let*
56      ((name (subseq project-definition 0 pos))
57       (src (subseq project-definition (1+ pos))))
58      (communication:query `(candle:add-project ,name ,src))
59      (format t "Added project ~A at src definition ~A~%" name src))))))
60
61 (defun delete-project (name)
62  (communication:query `(candle:delete-project ,name))
63  (format t "Removed project ~A~%" name))
64
65 (defun show-project (name)
66  (mapcar
67   (lambda (branch-info)
68    (format t "~A: ~A ~A at ~A~%"
69     (first branch-info)
70     (fourth branch-info)
71     (case (second branch-info)
72      (:succeeded (format nil "~c[1;32mPassed~c[0m" #\Esc #\Esc))
73      (:failed (format nil "~c[1;31mFailed~c[0m" #\Esc #\Esc))
74      (:queued "In queue")
75      (:no-candle-file "No candle file present")
76      (:in-progress "In progress"))
77     (format nil "~{~A/~A/~A ~A:~A~}"
78      (utils:time-as-list (third branch-info) :month :date :year :hr :min))))
79   (sort (communication:query `(candle:project-branch-information ,name)) #'< :key #'third)))
80
81 (defun project-history (name)
82  (mapcar
83   (lambda (job-info)
84    (format t "~A: ~A at ~A~%"
85     (first job-info)
86     (case (second job-info)
87      (:succeeded (format nil "~c[1;32mPassed~c[0m" #\Esc #\Esc))
88      (:failed (format nil "~c[1;31mFailed~c[0m" #\Esc #\Esc))
89      (:queued "In queue")
90      (:no-candle-file "No candle file present")
91      (:in-progress "In progress"))
92     (format nil "~{~A/~A/~A ~A:~A~}"
93      (utils:time-as-list (third job-info) :month :date :year :hr :min))))
94   (sort (communication:query `(candle:project-job-information ,name)) #'< :key #'third)))
95
96 (defun refresh-project (name)
97  (communication:query `(candle:refresh-project ,name))
98  (format t "Refreshed project ~A~%" name))
99
100 (defmethod execute-command ((command (eql :project)) args)
101  (multiple-value-bind (options remaining-args error) (opera:process-arguments (project-options) args)
102   (cond
103    ((eql error :unknown-option) (format *error-output* "Unknown option: ~A.  See 'candle project --help'.~%" (car remaining-args)))
104    ((eql error :required-argument-missing) (format *error-output* "Missing argument for ~A.  See 'candle project --help'.~%" (car remaining-args)))
105    ((opera:option-present :help options) (format t "~A" (opera:usage "candle project" (project-options))))
106    ((opera:option-present :delete options) (delete-project (opera:option-argument :delete options)))
107    ((opera:option-present :show options) (show-project (opera:option-argument :show options)))
108    ((opera:option-present :refresh options) (refresh-project (opera:option-argument :refresh options)))
109    ((opera:option-present :history options) (project-history (opera:option-argument :history options)))
110    ((opera:option-present :add options) (add-project (opera:option-argument :add options))))))
111
112 (defmethod execute-command ((command (eql :job)) args)
113  (multiple-value-bind (options remaining-args error) (opera:process-arguments (job-options) args)
114   (cond
115    ((eql error :unknown-option) (format *error-output* "Unknown option: ~A.  See 'candle job --help'.~%" (car remaining-args)))
116    ((eql error :required-argument-missing) (format *error-output* "Missing argument for ~A.  See 'candle job --help'.~%" (car remaining-args)))
117    ((opera:option-present :help options) (format t "~A" (opera:usage "candle job" (job-options)))))))
118
119 (defmethod execute-command ((command (eql :run)) args)
120  (multiple-value-bind (options remaining-args error) (opera:process-arguments (run-options) args)
121   (cond
122    ((eql error :unknown-option) (format *error-output* "Unknown option: ~A.  See 'candle run --help'.~%" (car remaining-args)))
123    ((eql error :required-argument-missing) (format *error-output* "Missing argument for ~A.  See 'candle run --help'.~%" (car remaining-args)))
124    (remaining-args (format *error-output* "Unknown option: ~A. See 'candle run --help'.~%" (car remaining-args)))
125    ((opera:option-present :help options) (format t "~A" (opera:usage "candle run" (run-options))))
126    ((not (candle:run)) (sb-ext:exit :code 1)))))
127
128 (defun run ()
129  (multiple-value-bind (options remaining-args error) (opera:process-arguments (main-options) (cdr sb-ext:*posix-argv*))
130   (cond
131    ((opera:option-present :help options) (main-usage))
132    ((eql error :unknown-option) (format *error-output* "Unknown option: ~A.  See 'candle --help'.~%" (car remaining-args)))
133    ((and (opera:option-present :port options) (not (parse-integer (opera:option-argument :port options) :junk-allowed t)))
134     (format *error-output* "--port requires a number.  See 'candle-server -h'~%"))
135    ((not remaining-args) (format *error-output* "Command required.  See 'candle --help'.~%"))
136    (t
137     (let
138      ((communication:*query-port*
139        (or
140         (and
141          (opera:option-present :port options)
142          (parse-integer (opera:option-argument :port options) :junk-allowed t))
143         25004)))
144      (execute-command (intern (string-upcase (car remaining-args)) :keyword) (cdr remaining-args)))))))