1 (in-package #:candle-cli)
3 (defgeneric execute-command (command args))
5 (defun error-and-exit (str &rest args)
6 (apply #'format *error-output* str args)
9 (defmethod execute-command (command args)
10 (error-and-exit "Unknown command '~(~A~)'. See 'candle --help'.~%" command))
12 (defun job-info->line (job-info)
13 (format nil "~A:~A (~A) ~A"
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))
22 (:no-candle-file "No candle file present")
23 (:in-progress "In progress"))))
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)
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))
39 ;;; Section for ./candle
42 (standard-cli "candle" (main-options) (cdr sb-ext:*posix-argv*) (main-usage) "Command"
46 (opera:option-present :port parsed-options)
47 (not (parse-integer (opera:option-argument :port parsed-options) :junk-allowed t)))
48 (error-and-exit "--port requires a number. See 'candle -h'~%")
50 ((communication:*query-port*
53 (opera:option-present :port parsed-options)
54 (parse-integer (opera:option-argument :port parsed-options) :junk-allowed t))
56 (execute-command (intern (string-upcase (car remaining-args)) :keyword) (cdr remaining-args))))
57 (candle:candle-error (e)
58 (case (candle:candle-error-reason e)
59 (:project-does-not-exist (error-and-exit "Project does not exist~%"))
60 (:job-does-not-exist (error-and-exit "Job does not exist~%"))
61 (:invalid-project-name (error-and-exit "Project name invalid~%"))
62 (:invalid-project-uri (error-and-exit "Project uri invalid~%"))
63 (:project-name-taken (error-and-exit "Project name already taken~%"))
64 (:project-failed-to-get-branches (error-and-exit "Unable to retrieve branches from server~%"))
65 (t (error-and-exit "Unknown error occurred: ~(~S~)~%" (candle:candle-error-reason e))))))))
67 (defun main-options ()
68 '((:name :help :short "h" :long "help" :description "Print this usage.")
69 (:name :port :short "p" :long "port" :takes-argument t :variable-name "PORT"
70 :description "Port on which to listen for commands. Defaults to 25004")
71 (:positional "<command>" :required t :description "Command for candle, see below")))
77 "Interacts with candle server. The available commands are:
78 project Interact with projects
79 job Get information about jobs
80 run Local command. Run candle in the current working directory"))
82 ;;; Section for ./candle project
84 (defmethod execute-command ((command (eql :project)) args)
85 (standard-cli "candle project" (project-options) args (project-usage) nil
87 ((subcommand (intern (string-upcase (car remaining-args)) :keyword)))
89 (:delete (delete-project (cdr remaining-args)))
90 (:add (add-project (cdr remaining-args)))
91 (:show (show-project (cdr remaining-args)))
92 (:refresh (refresh-project (cdr remaining-args)))
93 (:list (list-projects))
94 (:failures (project-failures (cdr remaining-args)))
95 (t (format t "~A" (project-usage)))))))
97 (defun project-usage ()
101 "Interacts with projects. The available project subcommands are:
102 list List all projects
103 add <name>:<src> Add a project
104 delete <name> Delete a project
105 show <name> Show project branch information
106 refresh <name> Tell the candle server to refresh the project information"))
108 (defun project-options ()
109 '((:name :help :short "h" :long "help" :description "Print this usage.")
110 (:positional "<subcommand>" :description "Project subcommand, see below.")))
112 (defun add-project (args)
115 `((:name :help :short "h" :long "help" :description "Print this usage.")
116 (:positional "<name>:<src>"
118 ,(format nil "~{~A~}"
120 "<name> is the name of the project, which must be alphanumeric (hyphens are allowed), while <src> is the "
121 "location of the repository for cloning. This location must be accessible by the machine running candle."
123 (standard-cli "candle project add" options args :default "<name>:<src>"
125 ((project-definition (car remaining-args))
126 (pos (position #\: project-definition)))
129 (error-and-exit "Project definition ~A is not valid. See 'candle project add --help'.~%" project-definition))
132 ((name (subseq project-definition 0 pos))
133 (src (subseq project-definition (1+ pos))))
134 (communication:query `(candle:add-project ,name ,src))
135 (format t "Added project ~A at src definition ~A~%" name src))))))))
137 (defun delete-project (args)
140 '((:name :help :short "h" :long "help" :description "Print this usage.")
141 (:positional "<name>" :description "<name> is the name of the project to delete"))))
142 (standard-cli "candle project delete" options args :default "<name>"
143 (communication:query `(candle:delete-project ,(car remaining-args)))
144 (format t "Removed project ~A~%" (car remaining-args)))))
146 (defun show-project (args)
149 '((:name :help :short "h" :long "help" :description "Print this usage.")
150 (:positional "<name>" :description "<name> is the name of the project to show"))))
151 (standard-cli "candle project show" options args :default "<name>"
153 ((branch-infos (communication:query `(candle:project-branch-information ,(car remaining-args))))
154 (width (apply #'max (mapcar #'length (mapcar #'car branch-infos)))))
156 (lambda (branch-info)
157 (format t (format nil "~~~A@A: ~~A~~%" width)
159 (job-info->line (second branch-info))))
160 (sort branch-infos #'< :key (lambda (branch-info) (fourth (second branch-info)))))))))
162 (defun refresh-project (args)
165 '((:name :help :short "h" :long "help" :description "Print this usage.")
166 (:positional "<name>" :description "<name> is the name of the project to refresh"))))
167 (standard-cli "candle project refresh" options args :default "<name>"
168 (communication:query `(candle:refresh-project ,(car remaining-args)))
169 (format t "Refreshed project ~A~%" (car remaining-args)))))
171 (defun list-projects ()
175 (format nil "~A ~A~A"
178 (if (zerop (caddr info)) "" (format nil " (~A branches ~c[1;31mfailing~c[0m)" (caddr info) #\Esc #\Esc))))
179 (communication:query `(candle:list-projects)))))
181 (defun project-failures (args)
184 '((:name :help :short "h" :long "help" :description "Print this usage.")
185 (:name :project :long "project" :variable-name "PROJECT" :takes-argument t
186 :description "Restrict failures to project named by PROJECT"))))
187 (standard-cli "candle project failures" options args :default nil
191 ,(when (opera:option-present :project parsed-options)
192 (opera:option-argument :project parsed-options))))))))
194 ;;; Section for ./candle job
196 (defmethod execute-command ((command (eql :job)) args)
197 (standard-cli "candle job" (job-options) args (job-usage) nil
199 ((subcommand (intern (string-upcase (car remaining-args)) :keyword)))
201 (:list (job-list (cdr remaining-args)))
202 (:log (job-log (cdr remaining-args)))
203 (:retry (retry-job (cdr remaining-args)))
204 (t (format t "~A" (job-usage)))))))
206 (defun job-options ()
207 '((:name :help :short "h" :long "help" :description "Print this usage.")
208 (:positional "<subcommand>" :description "Job subcommand, see below.")))
214 "Interacts with projects. The available project subcommands are:
216 log <project>:<sha> View the log for a job
217 retry <project>:<sha> Retry a job"))
219 (defun job-list (args)
222 '((:name :help :short "h" :long "help" :description "Print this usage.")
223 (:name :project :long "project" :variable-name "PROJECT" :takes-argument t
224 :description "Restrict jobs to project named by PROJECT"))))
225 (standard-cli "candle job list" options args :default nil
230 (communication:query `(candle:project-job-information ,(opera:option-argument :project parsed-options)))
231 #'< :key #'fourth))))))
233 (defun decompose-job-definition (job-definition)
235 ((pos (position #\: job-definition)))
239 (subseq job-definition 0 pos)
240 (subseq job-definition (1+ pos))))))
242 (defun job-log (args)
245 '((:name :help :short "h" :long "help" :description "Print this usage.")
246 (:positional "<project>:<sha>"
247 :description "<project> is the name of the project, while <sha> is the sha of the job in question."))))
248 (standard-cli "candle job log" options args :default "<project>:<sha>"
249 (multiple-value-bind (project-name sha) (decompose-job-definition (car remaining-args))
251 (format t "~A" (communication:query `(candle:get-job-log ,project-name ,sha)))
252 (error-and-exit "Job definition ~A is not valid. See 'candle job log --help'.~%" (car remaining-args)))))))
254 (defun retry-job (args)
257 '((:name :help :short "h" :long "help" :description "Print this usage.")
258 (:positional "<project>:<sha>"
259 :description "<project> is the name of the project, while <sha> is the sha of the job in question."))))
260 (standard-cli "candle job retry" options args :default "<project>:<sha>"
261 (multiple-value-bind (project-name sha) (decompose-job-definition (car remaining-args))
264 (communication:query `(candle:retry-job ,project-name ,sha))
265 (format t "Retrying job ~A~%" (car remaining-args)))
266 (error-and-exit "Job definition ~A is not valid. See 'candle job log --help'.~%" (car remaining-args)))))))
268 ;;; Section for ./candle run
270 (defmethod execute-command ((command (eql :run)) args)
273 '((:name :help :short "h" :long "help" :description "Print this usage.")
274 (:name :task :long "task" :variable-name "TASK" :takes-argument t :description "Run TASK")
275 (:name :env :long "env" :variable-name "ENV" :takes-argument t
276 :description "Runs candle with *candle-environment* set to ENV as a keyword."))))
277 (standard-cli "candle run" options args :default nil
278 (when (opera:option-present :env parsed-options)
279 (setf candle:*environment* (intern (string-upcase (opera:option-argument :env parsed-options)) :keyword)))
281 ((opera:option-present :task parsed-options)
283 ((tasks (candle:list-tasks))
284 (specified-task (intern (string-upcase (opera:option-argument :task parsed-options)) :keyword)))
286 (not (find specified-task (candle:list-tasks)))
287 (error-and-exit "Task ~(~A~) does not exist in .candle file" specified-task)
288 (candle:run specified-task))))
289 (t (when (not (candle:run)) (sb-ext:exit :code 1)))))))