Move server cli into repository with its own file
[candle] / src / main / server-cli.lisp
diff --git a/src/main/server-cli.lisp b/src/main/server-cli.lisp
new file mode 100644 (file)
index 0000000..8947727
--- /dev/null
@@ -0,0 +1,48 @@
+(in-package #:candle-server-cli)
+
+(defvar *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")
+   (:name :dir :long "candle-dir" :takes-argument t :variable-name "DIR"
+    :description "Directory for candle related data.  Will be created if does not exist.  Defaults to /opt/candle/")
+   (:name :system :long "system" :takes-argument t :variable-name "SYSTEM"
+    :description "System on which to run jobs.  Currently available are local and aws.  Defaults to local.")))
+
+(defun usage ()
+ (format t "~A"
+  (opera:usage
+   "candle-server"
+   *options*
+   "Starts a candle continuous integration server.  Use 'candle' to interact with the server.")))
+
+(defun run ()
+ (multiple-value-bind (options remaining-args error) (opera:process-arguments *options* (cdr sb-ext:*posix-argv*))
+  (cond
+   ((opera:option-present :help options) (usage))
+   (remaining-args
+    (format *error-output* "Don't understand ~A.  See 'candle-server -h'~%" (car remaining-args))
+    (sb-ext:exit :code 1))
+   ((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'~%"))
+   (t
+    (let
+     ((port (or (and
+                 (opera:option-present :port options)
+                 (parse-integer (opera:option-argument :port options) :junk-allowed t))
+             25004)))
+    (setf candle:*job-system*
+     (if (opera:option-present :system options)
+      (intern (string-upcase (opera:option-argument :system options)) :keyword)
+      :local))
+    (let
+     ((*error-output* (make-broadcast-stream)))
+     (case candle:*job-system*
+      (:aws (asdf:load-system :candle-aws))
+      (:local (asdf:load-system :candle-local))))
+    (setf candle:*candle-dir*
+     (if (opera:option-present :dir options)
+      (opera:option-argument :dir options)
+      "/opt/candle/"))
+    (format t "Starting server on port ~A~%" port)
+    (candle:server port nil))))))