Add Licensing and Contributing
[candle] / src / main / cli.lisp
1 ; Copyright 2022 Frank Duncan (frank@consxy.com) under AGPL3.  See distributed LICENSE.txt.
2 (in-package #:candle-cli)
3
4 (defgeneric execute-command (command args))
5
6 (defun error-and-exit (str &rest args)
7  (apply #'format *error-output* str args)
8  (sb-ext:exit :code 1))
9
10 (defmethod execute-command (command args)
11  (error-and-exit "Unknown command '~(~A~)'.  See 'candle --help'.~%" command))
12
13 (defun job-info->line (job-info)
14  (format nil "~A:~A (~A) ~A"
15   (first job-info)
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))
22    (:queued "In queue")
23    (:no-candle-file "No candle file present")
24    (:in-progress "In progress"))))
25
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)
28    (cond
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))
37     (t
38      ,@success))))
39
40 ;;; Section for ./candle
41
42 (defun run ()
43  (standard-cli "candle" (main-options) (cdr sb-ext:*posix-argv*) (main-usage) "Command"
44   (handler-case
45    (if
46     (and
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'~%")
50     (let
51      ((communication:*query-port*
52        (or
53         (and
54          (opera:option-present :port parsed-options)
55          (parse-integer (opera:option-argument :port parsed-options) :junk-allowed t))
56         25004)))
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))))))))
67
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")))
73
74 (defun main-usage ()
75  (opera:usage
76   "candle"
77   (main-options)
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"))
82
83 ;;; Section for ./candle project
84
85 (defmethod execute-command ((command (eql :project)) args)
86  (standard-cli "candle project" (project-options) args (project-usage) nil
87   (let
88    ((subcommand (intern (string-upcase (car remaining-args)) :keyword)))
89    (case subcommand
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)))))))
97
98 (defun project-usage ()
99  (opera:usage
100   "candle project"
101   (project-options)
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"))
108
109 (defun project-options ()
110  '((:name :help :short "h" :long "help" :description "Print this usage.")
111    (:positional "<subcommand>" :description "Project subcommand, see below.")))
112
113 (defun add-project (args)
114  (let
115   ((options
116     `((:name :help :short "h" :long "help" :description "Print this usage.")
117       (:positional "<name>:<src>"
118        :description
119        ,(format nil "~{~A~}"
120          (list
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."
123           ""))))))
124   (standard-cli "candle project add" options args :default "<name>:<src>"
125    (let*
126     ((project-definition (car remaining-args))
127      (pos (position #\: project-definition)))
128     (cond
129      ((not pos)
130       (error-and-exit "Project definition ~A is not valid.  See 'candle project add --help'.~%" project-definition))
131      (t
132       (let*
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))))))))
137
138 (defun delete-project (args)
139  (let
140   ((options
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)))))
146
147 (defun show-project (args)
148  (let
149   ((options
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>"
153    (let*
154     ((branch-infos (communication:query `(candle:project-branch-information ,(car remaining-args))))
155      (width (apply #'max (mapcar #'length (mapcar #'car branch-infos)))))
156     (mapcar
157      (lambda (branch-info)
158       (format t (format nil "~~~A@A: ~~A~~%" width)
159        (first branch-info)
160        (job-info->line (second branch-info))))
161      (sort branch-infos #'< :key (lambda (branch-info) (fourth (second branch-info)))))))))
162
163 (defun refresh-project (args)
164  (let
165   ((options
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)))))
171
172 (defun list-projects ()
173  (format t "~{~A~%~}"
174   (mapcar
175    (lambda (info)
176     (format nil "~A  ~A~A"
177      (car info)
178      (cadr info)
179      (if (zerop (caddr info)) "" (format nil " (~A branches ~c[1;31mfailing~c[0m)" (caddr info) #\Esc #\Esc))))
180    (communication:query `(candle:list-projects)))))
181
182 (defun project-failures (args)
183  (let
184   ((options
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
189    (format t "~A"
190     (communication:query
191      `(candle:failures
192        ,(when (opera:option-present :project parsed-options)
193          (opera:option-argument :project parsed-options))))))))
194
195 ;;; Section for ./candle job
196
197 (defmethod execute-command ((command (eql :job)) args)
198  (standard-cli "candle job" (job-options) args (job-usage) nil
199   (let
200    ((subcommand (intern (string-upcase (car remaining-args)) :keyword)))
201    (case subcommand
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)))))))
206
207 (defun job-options ()
208  '((:name :help :short "h" :long "help" :description "Print this usage.")
209    (:positional "<subcommand>" :description "Job subcommand, see below.")))
210
211 (defun job-usage ()
212  (opera:usage
213   "candle job"
214   (project-options)
215   "Interacts with projects.  The available project subcommands are:
216  list                   List jobs
217  log <project>:<sha>    View the log for a job
218  retry <project>:<sha>  Retry a job"))
219
220 (defun job-list (args)
221  (let
222   ((options
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
227    (format t "~{~A~%~}"
228     (mapcar
229      #'job-info->line
230      (sort
231       (communication:query `(candle:project-job-information ,(opera:option-argument :project parsed-options)))
232       #'< :key #'fourth))))))
233
234 (defun decompose-job-definition (job-definition)
235  (let
236   ((pos (position #\: job-definition)))
237   (when
238    pos
239    (values
240     (subseq job-definition 0 pos)
241     (subseq job-definition (1+ pos))))))
242
243 (defun job-log (args)
244  (let
245   ((options
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))
251     (if project-name
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)))))))
254
255 (defun retry-job (args)
256  (let
257   ((options
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))
263     (if project-name
264      (progn
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)))))))
268
269 ;;; Section for ./candle run
270
271 (defmethod execute-command ((command (eql :run)) args)
272  (let
273   ((options
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)))
281    (cond
282     ((opera:option-present :task parsed-options)
283      (let
284       ((tasks (candle:list-tasks))
285        (specified-task (intern (string-upcase (opera:option-argument :task parsed-options)) :keyword)))
286       (if
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)))))))