Commits

Steve Losh committed 6d52346

cli: Add the create command.

  • Participants
  • Parent commits 28c18a2

Comments (0)

Files changed (2)

File lib/gorilla/guts/cli.py

-import os, sys
+from __future__ import with_statement
+
+import operator, os, sys
+from gorilla.guts import util
 from gorilla.guts.catalog import Card, all_cards
 from gorilla.turnstile import turnstile
 
         ui.write('gorilla knows about %d packages\n' % len(list(all_cards())))
 
 def _handle_create(ui, **opts):
-    pass
+    if len(opts['args']) > 1:
+        ui.error('Slow down!  One package at a time!\n')
+        return
+    elif not opts['args']:
+        name = ui.prompt('Enter the name of the package/project:')
+    else:
+        name = opts['args'][0]
+    
+    name = name.lower()
+    filename = name.replace('-', '_').replace('.', '') + '.py'
+    
+    catalog = os.path.join(util.GORILLA_PATH, 'catalog')
+    template = os.path.join(catalog, 'template')
+    dest = os.path.join(catalog, filename)
+    
+    if name in map(operator.attrgetter('name'), all_cards()):
+        ui.error('Gorilla already seems to know about that package.\n')
+        return
+    
+    ui.write('\nCreating %s ...\n' % dest)
+    
+    import shutil, subprocess
+    editor = os.getenv('EDITOR') or 'vim'
+    command = '%s %s' % (editor, dest)
+    
+    shutil.copyfile(template, dest)
+    error = subprocess.call(command, shell=True)
+    
+    with open(dest, 'r') as df:
+        with open(template, 'r') as tf:
+            saved = df.readlines() != tf.readlines()
+    
+    if saved and not error:
+        ui.write('Card saved.\nRun "gorilla install %s" to install.\n' % name)
+    else:
+        if error:
+            ui.error('The card was not successfully saved.\n')
+        else:
+            ui.error('The template card was not edited.\n')
+        ui.error('Edit %s and run "gorilla install %s"\n' % (dest, name))
+        ui.error('Alternately, delete %s and forget about it.\n'% dest)
 
 def _handle_submit(ui, **opts):
     pass
 }
 
 _interface = {
+    'create': {
+        'aliases': ['new'],
+        'handler': _handle_create,
+        'options': {},
+        'help': 'create a card for a package',
+        'usage': '%s create [PACKAGE]',
+    },
     'home': {
         'aliases': [],
         'handler': _handle_home,

File lib/gorilla/guts/util.py

 class InvalidTargetDirectoryName(Exception):
     pass
 
-
 def upto(p, to):
     last = ''
     while last != to: