Change candle project --history to candle job
[candle] / src / main / cli.lisp
1 (in-package #:candle-cli)
2
3 (defun main-options ()
4  '((:name :help :short "h" :long "help" :description "Print this usage.")
5    (:name :port :short "p" :long "port" :takes-argument t :variable-name "PORT"
6     :description "Port on which to listen for commands.  Defaults to 25004")
7    (:positional "<command>" :required t :description "Command to send to candle server")))
8
9 (defun project-options ()
10  '((:name :help :short "h" :long "help" :description "Print this usage.")
11    (:name :add :long "add" :takes-argument t :description
12     "Add a project.  NAME is the name of the project, which must not include colons, while SRC is the location of the repository for cloning.  This location must be accessible by the machine running candle."
13     :variable-name "NAME:SRC")
14    (:name :show :long "show" :takes-argument t :description
15     "Show branch information for a project named by NAME."
16     :variable-name "NAME")
17    (:name :refresh :long "refresh" :takes-argument t :description
18     "Refresh project named by NAME."
19     :variable-name "NAME")
20    (:name :delete :long "delete" :takes-argument t :description
21     "Delete a project named by NAME."
22     :variable-name "NAME")))
23
24 (defun job-options ()
25  '((:name :help :short "h" :long "help" :description "Print this usage.")
26    (:name :project-name :long "project" :takes-argument t
27     :variable-name "PROJECT"
28     :description "The project name for the jobs under consideration.  Required argumnet.")))
29
30 (defun run-options ()
31  '((:name :help :short "h" :long "help" :description "Print this usage.")))
32
33 (defun main-usage ()
34  (format t "~A"
35   (opera:usage
36    "candle"
37    (main-options)
38    "Interacts with candle server.  The available commands are:
39   project   List, show or add projects
40   job       List or show jobs
41   run       Local command.  Run candle in the current working directory")))
42
43 (defgeneric execute-command (command args))
44
45 (defmethod execute-command (command args)
46  (format *error-output* "Unknown command '~(~A~)'.  See 'candle --help'.~%" command))
47
48 (defun add-project (project-definition)
49  (let
50   ((pos (position #\: project-definition)))
51   (cond
52    ((not pos) (format *error-output* "Project definition ~A is not valid.  See 'candle project --help'.~%" project-definition))
53    (t
54     (let*
55      ((name (subseq project-definition 0 pos))
56       (src (subseq project-definition (1+ pos))))
57      (communication:query `(candle:add-project ,name ,src))
58      (format t "Added project ~A at src definition ~A~%" name src))))))
59
60 (defun delete-project (name)
61  (communication:query `(candle:delete-project ,name))
62  (format t "Removed project ~A~%" name))
63
64 (defun job-info->line (job-info)
65  (format nil "~A (~A) ~A"
66   (subseq (first job-info) 0 8)
67   (format nil "~{~A/~A/~A ~A:~A~}"
68    (utils:time-as-list (third job-info) :month :date :year :hr :min))
69   (case (second job-info)
70    (:succeeded (format nil "~c[1;32mPassed~c[0m" #\Esc #\Esc))
71    (:failed (format nil "~c[1;31mFailed~c[0m" #\Esc #\Esc))
72    (:queued "In queue")
73    (:no-candle-file "No candle file present")
74    (:in-progress "In progress"))))
75
76 (defun show-project (name)
77  (let*
78   ((branch-infos (communication:query `(candle:project-branch-information ,name)))
79    (width (apply #'max (mapcar #'length (mapcar #'car branch-infos)))))
80   (mapcar
81    (lambda (branch-info)
82     (format t (format nil "~~~A@A: ~~A~~%" width)
83      (first branch-info)
84      (job-info->line (second branch-info))))
85    (sort branch-infos #'< :key (lambda (branch-info) (third (second branch-info)))))))
86
87 (defun project-history (name)
88  (format t "~{~A~%~}"
89   (mapcar
90    #'job-info->line
91    (sort (communication:query `(candle:project-job-information ,name)) #'< :key #'third))))
92
93 (defun refresh-project (name)
94  (communication:query `(candle:refresh-project ,name))
95  (format t "Refreshed project ~A~%" name))
96
97 (defmethod execute-command ((command (eql :project)) args)
98  (multiple-value-bind (options remaining-args error) (opera:process-arguments (project-options) args)
99   (cond
100    ((eql error :unknown-option) (format *error-output* "Unknown option: ~A.  See 'candle project --help'.~%" (car remaining-args)))
101    ((eql error :required-argument-missing) (format *error-output* "Missing argument for ~A.  See 'candle project --help'.~%" (car remaining-args)))
102    ((opera:option-present :help options) (format t "~A" (opera:usage "candle project" (project-options))))
103    ((opera:option-present :delete options) (delete-project (opera:option-argument :delete options)))
104    ((opera:option-present :show options) (show-project (opera:option-argument :show options)))
105    ((opera:option-present :refresh options) (refresh-project (opera:option-argument :refresh options)))
106    ((opera:option-present :add options) (add-project (opera:option-argument :add options))))))
107
108 (defmethod execute-command ((command (eql :job)) args)
109  (multiple-value-bind (options remaining-args error) (opera:process-arguments (job-options) args)
110   (cond
111    ((eql error :unknown-option) (format *error-output* "Unknown option: ~A.  See 'candle job --help'.~%" (car remaining-args)))
112    ((eql error :required-argument-missing) (format *error-output* "Missing argument for ~A.  See 'candle job --help'.~%" (car remaining-args)))
113    ((opera:option-present :help options) (format t "~A" (opera:usage "candle job" (job-options))))
114    ((not (opera:option-present :project-name options)) (format *error-output* "Requires --project argument.  See 'candle job --help'.~%" ))
115    (t (project-history (opera:option-argument :project-name options))))))
116
117 (defmethod execute-command ((command (eql :run)) args)
118  (multiple-value-bind (options remaining-args error) (opera:process-arguments (run-options) args)
119   (cond
120    ((eql error :unknown-option) (format *error-output* "Unknown option: ~A.  See 'candle run --help'.~%" (car remaining-args)))
121    ((eql error :required-argument-missing) (format *error-output* "Missing argument for ~A.  See 'candle run --help'.~%" (car remaining-args)))
122    (remaining-args (format *error-output* "Unknown option: ~A. See 'candle run --help'.~%" (car remaining-args)))
123    ((opera:option-present :help options) (format t "~A" (opera:usage "candle run" (run-options))))
124    ((not (candle:run)) (sb-ext:exit :code 1)))))
125
126 (defun run ()
127  (multiple-value-bind (options remaining-args error) (opera:process-arguments (main-options) (cdr sb-ext:*posix-argv*))
128   (cond
129    ((opera:option-present :help options) (main-usage))
130    ((eql error :unknown-option) (format *error-output* "Unknown option: ~A.  See 'candle --help'.~%" (car remaining-args)))
131    ((and (opera:option-present :port options) (not (parse-integer (opera:option-argument :port options) :junk-allowed t)))
132     (format *error-output* "--port requires a number.  See 'candle-server -h'~%"))
133    ((not remaining-args) (format *error-output* "Command required.  See 'candle --help'.~%"))
134    (t
135     (let
136      ((communication:*query-port*
137        (or
138         (and
139          (opera:option-present :port options)
140          (parse-integer (opera:option-argument :port options) :junk-allowed t))
141         25004)))
142      (execute-command (intern (string-upcase (car remaining-args)) :keyword) (cdr remaining-args)))))))