Move cli code into main source directory
[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
15 (defun job-options()
16  '((:name :help :short "h" :long "help" :description "Print this usage.")
17    (:name :project-name :short "p" :long "project" :takes-argument t :description "The project name for the jobs under consideration.")
18    (:name :add :long "add" :takes-argument t :description
19     "Add a job to a project.  <sha> is the commit for that project to run the job.  Requires --project to be specified."
20     :variable-name "<sha>")))
21
22 (defun main-usage ()
23  (format t "~A"
24   (opera:usage
25    "candle"
26    (main-options)
27    "Interacts with candle server.  The available commands are:
28   project   List, show or add projects
29   job       List or show jobs")))
30
31 (defgeneric execute-command (command args))
32
33 (defmethod execute-command (command args)
34  (format *error-output* "Unknown command '~(~A~)'.  See 'candle --help'.~%" command))
35
36 (defun add-project (project-definition)
37  (let
38   ((pos (position #\: project-definition)))
39   (cond
40    ((not pos) (format *error-output* "Project definition ~A is not valid.  See 'candle project --help'.~%" project-definition))
41    (t
42     (let*
43      ((name (subseq project-definition 0 pos))
44       (src (subseq project-definition (1+ pos)))
45       (response (communication:query `(candle:add-project ,name ,src))))
46      (format t "Added project ~A at src definition ~A~%" name src))))))
47
48 (defmethod execute-command ((command (eql :project)) args)
49  (multiple-value-bind (options remaining-args error) (opera:process-arguments (project-options) args)
50   (cond
51    ((eql error :unknown-option) (format *error-output* "Unknown option: ~A.  See 'candle project --help'.~%" (car remaining-args)))
52    ((eql error :required-argument-missing) (format *error-output* "Missing argument for ~A.  See 'candle project --help'.~%" (car remaining-args)))
53    ((opera:option-present :help options) (format t "~A" (opera:usage "candle project" (project-options))))
54    ((opera:option-present :add options) (add-project (opera:option-argument :add options))))))
55
56 (defun add-job (project-name sha)
57  (let
58   ((response (communication:query `(candle:add-job ,project-name ,sha))))
59   (format t "Added job with sha ~A to project ~A~%" sha project-name)))
60
61 (defmethod execute-command ((command (eql :job)) args)
62  (multiple-value-bind (options remaining-args error) (opera:process-arguments (job-options) args)
63   (cond
64    ((eql error :unknown-option) (format *error-output* "Unknown option: ~A.  See 'candle job --help'.~%" (car remaining-args)))
65    ((eql error :required-argument-missing) (format *error-output* "Missing argument for ~A.  See 'candle job --help'.~%" (car remaining-args)))
66    ((opera:option-present :help options) (format t "~A" (opera:usage "candle job" (job-options))))
67    ((and (opera:option-present :add options) (not (opera:option-present :project-name options)))
68     (format *error-output* "Cannot --add without --project.  See 'candle job --help'.~%"))
69    ((opera:option-present :add options)
70     (add-job
71      (opera:option-argument :project-name options)
72      (opera:option-argument :add options))))))
73
74 (defun run ()
75  (multiple-value-bind (options remaining-args error) (opera:process-arguments (main-options) (cdr sb-ext:*posix-argv*))
76   (cond
77    ((opera:option-present :help options) (main-usage))
78    ((eql error :unknown-option) (format *error-output* "Unknown option: ~A.  See 'candle --help'.~%" (car remaining-args)))
79    ((and (opera:option-present :port options) (not (parse-integer (opera:option-argument :port options) :junk-allowed t)))
80     (format *error-output* "--port requires a number.  See 'candle-server -h'~%"))
81    ((not remaining-args) (format *error-output* "Command required.  See 'candle --help'.~%"))
82    (t
83     (let
84      ((communication:*query-port*
85        (or
86         (and
87          (opera:option-present :port options)
88          (parse-integer (opera:option-argument :port options) :junk-allowed t))
89         25004)))
90      (execute-command (intern (string-upcase (car remaining-args)) :keyword) (cdr remaining-args)))))))
91
92 ; vim:ft=lisp