aboutsummaryrefslogtreecommitdiff
path: root/app/search.lisp
blob: 015a749a3e038031462159aa20cf023d56cdc353 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
;;;; search.lisp -- search function interface for oneliners wiki cli

(in-package :oneliners.cli.app )

(defun search/options ()
  "Returns a list of options for the search command."
  (list
   (cli:make-option
    :integer
    :short-name #\l
    :long-name "limit"
    :description "number of results to return"
    :initial-value 10
    :key :limit)
   (cli:make-option
    :flag
    :short-name #\f
    :long-name "flagged"
    :description "only flagged oneliners are returned"
    :key :flagged)
   (cli:make-option
    :flag
    :short-name #\F
    :long-name "no-flagged"
    :description "only unflagged oneliners are returned"
    :key :no-flagged)
   (cli:make-option
    :flag
    :short-name #\n
    :long-name "newest"
    :description "the newest matching oneliners are returned"
    :key :newest)))

(defun search/handler (cmd)
  "Handler function for the search command. "
  (let ((args
          (cli:command-arguments cmd))
        (limit
          (cli:getopt cmd :limit)))
    (cond
      (args
       ;; if there are search terms, call the main search function
       (ol:search-for-oneliners
        args
        limit
        (cli:getopt cmd :no-flagged)
        (cli:getopt cmd :flagged)
        (cli:getopt cmd :newest)))

      ;; if no search terms, but --newest or -n
      ((cli:getopt cmd :newest)
       (ol::newest-oneliners limit))

      ;; no args, but --flagged or -f
      ((cli:getopt cmd :flagged)
       (ol::all-flagged-oneliners limit))

      (t
       (cli::print-usage-and-exit cmd t)))))


(defparameter +search/examples+
  '(("Search for oneliners involving grep and awk" .
     "ol search grep awk")
    ("Limit search to flagged oneliners (useful for contributors)" .
     "ol search --flagged grep awk")))


(defun search/command ()
  "Creates a new command for interfacing with the search api."
  (cli:make-command
   :name "search"
   :description "search for oneliners with provided tags"
   :options (search/options)
   :handler #'search/handler
   :examples +search/examples+))