Commits

Steve Losh committed 0728be5

cli: Add the search command.

Comments (0)

Files changed (1)

lib/gorilla/guts/cli.py

     ui.write('\n'.join(sorted(card_names)) + '\n')
 
 def _handle_search(ui, **opts):
-    pass
+    args = opts.pop('args')
+    if not args:
+        return _handle_list(ui)
+    
+    find = [f.replace('-', '_').lower() for f in args]
+    check = any if opts.pop('any') else all
+    
+    catalog_path = os.path.join(util.GORILLA_PATH, 'catalog')
+    filenames = os.listdir(catalog_path)
+    card_files = filter(
+        lambda f: not f.startswith('_') and f.endswith('.py'),
+        filenames
+    )
+    cards = [Card(f[:-3]) for f in card_files]
+    
+    for card in cards:
+        content = '%s %s' % (card.name, card.description)
+        content = content.replace('-', '_').lower()
+        if check(map(lambda f: f in content, find)):
+            ui.write('%s\n' % card.name)
 
 def _handle_info(ui, **opts):
     if opts['args']:
         'help': 'specify your preferred VCS',
     }
 }
+_options_search = {
+    'any': {
+        'short': 'a',
+        'default': False,
+        'help': 'find packages that match any given STRING',
+    }
+}
 
 _interface = {
     'init': {
         'purpose': 'list available packages',
         'usage': '%s list',
     },
+    'search': {
+        'aliases': ['find'],
+        'handler': _handle_search,
+        'options': _options_search,
+        'purpose': 'search for packages',
+        'usage': '%s search STRING [STRING ...]',
+    },
     'info': {
         'aliases': [],
         'handler': _handle_info,