bacd1e3d62c7d15a6ede410728b02b0e84fb24a9
[candle] / src / main / cli.lisp
1 (in-package #:candle-cli)
2
3 (defgeneric execute-command (command args))
4
5 (defun error-and-exit (str &rest args)
6  (apply #'format *error-output* str args)
7  (sb-ext:exit :code 1))
8
9 (defmethod execute-command (command args)
10  (error-and-exit "Unknown command '~(~A~)'.  See 'candle --help'.~%" command))
11
12 (defun job-info->line (job-info)
13  (format nil "~A:~A (~A) ~A"
14   (first job-info)
15   (subseq (second job-info) 0 8)
16   (format nil "~{~2,,,'0@A/~2,,,'0@A/~A ~2,,,'0@A:~2,,,'0@A~}"
17    (utils:time-as-list (fourth job-info) :month :date :year :hr :min))
18   (case (third job-info)
19    (:succeeded (format nil "~c[1;32mPassed~c[0m" #\Esc #\Esc))
20    (:failed (format nil "~c[1;31mFailed~c[0m" #\Esc #\Esc))
21    (:queued "In queue")
22    (:no-candle-file "No candle file present")
23    (:in-progress "In progress"))))
24
25 (defmacro standard-cli (cmd options-in args usage remaining-args-required &rest success)
26 `(multiple-value-bind (parsed-options remaining-args error) (opera:process-arguments ,options-in ,args)
27   (cond
28    ((opera:option-present :help parsed-options)
29     (format t "~A" ,(if (eql usage :default) `(opera:usage ,cmd ,options-in) usage)))
30    ((eql error :unknown-option)
31     (error-and-exit "Unknown option: ~A.  See '~A --help'.~%" (car remaining-args) ,cmd))
32    ((eql error :required-argument-missing)
33     (error-and-exit "Missing argument for ~A.  See '~A --help'.~%" (car remaining-args) ,cmd))
34    ((and ,remaining-args-required (not remaining-args))
35     (error-and-exit "~A required.  See 'candle --help'.~%" ,remaining-args-required))
36    (t
37     ,@success))))
38
39 ;;; Section for ./candle
40
41 (defun run ()
42  (standard-cli "candle" (main-options) (cdr sb-ext:*posix-argv*) (main-usage) "Command"
43   (handler-case
44    (if
45     (and (opera:option-present :port parsed-options) (not (parse-integer (opera:option-argument :port parsed-options) :junk-allowed t)))
46     (error-and-exit "--port requires a number.  See 'candle -h'~%")
47     (let
48      ((communication:*query-port*
49        (or
50         (and
51          (opera:option-present :port parsed-options)
52          (parse-integer (opera:option-argument :port parsed-options) :junk-allowed t))
53         25004)))
54      (execute-command (intern (string-upcase (car remaining-args)) :keyword) (cdr remaining-args))))
55    (candle:candle-error (e)
56     (case (candle:candle-error-reason e)
57      (:project-does-not-exist (error-and-exit "Project does not exist~%"))
58      (:job-does-not-exist (error-and-exit "Job does not exist~%"))
59      (:invalid-project-name (error-and-exit "Project name invalid~%"))
60      (:invalid-project-uri (error-and-exit "Project uri invalid~%"))
61      (:project-name-taken (error-and-exit "Project name already taken~%"))
62      (:project-failed-to-get-branches (error-and-exit "Unable to retrieve branches from server~%"))
63      (t (error-and-exit "Unknown error occurred: ~(~S~)~%" (candle:candle-error-reason e))))))))
64
65 (defun main-options ()
66  '((:name :help :short "h" :long "help" :description "Print this usage.")
67    (:name :port :short "p" :long "port" :takes-argument t :variable-name "PORT"
68     :description "Port on which to listen for commands.  Defaults to 25004")
69    (:positional "<command>" :required t :description "Command for candle, see below")))
70
71 (defun main-usage ()
72  (opera:usage
73   "candle"
74   (main-options)
75   "Interacts with candle server.  The available commands are:
76  project   Interact with projects
77  job       Get information about jobs
78  run       Local command.  Run candle in the current working directory"))
79
80 ;;; Section for ./candle project
81
82 (defmethod execute-command ((command (eql :project)) args)
83  (standard-cli "candle project" (project-options) args (project-usage) nil
84   (let
85    ((subcommand (intern (string-upcase (car remaining-args)) :keyword)))
86    (case subcommand
87     (:delete (delete-project (cdr remaining-args)))
88     (:add (add-project (cdr remaining-args)))
89     (:show (show-project (cdr remaining-args)))
90     (:refresh (refresh-project (cdr remaining-args)))
91     (:list (list-projects))
92     (:failures (project-failures (cdr remaining-args)))
93     (t (format t "~A" (project-usage)))))))
94
95 (defun project-usage ()
96  (opera:usage
97   "candle project"
98   (project-options)
99   "Interacts with projects.  The available project subcommands are:
100  list              List all projects
101  add <name>:<src>  Add a project
102  delete <name>     Delete a project
103  show <name>       Show project branch information
104  refresh <name>    Tell the candle server to refresh the project information"))
105
106 (defun project-options ()
107  '((:name :help :short "h" :long "help" :description "Print this usage.")
108    (:positional "<subcommand>" :description "Project subcommand, see below.")))
109
110 (defun add-project (args)
111  (let
112   ((options
113    '((:name :help :short "h" :long "help" :description "Print this usage.")
114      (:positional "<name>:<src>" :description "<name> is the name of the project, which must be alphanumeric (hyphens are allowed), while <src> is the location of the repository for cloning.  This location must be accessible by the machine running candle."))))
115   (standard-cli "candle project add" options args :default "<name>:<src>"
116    (let*
117     ((project-definition (car remaining-args))
118      (pos (position #\: project-definition)))
119     (cond
120      ((not pos) (error-and-exit "Project definition ~A is not valid.  See 'candle project add --help'.~%" project-definition))
121      (t
122       (let*
123        ((name (subseq project-definition 0 pos))
124         (src (subseq project-definition (1+ pos))))
125        (communication:query `(candle:add-project ,name ,src))
126        (format t "Added project ~A at src definition ~A~%" name src))))))))
127
128 (defun delete-project (args)
129  (let
130   ((options
131    '((:name :help :short "h" :long "help" :description "Print this usage.")
132      (:positional "<name>" :description "<name> is the name of the project to delete"))))
133   (standard-cli "candle project delete" options args :default "<name>"
134     (communication:query `(candle:delete-project ,(car remaining-args)))
135     (format t "Removed project ~A~%" (car remaining-args)))))
136
137 (defun show-project (args)
138  (let
139   ((options
140    '((:name :help :short "h" :long "help" :description "Print this usage.")
141      (:positional "<name>" :description "<name> is the name of the project to show"))))
142   (standard-cli "candle project show" options args :default "<name>"
143    (let*
144     ((branch-infos (communication:query `(candle:project-branch-information ,(car remaining-args))))
145      (width (apply #'max (mapcar #'length (mapcar #'car branch-infos)))))
146     (mapcar
147      (lambda (branch-info)
148       (format t (format nil "~~~A@A: ~~A~~%" width)
149        (first branch-info)
150        (job-info->line (second branch-info))))
151      (sort branch-infos #'< :key (lambda (branch-info) (fourth (second branch-info)))))))))
152
153 (defun refresh-project (args)
154  (let
155   ((options
156    '((:name :help :short "h" :long "help" :description "Print this usage.")
157      (:positional "<name>" :description "<name> is the name of the project to refresh"))))
158   (standard-cli "candle project refresh" options args :default "<name>"
159    (communication:query `(candle:refresh-project ,(car remaining-args)))
160    (format t "Refreshed project ~A~%" (car remaining-args)))))
161
162 (defun list-projects ()
163  (format t "~{~A~%~}"
164   (mapcar
165    (lambda (info)
166     (format nil "~A  ~A~A"
167      (car info)
168      (cadr info)
169      (if (zerop (caddr info)) "" (format nil " (~A branches ~c[1;31mfailing~c[0m)" (caddr info) #\Esc #\Esc))))
170    (communication:query `(candle:list-projects)))))
171
172 (defun project-failures (args)
173  (let
174   ((options
175    '((:name :help :short "h" :long "help" :description "Print this usage.")
176      (:name :project :long "project" :variable-name "PROJECT" :takes-argument t :description "Restrict failures to project named by PROJECT"))))
177   (standard-cli "candle project failures" options args :default nil
178    (format t "~A"
179     (communication:query
180      `(candle:failures ,(when (opera:option-present :project parsed-options) (opera:option-argument :project parsed-options))))))))
181
182 ;;; Section for ./candle job
183
184 (defmethod execute-command ((command (eql :job)) args)
185  (standard-cli "candle job" (job-options) args (job-usage) nil
186   (let
187    ((subcommand (intern (string-upcase (car remaining-args)) :keyword)))
188    (case subcommand
189     (:list (job-list (cdr remaining-args)))
190     (:log (job-log (cdr remaining-args)))
191     (:retry (retry-job (cdr remaining-args)))
192     (t (format t "~A" (job-usage)))))))
193
194 (defun job-options ()
195  '((:name :help :short "h" :long "help" :description "Print this usage.")
196    (:positional "<subcommand>" :description "Job subcommand, see below.")))
197
198 (defun job-usage ()
199  (opera:usage
200   "candle job"
201   (project-options)
202   "Interacts with projects.  The available project subcommands are:
203  list                   List jobs
204  log <project>:<sha>    View the log for a job
205  retry <project>:<sha>  Retry a job"))
206
207 (defun job-list (args)
208  (let
209   ((options
210    '((:name :help :short "h" :long "help" :description "Print this usage.")
211      (:name :project :long "project" :variable-name "PROJECT" :takes-argument t :description "Restrict jobs to project named by PROJECT"))))
212   (standard-cli "candle job list" options args :default nil
213    (format t "~{~A~%~}"
214     (mapcar
215      #'job-info->line
216      (sort (communication:query `(candle:project-job-information ,(opera:option-argument :project parsed-options))) #'< :key #'fourth))))))
217
218 (defun decompose-job-definition (job-definition)
219  (let
220   ((pos (position #\: job-definition)))
221   (when
222    pos
223    (values
224     (subseq job-definition 0 pos)
225     (subseq job-definition (1+ pos))))))
226
227 (defun job-log (args)
228  (let
229   ((options
230    '((:name :help :short "h" :long "help" :description "Print this usage.")
231      (:positional "<project>:<sha>" :description "<project> is the name of the project, while <sha> is the sha of the job in question."))))
232   (standard-cli "candle job log" options args :default "<project>:<sha>"
233    (multiple-value-bind (project-name sha) (decompose-job-definition (car remaining-args))
234     (if project-name
235      (format t "~A" (communication:query `(candle:get-job-log ,project-name ,sha)))
236      (error-and-exit "Job definition ~A is not valid.  See 'candle job log --help'.~%" (car remaining-args)))))))
237
238 (defun retry-job (args)
239  (let
240   ((options
241    '((:name :help :short "h" :long "help" :description "Print this usage.")
242      (:positional "<project>:<sha>" :description "<project> is the name of the project, while <sha> is the sha of the job in question."))))
243   (standard-cli "candle job retry" options args :default "<project>:<sha>"
244    (multiple-value-bind (project-name sha) (decompose-job-definition (car remaining-args))
245     (if project-name
246      (progn
247       (communication:query `(candle:retry-job ,project-name ,sha))
248       (format t "Retrying job ~A~%" (car remaining-args)))
249      (error-and-exit "Job definition ~A is not valid.  See 'candle job log --help'.~%" (car remaining-args)))))))
250
251 ;;; Section for ./candle run
252
253 (defmethod execute-command ((command (eql :run)) args)
254  (let
255   ((options '((:name :help :short "h" :long "help" :description "Print this usage."))))
256   (standard-cli "run" options args :default nil
257    (when (not (candle:run)) (sb-ext:exit :code 1)))))