aboutsummaryrefslogtreecommitdiff
path: root/app/search.lisp
blob: f54cfa2bc1d00dd0ad0fbb72400e7208a939fda3 (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
77
78
79
80
81
82
83
84
85
86
87
;;;; 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)

   ;; TODO
   ;; (cli:make-option
   ;;  :flag
   ;;  :short-name #\c
   ;;  :long-name "cache"
   ;;  :description "restrict search to what is already in the local cache"
   ;;  :env-vars '("OL_CACHE_ONLY")
   ;;  :key :cache)
   

   ))

(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)))

      ;; 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 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"
   :usage "[options] [term ...]"
   :options (search/options)
   :handler #'search/handler
   :examples +search/examples+))