4272f7f310022ebb01773c9c71bacb8648818e72
[candle] / bin / candle
1 #!/usr/bin/sbcl --script
2
3 (setf *compile-print* nil)
4 (require 'asdf)
5 (asdf:initialize-source-registry
6  `(:source-registry (:tree ,(car (directory "."))) :INHERIT-CONFIGURATION))
7 (asdf:load-system :candle)
8 (asdf:load-system :opera)
9
10 (in-package #:candle)
11
12 (defun main-options ()
13  '((:name :help :short "h" :long "help" :description "Print this usage.")
14    (:name :port :short "p" :long "port" :takes-argument t :variable-name "PORT"
15     :description "Port on which to listen for commands.  Defaults to 25004")
16    (:positional "<command>" :required t :description "Command to send to candle server")))
17
18 (defun project-options()
19  '((:name :help :short "h" :long "help" :description "Print this usage.")
20    (:name :add :long "add" :takes-argument t :description
21     "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."
22     :variable-name "<name>:<src>")))
23
24 (defun main-usage ()
25  (format t "~A"
26   (opera:usage
27    "candle"
28    (main-options)
29    "Interacts with candle server.  The available commands are:
30   project   List, show or add projects
31   job       List or show jobs")))
32
33 (defgeneric execute-command (command args))
34
35 (defmethod execute-command (command args)
36  (format *error-output* "Unknown command '~(~A~)'.  See 'candle --help'.~%" command))
37
38 (defun add-project (project-definition)
39  (let
40   ((pos (position #\: project-definition)))
41   (cond
42    ((not pos) (format *error-output* "Project definition ~A is not valid.  See 'candle project --help'.~%" project-definition))
43    (t
44     (let
45      ((response (communication:query `(add-project ,(subseq project-definition 0 pos) ,(subseq project-definition (1+ pos))))))
46      (format t "Add project ~A at src definition ~A~%" (project-name response) (project-src response)))))))
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 (defmethod execute-command ((command (eql :job)) args)
57  (format t "~A~%" (communication:query '(+ 2 2))))
58
59 (multiple-value-bind (options remaining-args error) (opera:process-arguments (main-options) (cdr sb-ext:*posix-argv*))
60  (cond
61   ((opera:option-present :help options) (main-usage))
62   ((eql error :unknown-option) (format *error-output* "Unknown option: ~A.  See 'candle --help'.~%" (car remaining-args)))
63   ((and (opera:option-present :port options) (not (parse-integer (opera:option-argument :port options) :junk-allowed t)))
64    (format *error-output* "--port requires a number.  See 'candle-server -h'~%"))
65   ((not remaining-args) (format *error-output* "Command required.  See 'candle --help'.~%"))
66   (t
67    (let
68     ((communication:*query-port*
69       (or
70        (and
71         (opera:option-present :port options)
72         (parse-integer (opera:option-argument :port options) :junk-allowed t))
73        25004)))
74     (execute-command (intern (string-upcase (car remaining-args)) :keyword) (cdr remaining-args))))))
75
76 ; vim:ft=lisp