1 ; Copyright 2022 Frank Duncan (frank@consxy.com) under AGPL3. See distributed LICENSE.txt.
2 (in-package #:candle-cli)
4 (defgeneric execute-command (command args))
6 (defun error-and-exit (str &rest args)
7 (apply #'format *error-output* str args)
10 (defmethod execute-command (command args)
11 (error-and-exit "Unknown command '~(~A~)'. See 'candle --help'.~%" command))
13 (defun job-info->line (job-info)
14 (format nil "~A:~A (~A) ~A"
16 (subseq (second job-info) 0 8)
17 (format nil "~{~2,,,'0@A/~2,,,'0@A/~A ~2,,,'0@A:~2,,,'0@A~}"
18 (utils:time-as-list (fourth job-info) :month :date :year :hr :min))
19 (case (third job-info)
20 (:succeeded (format nil "~c[1;32mPassed~c[0m" #\Esc #\Esc))
21 (:failed (format nil "~c[1;31mFailed~c[0m" #\Esc #\Esc))
23 (:no-candle-file "No candle file present")
24 (:in-progress "In progress"))))
26 (defmacro standard-cli (cmd options-in args usage remaining-args-required &rest success)
27 `(multiple-value-bind (parsed-options remaining-args error) (opera:process-arguments ,options-in ,args)
29 ((opera:option-present :help parsed-options)
30 (format t "~A" ,(if (eql usage :default) `(opera:usage ,cmd ,options-in) usage)))
31 ((eql error :unknown-option)
32 (error-and-exit "Unknown option: ~A. See '~A --help'.~%" (car remaining-args) ,cmd))
33 ((eql error :required-argument-missing)
34 (error-and-exit "Missing argument for ~A. See '~A --help'.~%" (car remaining-args) ,cmd))
35 ((and ,remaining-args-required (not remaining-args))
36 (error-and-exit "~A required. See 'candle --help'.~%" ,remaining-args-required))
40 ;;; Section for ./candle
43 (standard-cli "candle" (main-options) (cdr sb-ext:*posix-argv*) (main-usage) "Command"
47 (opera:option-present :port parsed-options)
48 (not (parse-integer (opera:option-argument :port parsed-options) :junk-allowed t)))
49 (error-and-exit "--port requires a number. See 'candle -h'~%")
51 ((communication:*query-port*
54 (opera:option-present :port parsed-options)
55 (parse-integer (opera:option-argument :port parsed-options) :junk-allowed t))
57 (execute-command (intern (string-upcase (car remaining-args)) :keyword) (cdr remaining-args))))
58 (candle:candle-error (e)
59 (case (candle:candle-error-reason e)
60 (:project-does-not-exist (error-and-exit "Project does not exist~%"))
61 (:job-does-not-exist (error-and-exit "Job does not exist~%"))
62 (:invalid-project-name (error-and-exit "Project name invalid~%"))
63 (:invalid-project-uri (error-and-exit "Project uri invalid~%"))
64 (:project-name-taken (error-and-exit "Project name already taken~%"))
65 (:project-failed-to-get-branches (error-and-exit "Unable to retrieve branches from server~%"))
66 (t (error-and-exit "Unknown error occurred: ~(~S~)~%" (candle:candle-error-reason e))))))))
68 (defun main-options ()
69 '((:name :help :short "h" :long "help" :description "Print this usage.")
70 (:name :port :short "p" :long "port" :takes-argument t :variable-name "PORT"
71 :description "Port on which to listen for commands. Defaults to 25004")
72 (:positional "<command>" :required t :description "Command for candle, see below")))
78 "Interacts with candle server. The available commands are:
79 project Interact with projects
80 job Get information about jobs
81 run Local command. Run candle in the current working directory"))
83 ;;; Section for ./candle project
85 (defmethod execute-command ((command (eql :project)) args)
86 (standard-cli "candle project" (project-options) args (project-usage) nil
88 ((subcommand (intern (string-upcase (car remaining-args)) :keyword)))
90 (:delete (delete-project (cdr remaining-args)))
91 (:add (add-project (cdr remaining-args)))
92 (:show (show-project (cdr remaining-args)))
93 (:refresh (refresh-project (cdr remaining-args)))
94 (:list (list-projects))
95 (:failures (project-failures (cdr remaining-args)))
96 (t (format t "~A" (project-usage)))))))
98 (defun project-usage ()
102 "Interacts with projects. The available project subcommands are:
103 list List all projects
104 add <name>:<src> Add a project
105 delete <name> Delete a project
106 show <name> Show project branch information
107 refresh <name> Tell the candle server to refresh the project information"))
109 (defun project-options ()
110 '((:name :help :short "h" :long "help" :description "Print this usage.")
111 (:positional "<subcommand>" :description "Project subcommand, see below.")))
113 (defun add-project (args)
116 `((:name :help :short "h" :long "help" :description "Print this usage.")
117 (:positional "<name>:<src>"
119 ,(format nil "~{~A~}"
121 "<name> is the name of the project, which must be alphanumeric (hyphens are allowed), while <src> is the "
122 "location of the repository for cloning. This location must be accessible by the machine running candle."
124 (standard-cli "candle project add" options args :default "<name>:<src>"
126 ((project-definition (car remaining-args))
127 (pos (position #\: project-definition)))
130 (error-and-exit "Project definition ~A is not valid. See 'candle project add --help'.~%" project-definition))
133 ((name (subseq project-definition 0 pos))
134 (src (subseq project-definition (1+ pos))))
135 (communication:query `(candle:add-project ,name ,src))
136 (format t "Added project ~A at src definition ~A~%" name src))))))))
138 (defun delete-project (args)
141 '((:name :help :short "h" :long "help" :description "Print this usage.")
142 (:positional "<name>" :description "<name> is the name of the project to delete"))))
143 (standard-cli "candle project delete" options args :default "<name>"
144 (communication:query `(candle:delete-project ,(car remaining-args)))
145 (format t "Removed project ~A~%" (car remaining-args)))))
147 (defun show-project (args)
150 '((:name :help :short "h" :long "help" :description "Print this usage.")
151 (:positional "<name>" :description "<name> is the name of the project to show"))))
152 (standard-cli "candle project show" options args :default "<name>"
154 ((branch-infos (communication:query `(candle:project-branch-information ,(car remaining-args))))
155 (width (apply #'max (mapcar #'length (mapcar #'car branch-infos)))))
157 (lambda (branch-info)
158 (format t (format nil "~~~A@A: ~~A~~%" width)
160 (job-info->line (second branch-info))))
161 (sort branch-infos #'< :key (lambda (branch-info) (fourth (second branch-info)))))))))
163 (defun refresh-project (args)
166 '((:name :help :short "h" :long "help" :description "Print this usage.")
167 (:positional "<name>" :description "<name> is the name of the project to refresh"))))
168 (standard-cli "candle project refresh" options args :default "<name>"
169 (communication:query `(candle:refresh-project ,(car remaining-args)))
170 (format t "Refreshed project ~A~%" (car remaining-args)))))
172 (defun list-projects ()
176 (format nil "~A ~A~A"
179 (if (zerop (caddr info)) "" (format nil " (~A branches ~c[1;31mfailing~c[0m)" (caddr info) #\Esc #\Esc))))
180 (communication:query `(candle:list-projects)))))
182 (defun project-failures (args)
185 '((:name :help :short "h" :long "help" :description "Print this usage.")
186 (:name :project :long "project" :variable-name "PROJECT" :takes-argument t
187 :description "Restrict failures to project named by PROJECT"))))
188 (standard-cli "candle project failures" options args :default nil
192 ,(when (opera:option-present :project parsed-options)
193 (opera:option-argument :project parsed-options))))))))
195 ;;; Section for ./candle job
197 (defmethod execute-command ((command (eql :job)) args)
198 (standard-cli "candle job" (job-options) args (job-usage) nil
200 ((subcommand (intern (string-upcase (car remaining-args)) :keyword)))
202 (:list (job-list (cdr remaining-args)))
203 (:log (job-log (cdr remaining-args)))
204 (:retry (retry-job (cdr remaining-args)))
205 (t (format t "~A" (job-usage)))))))
207 (defun job-options ()
208 '((:name :help :short "h" :long "help" :description "Print this usage.")
209 (:positional "<subcommand>" :description "Job subcommand, see below.")))
215 "Interacts with projects. The available project subcommands are:
217 log <project>:<sha> View the log for a job
218 retry <project>:<sha> Retry a job"))
220 (defun job-list (args)
223 '((:name :help :short "h" :long "help" :description "Print this usage.")
224 (:name :project :long "project" :variable-name "PROJECT" :takes-argument t
225 :description "Restrict jobs to project named by PROJECT"))))
226 (standard-cli "candle job list" options args :default nil
231 (communication:query `(candle:project-job-information ,(opera:option-argument :project parsed-options)))
232 #'< :key #'fourth))))))
234 (defun decompose-job-definition (job-definition)
236 ((pos (position #\: job-definition)))
240 (subseq job-definition 0 pos)
241 (subseq job-definition (1+ pos))))))
243 (defun job-log (args)
246 '((:name :help :short "h" :long "help" :description "Print this usage.")
247 (:positional "<project>:<sha>"
248 :description "<project> is the name of the project, while <sha> is the sha of the job in question."))))
249 (standard-cli "candle job log" options args :default "<project>:<sha>"
250 (multiple-value-bind (project-name sha) (decompose-job-definition (car remaining-args))
252 (format t "~A" (communication:query `(candle:get-job-log ,project-name ,sha)))
253 (error-and-exit "Job definition ~A is not valid. See 'candle job log --help'.~%" (car remaining-args)))))))
255 (defun retry-job (args)
258 '((:name :help :short "h" :long "help" :description "Print this usage.")
259 (:positional "<project>:<sha>"
260 :description "<project> is the name of the project, while <sha> is the sha of the job in question."))))
261 (standard-cli "candle job retry" options args :default "<project>:<sha>"
262 (multiple-value-bind (project-name sha) (decompose-job-definition (car remaining-args))
265 (communication:query `(candle:retry-job ,project-name ,sha))
266 (format t "Retrying job ~A~%" (car remaining-args)))
267 (error-and-exit "Job definition ~A is not valid. See 'candle job log --help'.~%" (car remaining-args)))))))
269 ;;; Section for ./candle run
271 (defmethod execute-command ((command (eql :run)) args)
274 '((:name :help :short "h" :long "help" :description "Print this usage.")
275 (:name :task :long "task" :variable-name "TASK" :takes-argument t :description "Run TASK")
276 (:name :env :long "env" :variable-name "ENV" :takes-argument t
277 :description "Runs candle with *candle-environment* set to ENV as a keyword."))))
278 (standard-cli "candle run" options args :default nil
279 (when (opera:option-present :env parsed-options)
280 (setf candle:*environment* (intern (string-upcase (opera:option-argument :env parsed-options)) :keyword)))
282 ((opera:option-present :task parsed-options)
284 ((tasks (candle:list-tasks))
285 (specified-task (intern (string-upcase (opera:option-argument :task parsed-options)) :keyword)))
287 (not (find specified-task (candle:list-tasks)))
288 (error-and-exit "Task ~(~A~) does not exist in .candle file" specified-task)
289 (candle:run specified-task))))
290 (t (when (not (candle:run)) (sb-ext:exit :code 1)))))))