lcmlog.lisp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. (defpackage lcmlog
  2. (:use :cl))
  3. (in-package :lcmlog)
  4. (defconstant *help*
  5. (concatenate 'string "lcmlog: client side of a tool for reading and writing the lcm staff logs.\n\n"
  6. "Usage: lcmlog [option]"
  7. "Options:\n"
  8. "\t-g\tMethod GET: print all the logs that meet the required criteria\n"
  9. "\t-p\tMethod POST: add a log to the database\n"
  10. "\t-u\tMethod UPDATE: regenerate the internal server database\n"
  11. "\t-h\tShow this help\n")
  12. "Program help message.")
  13. (defconstant *usage*
  14. (concatenate 'string "Usage: lcmlog [-g|-p|-u]"
  15. "Use -h option to show the help message\n")
  16. "Program usage message.")
  17. (defconstant editor
  18. (let (env-editor (getenv "EDITOR"))
  19. (if (null env-editor)
  20. "nano"
  21. env-editor))
  22. "Program to use to edit the log.")
  23. (defun get ()
  24. "Returns a string containing the right headers for the get method."
  25. (let ((user-type (ask-user-type))
  26. (date (ask-date)))
  27. nil))
  28. (defun double-choice (first second)
  29. "Return `t' if the user inputs FIRST, `nil' if the user inputs
  30. SECOND."
  31. (let ((c (read-char)))
  32. (cond
  33. ((char= c first)
  34. t)
  35. ((char= c #\Newline)
  36. (format t (concatenate 'string "Answer " (string first) " or " (string second) " "))
  37. (double-choice first second))
  38. (t
  39. nil))))
  40. (defun ask (prompt)
  41. "Ask a question, assuming positive answer by default."
  42. (format t prompt)
  43. (double-choice #\y #\n))
  44. (defun ask-user-type ()
  45. "Return user type after prompting the user. Result can be 'ADMIN' or
  46. '150'."
  47. (format t "Admin log [a] or 150 log [1]?")
  48. (let ((result (if (double-choice #\a #\1)
  49. "Admin"
  50. "150")))
  51. (if (ask (concatenate 'string "Confirm " result " log? (y or n)"))
  52. result
  53. (ask-user-type))))
  54. (defun split-string (string separator)
  55. "Returns a list of fields in STRING divided by SEPARATOR."
  56. (loop for i = 0 then (1+ j)
  57. as j = (position separator string :start i)
  58. collect (subseq string i j)
  59. while j))
  60. (defun ask-date ()
  61. (format t "Search for which date? (yyyy-mm-dd) (optional) ")
  62. (let ((date (read-line)))
  63. (cond
  64. ((not (validate-date date))
  65. (ask-date))
  66. ((null date)
  67. nil)
  68. (t
  69. date))))
  70. (defun validate-date (date)
  71. "Return `t' if DATE is in a valid date format: yyyy-mm-dd."
  72. (let ((tokens (split-string date #\-)))
  73. (if (or (/= (length (first tokens)) 4)
  74. (/= (length (second tokens)) 2)
  75. (/= (length (third tokens)) 2))
  76. nil
  77. t)))