+(defgeneric execute-command (command args))
+
+(defun error-and-exit (str &rest args)
+ (apply #'format *error-output* str args)
+ (sb-ext:exit :code 1))
+
+(defmethod execute-command (command args)
+ (error-and-exit "Unknown command '~(~A~)'. See 'candle --help'.~%" command))
+
+(defun job-info->line (job-info)
+ (format nil "~A:~A (~A) ~A"
+ (first job-info)
+ (subseq (second job-info) 0 8)
+ (format nil "~{~2,,,'0@A/~2,,,'0@A/~A ~2,,,'0@A:~2,,,'0@A~}"
+ (utils:time-as-list (fourth job-info) :month :date :year :hr :min))
+ (case (third 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"))))
+
+(defmacro standard-cli (cmd options-in args usage remaining-args-required &rest success)
+`(multiple-value-bind (parsed-options remaining-args error) (opera:process-arguments ,options-in ,args)
+ (cond
+ ((opera:option-present :help parsed-options)
+ (format t "~A" ,(if (eql usage :default) `(opera:usage ,cmd ,options-in) usage)))
+ ((eql error :unknown-option)
+ (error-and-exit "Unknown option: ~A. See '~A --help'.~%" (car remaining-args) ,cmd))
+ ((eql error :required-argument-missing)
+ (error-and-exit "Missing argument for ~A. See '~A --help'.~%" (car remaining-args) ,cmd))
+ ((and ,remaining-args-required (not remaining-args))
+ (error-and-exit "~A required. See 'candle --help'.~%" ,remaining-args-required))
+ (t
+ ,@success))))
+
+;;; Section for ./candle
+
+(defun run ()
+ (standard-cli "candle" (main-options) (cdr sb-ext:*posix-argv*) (main-usage) "Command"
+ (handler-case
+ (if
+ (and (opera:option-present :port parsed-options) (not (parse-integer (opera:option-argument :port parsed-options) :junk-allowed t)))
+ (error-and-exit "--port requires a number. See 'candle -h'~%")
+ (let
+ ((communication:*query-port*
+ (or
+ (and
+ (opera:option-present :port parsed-options)
+ (parse-integer (opera:option-argument :port parsed-options) :junk-allowed t))
+ 25004)))
+ (execute-command (intern (string-upcase (car remaining-args)) :keyword) (cdr remaining-args))))
+ (candle:candle-error (e)
+ (case (candle:candle-error-reason e)
+ (:project-does-not-exist (error-and-exit "Project does not exist~%"))
+ (:job-does-not-exist (error-and-exit "Job does not exist~%"))
+ (:invalid-project-name (error-and-exit "Project name invalid~%"))
+ (:invalid-project-uri (error-and-exit "Project uri invalid~%"))
+ (:project-name-taken (error-and-exit "Project name already taken~%"))
+ (:project-failed-to-get-branches (error-and-exit "Unable to retrieve branches from server~%"))
+ (t (error-and-exit "Unknown error occurred: ~(~S~)~%" (candle:candle-error-reason e))))))))
+