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"
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'~%")
48 ((communication:*query-port*
51 (opera:option-present :port parsed-options)
52 (parse-integer (opera:option-argument :port parsed-options) :junk-allowed t))
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))))))))
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")))
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"))
80 ;;; Section for ./candle project
82 (defmethod execute-command ((command (eql :project)) args)
83 (standard-cli "candle project" (project-options) args (project-usage) nil
85 ((subcommand (intern (string-upcase (car remaining-args)) :keyword)))
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)))))))
95 (defun project-usage ()
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"))
106 (defun project-options ()
107 '((:name :help :short "h" :long "help" :description "Print this usage.")
108 (:positional "<subcommand>" :description "Project subcommand, see below.")))
110 (defun add-project (args)
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>"
117 ((project-definition (car remaining-args))
118 (pos (position #\: project-definition)))
120 ((not pos) (error-and-exit "Project definition ~A is not valid. See 'candle project add --help'.~%" project-definition))
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))))))))
128 (defun delete-project (args)
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)))))
137 (defun show-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 show"))))
142 (standard-cli "candle project show" options args :default "<name>"
144 ((branch-infos (communication:query `(candle:project-branch-information ,(car remaining-args))))
145 (width (apply #'max (mapcar #'length (mapcar #'car branch-infos)))))
147 (lambda (branch-info)
148 (format t (format nil "~~~A@A: ~~A~~%" width)
150 (job-info->line (second branch-info))))
151 (sort branch-infos #'< :key (lambda (branch-info) (fourth (second branch-info)))))))))
153 (defun refresh-project (args)
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)))))
162 (defun list-projects ()
166 (format nil "~A ~A~A"
169 (if (zerop (caddr info)) "" (format nil " (~A branches ~c[1;31mfailing~c[0m)" (caddr info) #\Esc #\Esc))))
170 (communication:query `(candle:list-projects)))))
172 (defun project-failures (args)
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
180 `(candle:failures ,(when (opera:option-present :project parsed-options) (opera:option-argument :project parsed-options))))))))
182 ;;; Section for ./candle job
184 (defmethod execute-command ((command (eql :job)) args)
185 (standard-cli "candle job" (job-options) args (job-usage) nil
187 ((subcommand (intern (string-upcase (car remaining-args)) :keyword)))
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)))))))
194 (defun job-options ()
195 '((:name :help :short "h" :long "help" :description "Print this usage.")
196 (:positional "<subcommand>" :description "Job subcommand, see below.")))
202 "Interacts with projects. The available project subcommands are:
204 log <project>:<sha> View the log for a job
205 retry <project>:<sha> Retry a job"))
207 (defun job-list (args)
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
216 (sort (communication:query `(candle:project-job-information ,(opera:option-argument :project parsed-options))) #'< :key #'fourth))))))
218 (defun decompose-job-definition (job-definition)
220 ((pos (position #\: job-definition)))
224 (subseq job-definition 0 pos)
225 (subseq job-definition (1+ pos))))))
227 (defun job-log (args)
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))
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)))))))
238 (defun retry-job (args)
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))
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)))))))
251 ;;; Section for ./candle run
253 (defmethod execute-command ((command (eql :run)) args)
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)))))