1 (in-package #:candle-cli)
3 (defgeneric execute-command (command args))
5 (defmethod execute-command (command args)
6 (format *error-output* "Unknown command '~(~A~)'. See 'candle --help'.~%" command))
8 (defun job-info->line (job-info)
9 (format nil "~A (~A) ~A"
10 (subseq (first job-info) 0 8)
11 (format nil "~{~2,,,'0@A/~2,,,'0@A/~A ~2,,,'0@A:~2,,,'0@A~}"
12 (utils:time-as-list (third job-info) :month :date :year :hr :min))
13 (case (second job-info)
14 (:succeeded (format nil "~c[1;32mPassed~c[0m" #\Esc #\Esc))
15 (:failed (format nil "~c[1;31mFailed~c[0m" #\Esc #\Esc))
17 (:no-candle-file "No candle file present")
18 (:in-progress "In progress"))))
20 ;;; Section for ./candle
23 (multiple-value-bind (options remaining-args error) (opera:process-arguments (main-options) (cdr sb-ext:*posix-argv*))
25 ((opera:option-present :help options) (main-usage))
26 ((eql error :unknown-option) (format *error-output* "Unknown option: ~A. See 'candle --help'.~%" (car remaining-args)))
27 ((and (opera:option-present :port options) (not (parse-integer (opera:option-argument :port options) :junk-allowed t)))
28 (format *error-output* "--port requires a number. See 'candle-server -h'~%"))
29 ((not remaining-args) (format *error-output* "Command required. See 'candle --help'.~%"))
32 ((communication:*query-port*
35 (opera:option-present :port options)
36 (parse-integer (opera:option-argument :port options) :junk-allowed t))
38 (execute-command (intern (string-upcase (car remaining-args)) :keyword) (cdr remaining-args)))))))
40 (defun main-options ()
41 '((:name :help :short "h" :long "help" :description "Print this usage.")
42 (:name :port :short "p" :long "port" :takes-argument t :variable-name "PORT"
43 :description "Port on which to listen for commands. Defaults to 25004")
44 (:positional "<command>" :required t :description "Command for candle, see below")))
51 "Interacts with candle server. The available commands are:
52 project Interact with projects
53 job Get information about jobs
54 run Local command. Run candle in the current working directory")))
56 ;;; Section for ./candle project
58 (defmethod execute-command ((command (eql :project)) args)
59 (multiple-value-bind (options remaining-args error) (opera:process-arguments (project-options) args)
61 ((eql error :unknown-option)
62 (format *error-output* "Unknown option: ~A. See 'candle project --help'.~%" (car remaining-args)))
63 ((eql error :required-argument-missing)
64 (format *error-output* "Missing argument for ~A. See 'candle project --help'.~%" (car remaining-args)))
65 ((opera:option-present :help options) (project-usage))
68 ((subcommand (intern (string-upcase (car remaining-args)) :keyword)))
70 (:delete (delete-project (cdr remaining-args)))
71 (:add (add-project (cdr remaining-args)))
72 (:show (show-project (cdr remaining-args)))
73 (:refresh (refresh-project (cdr remaining-args)))
74 (:list (list-projects))
75 (:failures (project-failures (cdr remaining-args)))
76 (t (project-usage))))))))
78 (defun project-usage ()
83 "Interacts with projects. The available project subcommands are:
84 list List all projects
85 add <name>:<src> Add a project
86 delete <name> Delete a project
87 show <name> Show project branch information
88 refresh <name> Tell the candle server to refresh the project information")))
90 (defun project-options ()
91 '((:name :help :short "h" :long "help" :description "Print this usage.")
92 (:positional "<subcommand>" :description "Project subcommand, see below.")))
94 (defun add-project (args)
97 '((:name :help :short "h" :long "help" :description "Print this usage.")
98 (: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.")))
99 (usage (opera:usage "candle project add" options)))
100 (multiple-value-bind (options remaining-args error) (opera:process-arguments options args)
102 ((eql error :unknown-option) (format *error-output* "Unknown option: ~A. See 'candle project add --help'.~%" (car remaining-args)))
103 ((eql error :required-argument-missing) (format *error-output* "Missing argument for ~A. See 'candle project add --help'.~%" (car remaining-args)))
104 ((opera:option-present :help options) (format t "~A" usage))
105 ((not remaining-args) (format *error-output* "Required <name>:<src>. See 'candle project add --help'.~%"))
108 ((project-definition (car remaining-args))
109 (pos (position #\: project-definition)))
111 ((not pos) (format *error-output* "Project definition ~A is not valid. See 'candle project --help'.~%" project-definition))
114 ((name (subseq project-definition 0 pos))
115 (src (subseq project-definition (1+ pos))))
116 (communication:query `(candle:add-project ,name ,src))
117 (format t "Added project ~A at src definition ~A~%" name src))))))))))
119 (defun delete-project (args)
122 '((:name :help :short "h" :long "help" :description "Print this usage.")
123 (:positional "<name>" :description "<name> is the name of the project to delete")))
124 (usage (opera:usage "candle project delete" options)))
125 (multiple-value-bind (options remaining-args error) (opera:process-arguments options args)
127 ((eql error :unknown-option) (format *error-output* "Unknown option: ~A. See 'candle project delete --help'.~%" (car remaining-args)))
128 ((eql error :required-argument-missing) (format *error-output* "Missing argument for ~A. See 'candle project delete --help'.~%" (car remaining-args)))
129 ((opera:option-present :help options) (format t "~A" usage))
130 ((not remaining-args) (format *error-output* "Required <name>. See 'candle project delete --help'.~%"))
132 (communication:query `(candle:delete-project ,(car remaining-args)))
133 (format t "Removed project ~A~%" (car remaining-args)))))))
135 (defun show-project (args)
138 '((:name :help :short "h" :long "help" :description "Print this usage.")
139 (:positional "<name>" :description "<name> is the name of the project to show")))
140 (usage (opera:usage "candle project delete" options)))
141 (multiple-value-bind (options remaining-args error) (opera:process-arguments options args)
143 ((eql error :unknown-option) (format *error-output* "Unknown option: ~A. See 'candle project show --help'.~%" (car remaining-args)))
144 ((eql error :required-argument-missing) (format *error-output* "Missing argument for ~A. See 'candle project show --help'.~%" (car remaining-args)))
145 ((opera:option-present :help options) (format t "~A" usage))
146 ((not remaining-args) (format *error-output* "Required <name>. See 'candle project show --help'.~%"))
149 ((branch-infos (communication:query `(candle:project-branch-information ,(car remaining-args))))
150 (width (apply #'max (mapcar #'length (mapcar #'car branch-infos)))))
152 (lambda (branch-info)
153 (format t (format nil "~~~A@A: ~~A~~%" width)
155 (job-info->line (second branch-info))))
156 (sort branch-infos #'< :key (lambda (branch-info) (third (second branch-info)))))))))))
158 (defun refresh-project (args)
161 '((:name :help :short "h" :long "help" :description "Print this usage.")
162 (:positional "<name>" :description "<name> is the name of the project to refresh")))
163 (usage (opera:usage "candle project refresh" options)))
164 (multiple-value-bind (options remaining-args error) (opera:process-arguments options args)
166 ((eql error :unknown-option) (format *error-output* "Unknown option: ~A. See 'candle project refresh --help'.~%" (car remaining-args)))
167 ((eql error :required-argument-missing) (format *error-output* "Missing argument for ~A. See 'candle project refresh --help'.~%" (car remaining-args)))
168 ((opera:option-present :help options) (format t "~A" usage))
169 ((not remaining-args) (format *error-output* "Required <name>. See 'candle project refresh --help'.~%"))
171 (communication:query `(candle:refresh-project ,(car remaining-args)))
172 (format t "Refreshed project ~A~%" (car remaining-args)))))))
174 (defun list-projects ()
175 (format t "~{~{~A ~A~}~%~}" (communication:query `(candle:list-projects))))
177 (defun project-failures (args)
180 '((:name :help :short "h" :long "help" :description "Print this usage.")
181 (:name :project :long "project" :variable-name "PROJECT" :takes-argument t :description "Restrict failures to project named by PROJECT")))
182 (usage (opera:usage "candle project failures" options)))
183 (multiple-value-bind (options remaining-args error) (opera:process-arguments options args)
185 ((eql error :unknown-option) (format *error-output* "Unknown option: ~A. See 'candle project failures --help'.~%" (car remaining-args)))
186 ((eql error :required-argument-missing) (format *error-output* "Missing argument for ~A. See 'candle project failures --help'.~%" (car remaining-args)))
187 ((opera:option-present :help options) (format t "~A" usage))
191 `(candle:failures ,(when (opera:option-present :project options) (opera:option-argument :project options))))))))))
193 ;;; Section for ./candle job
195 (defmethod execute-command ((command (eql :job)) args)
196 (multiple-value-bind (options remaining-args error) (opera:process-arguments (job-options) args)
198 ((eql error :unknown-option) (format *error-output* "Unknown option: ~A. See 'candle job --help'.~%" (car remaining-args)))
199 ((eql error :required-argument-missing) (format *error-output* "Missing argument for ~A. See 'candle job --help'.~%" (car remaining-args)))
200 ((opera:option-present :help options) (format t "~A" (opera:usage "candle job" (job-options) "Lists all jobs in PROJECT if no other action is specified")))
201 ((not (opera:option-present :project-name options)) (format *error-output* "Requires --project argument. See 'candle job --help'.~%" ))
202 ((opera:option-present :log options) (job-log (opera:option-argument :project-name options) (opera:option-argument :log options)))
203 (t (project-history (opera:option-argument :project-name options))))))
205 (defun job-options ()
206 '((:name :help :short "h" :long "help" :description "Print this usage.")
207 (:name :project-name :long "project" :takes-argument t
208 :variable-name "PROJECT"
209 :description "The project name for the jobs under consideration. Required argumnet.")
210 (:name :log :long "log" :takes-argument t
212 :description "Show's the processing log for job at sha SHA. SHA can be truncated.")))
214 (defun project-history (name)
218 (sort (communication:query `(candle:project-job-information ,name)) #'< :key #'third))))
220 (defun job-log (project-name sha)
221 (format t "~A~%" (communication:query `(candle:get-job-log ,project-name ,sha))))
223 ;;; Section for ./candle run
225 (defmethod execute-command ((command (eql :run)) args)
226 (multiple-value-bind (options remaining-args error) (opera:process-arguments (run-options) args)
228 ((eql error :unknown-option) (format *error-output* "Unknown option: ~A. See 'candle run --help'.~%" (car remaining-args)))
229 ((eql error :required-argument-missing) (format *error-output* "Missing argument for ~A. See 'candle run --help'.~%" (car remaining-args)))
230 (remaining-args (format *error-output* "Unknown option: ~A. See 'candle run --help'.~%" (car remaining-args)))
231 ((opera:option-present :help options) (format t "~A" (opera:usage "candle run" (run-options))))
232 ((not (candle:run)) (sb-ext:exit :code 1)))))
234 (defun run-options ()
235 '((:name :help :short "h" :long "help" :description "Print this usage.")))