123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- (defpackage lcmlog
- (:use :cl))
- (in-package :lcmlog)
- (defconstant *help*
- (concatenate 'string "lcmlog: client side of a tool for reading and writing the lcm staff logs.\n\n"
- "Usage: lcmlog [option]"
- "Options:\n"
- "\t-g\tMethod GET: print all the logs that meet the required criteria\n"
- "\t-p\tMethod POST: add a log to the database\n"
- "\t-u\tMethod UPDATE: regenerate the internal server database\n"
- "\t-h\tShow this help\n")
- "Program help message.")
- (defconstant *usage*
- (concatenate 'string "Usage: lcmlog [-g|-p|-u]"
- "Use -h option to show the help message\n")
- "Program usage message.")
- (defconstant editor
- (let (env-editor (getenv "EDITOR"))
- (if (null env-editor)
- "nano"
- env-editor))
- "Program to use to edit the log.")
- (defun get ()
- "Returns a string containing the right headers for the get method."
- (let ((user-type (ask-user-type))
- (date (ask-date)))
- nil))
- (defun double-choice (first second)
- "Return `t' if the user inputs FIRST, `nil' if the user inputs
- SECOND."
- (let ((c (read-char)))
- (cond
- ((char= c first)
- t)
- ((char= c #\Newline)
- (format t (concatenate 'string "Answer " (string first) " or " (string second) " "))
- (double-choice first second))
- (t
- nil))))
- (defun ask (prompt)
- "Ask a question, assuming positive answer by default."
- (format t prompt)
- (double-choice #\y #\n))
- (defun ask-user-type ()
- "Return user type after prompting the user. Result can be 'ADMIN' or
- '150'."
- (format t "Admin log [a] or 150 log [1]?")
- (let ((result (if (double-choice #\a #\1)
- "Admin"
- "150")))
- (if (ask (concatenate 'string "Confirm " result " log? (y or n)"))
- result
- (ask-user-type))))
- (defun split-string (string separator)
- "Returns a list of fields in STRING divided by SEPARATOR."
- (loop for i = 0 then (1+ j)
- as j = (position separator string :start i)
- collect (subseq string i j)
- while j))
- (defun ask-date ()
- (format t "Search for which date? (yyyy-mm-dd) (optional) ")
- (let ((date (read-line)))
- (cond
- ((not (validate-date date))
- (ask-date))
- ((null date)
- nil)
- (t
- date))))
- (defun validate-date (date)
- "Return `t' if DATE is in a valid date format: yyyy-mm-dd."
- (let ((tokens (split-string date #\-)))
- (if (or (/= (length (first tokens)) 4)
- (/= (length (second tokens)) 2)
- (/= (length (third tokens)) 2))
- nil
- t)))
|