Change project options in subcommands, refactor cli
[candle] / src / main / cli.lisp
1 (in-package #:candle-cli)
2
3 (defgeneric execute-command (command args))
4
5 (defmethod execute-command (command args)
6  (format *error-output* "Unknown command '~(~A~)'.  See 'candle --help'.~%" command))
7
8 (defun job-info->line (job-info)
9  (format nil "~A (~A) ~A"
10   (subseq (first job-info) 0 8)
11   (format nil "~{~A/~A/~A ~A:~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))
16    (:queued "In queue")
17    (:no-candle-file "No candle file present")
18    (:in-progress "In progress"))))
19
20 ;;; Section for ./candle
21
22 (defun run ()
23  (multiple-value-bind (options remaining-args error) (opera:process-arguments (main-options) (cdr sb-ext:*posix-argv*))
24   (cond
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'.~%"))
30    (t
31     (let
32      ((communication:*query-port*
33        (or
34         (and
35          (opera:option-present :port options)
36          (parse-integer (opera:option-argument :port options) :junk-allowed t))
37         25004)))
38      (execute-command (intern (string-upcase (car remaining-args)) :keyword) (cdr remaining-args)))))))
39
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")))
45
46 (defun main-usage ()
47  (format t "~A"
48   (opera:usage
49    "candle"
50    (main-options)
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")))
55
56 ;;; Section for ./candle project
57
58 (defmethod execute-command ((command (eql :project)) args)
59  (multiple-value-bind (options remaining-args error) (opera:process-arguments (project-options) args)
60   (cond
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))
66    (t
67     (let
68      ((subcommand (intern (string-upcase (car remaining-args)) :keyword)))
69      (case subcommand
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       (t (project-usage))))))))
75
76 (defun project-usage ()
77  (format t "~A"
78   (opera:usage
79    "candle project"
80    (project-options)
81    "Interacts with projects.  The available project subcommands are:
82   add       Add a project
83   delete    Delete a project
84   show      Show project branch information
85   refresh   Tell the candle server to refresh the project information")))
86
87 (defun project-options ()
88  '((:name :help :short "h" :long "help" :description "Print this usage.")
89    (:positional "<subcommand>" :description "Project subcommand, see below.")))
90
91 ;   (:name :show :long "show" :takes-argument t :description
92 ;    "Show branch information for a project named by NAME."
93 ;    :variable-name "NAME")
94 ;   (:name :refresh :long "refresh" :takes-argument t :description
95 ;    "Refresh project named by NAME."
96 ;    :variable-name "NAME")
97 ;   (:name :delete :long "delete" :takes-argument t :description
98 ;    "Delete a project named by NAME."
99 ;    :variable-name "NAME")
100
101 (defun add-project (args)
102  (let*
103   ((options
104    '((:name :help :short "h" :long "help" :description "Print this usage.")
105      (: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.")))
106     (usage (opera:usage "candle project add" options)))
107   (multiple-value-bind (options remaining-args error) (opera:process-arguments options args)
108    (cond
109     ((eql error :unknown-option) (format *error-output* "Unknown option: ~A.  See 'candle project add --help'.~%" (car remaining-args)))
110     ((eql error :required-argument-missing) (format *error-output* "Missing argument for ~A.  See 'candle project add --help'.~%" (car remaining-args)))
111     ((opera:option-present :help options) (format t "~A" usage))
112     ((not remaining-args) (format *error-output* "Required <name>:<src>.  See 'candle project add --help'.~%"))
113     (t
114      (let*
115       ((project-definition (car remaining-args))
116        (pos (position #\: project-definition)))
117       (cond
118        ((not pos) (format *error-output* "Project definition ~A is not valid.  See 'candle project --help'.~%" project-definition))
119        (t
120         (let*
121          ((name (subseq project-definition 0 pos))
122           (src (subseq project-definition (1+ pos))))
123          (communication:query `(candle:add-project ,name ,src))
124          (format t "Added project ~A at src definition ~A~%" name src))))))))))
125
126 (defun delete-project (args)
127  (let*
128   ((options
129    '((:name :help :short "h" :long "help" :description "Print this usage.")
130      (:positional "<name>" :description "<name> is the name of the project to delete")))
131     (usage (opera:usage "candle project delete" options)))
132   (multiple-value-bind (options remaining-args error) (opera:process-arguments options args)
133    (cond
134     ((eql error :unknown-option) (format *error-output* "Unknown option: ~A.  See 'candle project delete --help'.~%" (car remaining-args)))
135     ((eql error :required-argument-missing) (format *error-output* "Missing argument for ~A.  See 'candle project delete --help'.~%" (car remaining-args)))
136     ((opera:option-present :help options) (format t "~A" usage))
137     ((not remaining-args) (format *error-output* "Required <name>.  See 'candle project delete --help'.~%"))
138     (t
139      (communication:query `(candle:delete-project ,(car remaining-args)))
140      (format t "Removed project ~A~%" (car remaining-args)))))))
141
142 (defun show-project (args)
143  (let*
144   ((options
145    '((:name :help :short "h" :long "help" :description "Print this usage.")
146      (:positional "<name>" :description "<name> is the name of the project to show")))
147     (usage (opera:usage "candle project delete" options)))
148   (multiple-value-bind (options remaining-args error) (opera:process-arguments options args)
149    (cond
150     ((eql error :unknown-option) (format *error-output* "Unknown option: ~A.  See 'candle project show --help'.~%" (car remaining-args)))
151     ((eql error :required-argument-missing) (format *error-output* "Missing argument for ~A.  See 'candle project show --help'.~%" (car remaining-args)))
152     ((opera:option-present :help options) (format t "~A" usage))
153     ((not remaining-args) (format *error-output* "Required <name>.  See 'candle project show --help'.~%"))
154     (t
155      (let*
156       ((branch-infos (communication:query `(candle:project-branch-information ,(car remaining-args))))
157        (width (apply #'max (mapcar #'length (mapcar #'car branch-infos)))))
158       (mapcar
159        (lambda (branch-info)
160         (format t (format nil "~~~A@A: ~~A~~%" width)
161          (first branch-info)
162          (job-info->line (second branch-info))))
163        (sort branch-infos #'< :key (lambda (branch-info) (third (second branch-info)))))))))))
164
165 (defun refresh-project (args)
166  (let*
167   ((options
168    '((:name :help :short "h" :long "help" :description "Print this usage.")
169      (:positional "<name>" :description "<name> is the name of the project to refresh")))
170     (usage (opera:usage "candle project refresh" options)))
171   (multiple-value-bind (options remaining-args error) (opera:process-arguments options args)
172    (cond
173     ((eql error :unknown-option) (format *error-output* "Unknown option: ~A.  See 'candle project refresh --help'.~%" (car remaining-args)))
174     ((eql error :required-argument-missing) (format *error-output* "Missing argument for ~A.  See 'candle project refresh --help'.~%" (car remaining-args)))
175     ((opera:option-present :help options) (format t "~A" usage))
176     ((not remaining-args) (format *error-output* "Required <name>.  See 'candle project refresh --help'.~%"))
177     (t
178      (communication:query `(candle:refresh-project ,(car remaining-args)))
179      (format t "Refreshed project ~A~%" (car remaining-args)))))))
180
181 ;;; Section for ./candle job
182
183 (defmethod execute-command ((command (eql :job)) args)
184  (multiple-value-bind (options remaining-args error) (opera:process-arguments (job-options) args)
185   (cond
186    ((eql error :unknown-option) (format *error-output* "Unknown option: ~A.  See 'candle job --help'.~%" (car remaining-args)))
187    ((eql error :required-argument-missing) (format *error-output* "Missing argument for ~A.  See 'candle job --help'.~%" (car remaining-args)))
188    ((opera:option-present :help options) (format t "~A" (opera:usage "candle job" (job-options))))
189    ((not (opera:option-present :project-name options)) (format *error-output* "Requires --project argument.  See 'candle job --help'.~%" ))
190    ((opera:option-present :log options) (job-log (opera:option-argument :project-name options) (opera:option-argument :log options)))
191    (t (project-history (opera:option-argument :project-name options))))))
192
193 (defun job-options ()
194  '((:name :help :short "h" :long "help" :description "Print this usage.")
195    (:name :project-name :long "project" :takes-argument t
196     :variable-name "PROJECT"
197     :description "The project name for the jobs under consideration.  Required argumnet.")
198    (:name :log :long "log" :takes-argument t
199     :variable-name "SHA"
200     :description "Show's the processing log for job at sha SHA.  SHA can be truncated.")))
201
202 (defun project-history (name)
203  (format t "~{~A~%~}"
204   (mapcar
205    #'job-info->line
206    (sort (communication:query `(candle:project-job-information ,name)) #'< :key #'third))))
207
208 (defun job-log (project-name sha)
209  (format t "~A~%" (communication:query `(candle:get-job-log ,project-name ,sha))))
210
211 ;;; Section for ./candle run
212
213 (defmethod execute-command ((command (eql :run)) args)
214  (multiple-value-bind (options remaining-args error) (opera:process-arguments (run-options) args)
215   (cond
216    ((eql error :unknown-option) (format *error-output* "Unknown option: ~A.  See 'candle run --help'.~%" (car remaining-args)))
217    ((eql error :required-argument-missing) (format *error-output* "Missing argument for ~A.  See 'candle run --help'.~%" (car remaining-args)))
218    (remaining-args (format *error-output* "Unknown option: ~A. See 'candle run --help'.~%" (car remaining-args)))
219    ((opera:option-present :help options) (format t "~A" (opera:usage "candle run" (run-options))))
220    ((not (candle:run)) (sb-ext:exit :code 1)))))
221
222 (defun run-options ()
223  '((:name :help :short "h" :long "help" :description "Print this usage.")))