Add project listing
[candle] / src / main / cli.lisp
index 006830eca33bab8387aa3c986ff253488e5c1386..8afc8403111cedf3a474fa72ed4cb23b19e581fb 100644 (file)
@@ -1,26 +1,47 @@
 (in-package #:candle-cli)
 
+(defgeneric execute-command (command args))
+
+(defmethod execute-command (command args)
+ (format *error-output* "Unknown command '~(~A~)'.  See 'candle --help'.~%" command))
+
+(defun job-info->line (job-info)
+ (format nil "~A (~A) ~A"
+  (subseq (first job-info) 0 8)
+  (format nil "~{~A/~A/~A ~A:~A~}"
+   (utils:time-as-list (third job-info) :month :date :year :hr :min))
+  (case (second job-info)
+   (:succeeded (format nil "~c[1;32mPassed~c[0m" #\Esc #\Esc))
+   (:failed (format nil "~c[1;31mFailed~c[0m" #\Esc #\Esc))
+   (:queued "In queue")
+   (:no-candle-file "No candle file present")
+   (:in-progress "In progress"))))
+
+;;; Section for ./candle
+
+(defun run ()
+ (multiple-value-bind (options remaining-args error) (opera:process-arguments (main-options) (cdr sb-ext:*posix-argv*))
+  (cond
+   ((opera:option-present :help options) (main-usage))
+   ((eql error :unknown-option) (format *error-output* "Unknown option: ~A.  See 'candle --help'.~%" (car remaining-args)))
+   ((and (opera:option-present :port options) (not (parse-integer (opera:option-argument :port options) :junk-allowed t)))
+    (format *error-output* "--port requires a number.  See 'candle-server -h'~%"))
+   ((not remaining-args) (format *error-output* "Command required.  See 'candle --help'.~%"))
+   (t
+    (let
+     ((communication:*query-port*
+       (or
+        (and
+         (opera:option-present :port options)
+         (parse-integer (opera:option-argument :port options) :junk-allowed t))
+        25004)))
+     (execute-command (intern (string-upcase (car remaining-args)) :keyword) (cdr remaining-args)))))))
+
 (defun main-options ()
  '((:name :help :short "h" :long "help" :description "Print this usage.")
    (:name :port :short "p" :long "port" :takes-argument t :variable-name "PORT"
     :description "Port on which to listen for commands.  Defaults to 25004")
-   (:positional "<command>" :required t :description "Command to send to candle server")))
-
-(defun project-options ()
- '((:name :help :short "h" :long "help" :description "Print this usage.")
-   (:name :add :long "add" :takes-argument t :description
-    "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."
-    :variable-name "<name>:<src>")))
-
-(defun job-options ()
- '((:name :help :short "h" :long "help" :description "Print this usage.")
-   (:name :project-name :long "project" :takes-argument t :description "The project name for the jobs under consideration.")
-   (:name :add :long "add" :takes-argument t :description
-    "Add a job to a project.  <sha> is the commit for that project to run the job.  Requires --project to be specified."
-    :variable-name "<sha>")))
-
-(defun run-options ()
- '((:name :help :short "h" :long "help" :description "Print this usage.")))
+   (:positional "<command>" :required t :description "Command for candle, see below")))
 
 (defun main-usage ()
  (format t "~A"
    "candle"
    (main-options)
    "Interacts with candle server.  The available commands are:
-  project   List, show or add projects
-  job       List or show jobs
+  project   Interact with projects
+  job       Get information about jobs
   run       Local command.  Run candle in the current working directory")))
 
-(defgeneric execute-command (command args))
-
-(defmethod execute-command (command args)
- (format *error-output* "Unknown command '~(~A~)'.  See 'candle --help'.~%" command))
-
-(defun add-project (project-definition)
- (let
-  ((pos (position #\: project-definition)))
-  (cond
-   ((not pos) (format *error-output* "Project definition ~A is not valid.  See 'candle project --help'.~%" project-definition))
-   (t
-    (let*
-     ((name (subseq project-definition 0 pos))
-      (src (subseq project-definition (1+ pos))))
-     (format t "Added project ~A at src definition ~A~%" name src))))))
+;;; Section for ./candle project
 
 (defmethod execute-command ((command (eql :project)) args)
  (multiple-value-bind (options remaining-args error) (opera:process-arguments (project-options) args)
   (cond
-   ((eql error :unknown-option) (format *error-output* "Unknown option: ~A.  See 'candle project --help'.~%" (car remaining-args)))
-   ((eql error :required-argument-missing) (format *error-output* "Missing argument for ~A.  See 'candle project --help'.~%" (car remaining-args)))
-   ((opera:option-present :help options) (format t "~A" (opera:usage "candle project" (project-options))))
-   ((opera:option-present :add options) (add-project (opera:option-argument :add options))))))
+   ((eql error :unknown-option)
+    (format *error-output* "Unknown option: ~A.  See 'candle project --help'.~%" (car remaining-args)))
+   ((eql error :required-argument-missing)
+    (format *error-output* "Missing argument for ~A.  See 'candle project --help'.~%" (car remaining-args)))
+   ((opera:option-present :help options) (project-usage))
+   (t
+    (let
+     ((subcommand (intern (string-upcase (car remaining-args)) :keyword)))
+     (case subcommand
+      (:delete (delete-project (cdr remaining-args)))
+      (:add (add-project (cdr remaining-args)))
+      (:show (show-project (cdr remaining-args)))
+      (:refresh (refresh-project (cdr remaining-args)))
+      (:list (list-projects))
+      (t (project-usage))))))))
+
+(defun project-usage ()
+ (format t "~A"
+  (opera:usage
+   "candle project"
+   (project-options)
+   "Interacts with projects.  The available project subcommands are:
+  list              List all projects
+  add <name>:<src>  Add a project
+  delete <name>     Delete a project
+  show <name>       Show project branch information
+  refresh <name>    Tell the candle server to refresh the project information")))
+
+(defun project-options ()
+ '((:name :help :short "h" :long "help" :description "Print this usage.")
+   (:positional "<subcommand>" :description "Project subcommand, see below.")))
+
+(defun add-project (args)
+ (let*
+  ((options
+   '((:name :help :short "h" :long "help" :description "Print this usage.")
+     (: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.")))
+    (usage (opera:usage "candle project add" options)))
+  (multiple-value-bind (options remaining-args error) (opera:process-arguments options args)
+   (cond
+    ((eql error :unknown-option) (format *error-output* "Unknown option: ~A.  See 'candle project add --help'.~%" (car remaining-args)))
+    ((eql error :required-argument-missing) (format *error-output* "Missing argument for ~A.  See 'candle project add --help'.~%" (car remaining-args)))
+    ((opera:option-present :help options) (format t "~A" usage))
+    ((not remaining-args) (format *error-output* "Required <name>:<src>.  See 'candle project add --help'.~%"))
+    (t
+     (let*
+      ((project-definition (car remaining-args))
+       (pos (position #\: project-definition)))
+      (cond
+       ((not pos) (format *error-output* "Project definition ~A is not valid.  See 'candle project --help'.~%" project-definition))
+       (t
+        (let*
+         ((name (subseq project-definition 0 pos))
+          (src (subseq project-definition (1+ pos))))
+         (communication:query `(candle:add-project ,name ,src))
+         (format t "Added project ~A at src definition ~A~%" name src))))))))))
 
-(defun add-job (project-name sha)
- (format t "Added job with sha ~A to project ~A~%" sha project-name))
+(defun delete-project (args)
+ (let*
+  ((options
+   '((:name :help :short "h" :long "help" :description "Print this usage.")
+     (:positional "<name>" :description "<name> is the name of the project to delete")))
+    (usage (opera:usage "candle project delete" options)))
+  (multiple-value-bind (options remaining-args error) (opera:process-arguments options args)
+   (cond
+    ((eql error :unknown-option) (format *error-output* "Unknown option: ~A.  See 'candle project delete --help'.~%" (car remaining-args)))
+    ((eql error :required-argument-missing) (format *error-output* "Missing argument for ~A.  See 'candle project delete --help'.~%" (car remaining-args)))
+    ((opera:option-present :help options) (format t "~A" usage))
+    ((not remaining-args) (format *error-output* "Required <name>.  See 'candle project delete --help'.~%"))
+    (t
+     (communication:query `(candle:delete-project ,(car remaining-args)))
+     (format t "Removed project ~A~%" (car remaining-args)))))))
+
+(defun show-project (args)
+ (let*
+  ((options
+   '((:name :help :short "h" :long "help" :description "Print this usage.")
+     (:positional "<name>" :description "<name> is the name of the project to show")))
+    (usage (opera:usage "candle project delete" options)))
+  (multiple-value-bind (options remaining-args error) (opera:process-arguments options args)
+   (cond
+    ((eql error :unknown-option) (format *error-output* "Unknown option: ~A.  See 'candle project show --help'.~%" (car remaining-args)))
+    ((eql error :required-argument-missing) (format *error-output* "Missing argument for ~A.  See 'candle project show --help'.~%" (car remaining-args)))
+    ((opera:option-present :help options) (format t "~A" usage))
+    ((not remaining-args) (format *error-output* "Required <name>.  See 'candle project show --help'.~%"))
+    (t
+     (let*
+      ((branch-infos (communication:query `(candle:project-branch-information ,(car remaining-args))))
+       (width (apply #'max (mapcar #'length (mapcar #'car branch-infos)))))
+      (mapcar
+       (lambda (branch-info)
+        (format t (format nil "~~~A@A: ~~A~~%" width)
+         (first branch-info)
+         (job-info->line (second branch-info))))
+       (sort branch-infos #'< :key (lambda (branch-info) (third (second branch-info)))))))))))
+
+(defun refresh-project (args)
+ (let*
+  ((options
+   '((:name :help :short "h" :long "help" :description "Print this usage.")
+     (:positional "<name>" :description "<name> is the name of the project to refresh")))
+    (usage (opera:usage "candle project refresh" options)))
+  (multiple-value-bind (options remaining-args error) (opera:process-arguments options args)
+   (cond
+    ((eql error :unknown-option) (format *error-output* "Unknown option: ~A.  See 'candle project refresh --help'.~%" (car remaining-args)))
+    ((eql error :required-argument-missing) (format *error-output* "Missing argument for ~A.  See 'candle project refresh --help'.~%" (car remaining-args)))
+    ((opera:option-present :help options) (format t "~A" usage))
+    ((not remaining-args) (format *error-output* "Required <name>.  See 'candle project refresh --help'.~%"))
+    (t
+     (communication:query `(candle:refresh-project ,(car remaining-args)))
+     (format t "Refreshed project ~A~%" (car remaining-args)))))))
+
+(defun list-projects ()
+ (format t "~{~{~A  ~A~}~%~}" (communication:query `(candle:list-projects))))
+
+;;; Section for ./candle job
 
 (defmethod execute-command ((command (eql :job)) args)
  (multiple-value-bind (options remaining-args error) (opera:process-arguments (job-options) args)
   (cond
    ((eql error :unknown-option) (format *error-output* "Unknown option: ~A.  See 'candle job --help'.~%" (car remaining-args)))
    ((eql error :required-argument-missing) (format *error-output* "Missing argument for ~A.  See 'candle job --help'.~%" (car remaining-args)))
-   ((opera:option-present :help options) (format t "~A" (opera:usage "candle job" (job-options))))
-   ((and (opera:option-present :add options) (not (opera:option-present :project-name options)))
-    (format *error-output* "Cannot --add without --project.  See 'candle job --help'.~%"))
-   ((opera:option-present :add options)
-    (add-job
-     (opera:option-argument :project-name options)
-     (opera:option-argument :add options))))))
+   ((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")))
+   ((not (opera:option-present :project-name options)) (format *error-output* "Requires --project argument.  See 'candle job --help'.~%" ))
+   ((opera:option-present :log options) (job-log (opera:option-argument :project-name options) (opera:option-argument :log options)))
+   (t (project-history (opera:option-argument :project-name options))))))
+
+(defun job-options ()
+ '((:name :help :short "h" :long "help" :description "Print this usage.")
+   (:name :project-name :long "project" :takes-argument t
+    :variable-name "PROJECT"
+    :description "The project name for the jobs under consideration.  Required argumnet.")
+   (:name :log :long "log" :takes-argument t
+    :variable-name "SHA"
+    :description "Show's the processing log for job at sha SHA.  SHA can be truncated.")))
+
+(defun project-history (name)
+ (format t "~{~A~%~}"
+  (mapcar
+   #'job-info->line
+   (sort (communication:query `(candle:project-job-information ,name)) #'< :key #'third))))
+
+(defun job-log (project-name sha)
+ (format t "~A~%" (communication:query `(candle:get-job-log ,project-name ,sha))))
+
+;;; Section for ./candle run
 
 (defmethod execute-command ((command (eql :run)) args)
  (multiple-value-bind (options remaining-args error) (opera:process-arguments (run-options) args)
    ((opera:option-present :help options) (format t "~A" (opera:usage "candle run" (run-options))))
    ((not (candle:run)) (sb-ext:exit :code 1)))))
 
-(defun run ()
- (multiple-value-bind (options remaining-args error) (opera:process-arguments (main-options) (cdr sb-ext:*posix-argv*))
-  (cond
-   ((opera:option-present :help options) (main-usage))
-   ((eql error :unknown-option) (format *error-output* "Unknown option: ~A.  See 'candle --help'.~%" (car remaining-args)))
-   ((and (opera:option-present :port options) (not (parse-integer (opera:option-argument :port options) :junk-allowed t)))
-    (format *error-output* "--port requires a number.  See 'candle-server -h'~%"))
-   ((not remaining-args) (format *error-output* "Command required.  See 'candle --help'.~%"))
-   (t
-    (let
-     ((communication:*query-port*
-       (or
-        (and
-         (opera:option-present :port options)
-         (parse-integer (opera:option-argument :port options) :junk-allowed t))
-        25004)))
-     (execute-command (intern (string-upcase (car remaining-args)) :keyword) (cdr remaining-args)))))))
+(defun run-options ()
+ '((:name :help :short "h" :long "help" :description "Print this usage.")))