Commits

Mark Gemmill committed 8fdccf0

added filtering option to list command

Comments (0)

Files changed (2)

+version 0.2.? (February 18, 2013)
+---------------------------------
 
-Branch cli-update (February 11, 2013)
--------------------------------------
+- expanded 'list' command to include filter options:
+    --public -> filter for public repos
+    --private -> filter for private repos
+    --scm -> filter for given repo type
+    --expression -> filter name agains regular expression
+
+
+version 0.2.? (February 11, 2013)
+---------------------------------
 
 - updated command line help options to provide better information
 
 import os
+import re
 import sys
 import getpass
 import argparse
 @password
 def list_command(args):
     response = get_user_repos(args.username, args.password)
+    repo_count = 0
+    filters = []
+    # filter for public and/or private repo
+    filters.append(lambda repo: (args.list_public and not repo.get('is_private', False)) or
+                                (args.list_private and repo.get('is_private', False)) or
+                                (args.list_public == args.list_private))
+    # filter for type of repository
+    filters.append(lambda repo: args.scm == 'all' or repo['scm'] == args.scm)
+    # filter name on regular expression
+    exp = re.compile(args.expression, re.IGNORECASE)
+    filters.append(lambda repo: exp.search(repo['name']))
+
     for repo in response:
-        display_repo_info(repo)
-    print '{0} repositories listed'.format(len(response))
+        if all([filter(repo) for filter in filters]):
+            repo_count += 1
+            display_repo_info(repo)
+
+    print '{0} repositories listed'.format(repo_count)
 
 
 def run():
                             type=str,
                             help='the bitbucket repository name')
 
-    command_names = ('create', 'update', 'delete', 'clone', 'create_from_local', 'pull', 'download', 'list')
+    command_names = ('create', 'update', 'delete', 'clone', 'create_from_local',
+                     'pull', 'download', 'list')
     # SUBPARSER
     subp = p.add_subparsers(title='Commands', metavar='\n  '.join(command_names))
 
     # CREATE COMMAND PARSER
     create_cmd_parser = subp.add_parser('create',
-                            usage=('bitbucket create [-h] [--username USERNAME]\n'
-                                   '                        [--password PASSWORD] [--private | --public]\n'
-                                   '                        [--scm SCM] [--protocol PROTOCOL]\n'
-                                   '                        reponame'),
-                            description='create a new bitbucket repository')
+                        usage=('bitbucket create [-h] [--username USERNAME]\n'
+                               '                        [--password PASSWORD] [--private | --public]\n'
+                               '                        [--scm SCM] [--protocol PROTOCOL]\n'
+                               '                        reponame'),
+                        description='create a new bitbucket repository')
     add_standard_args(create_cmd_parser,
                       ('username',
                        'password',
     #
     list_cmd_parser = subp.add_parser('list',
                             usage=('bitbucket list [-h] [--username USERNAME]\n'
-                                   '                          [--password PASSWORD]'),
+                                   '                      [--password PASSWORD]\n'
+                                   '                      [--public]\n'
+                                   '                      [--private]\n'
+                                   '                      [--scm SCM]\n'
+                                   '                      [--expression EXPRESSION]'),
                             description='list all bitbucket repos')
     add_standard_args(list_cmd_parser,
                       ('username',
                        'password'))
     list_cmd_parser.set_defaults(func=list_command)
+    list_cmd_parser.add_argument('--private', '-c', action='store_true',
+              dest='list_private',
+              default=False,
+              help='list private repositories only.')
+    list_cmd_parser.add_argument('--public ', '-o', action='store_true',
+              dest='list_public',
+              default=False,
+              help='list public repositories only.')
+    list_cmd_parser.add_argument('--scm', '-s', default='all',
+              help='list only the given scm (git|hg)')
+    list_cmd_parser.add_argument('--expression', '-e', default='.*',
+              help='list only repository names matching expression')
 
     try:
         args = p.parse_args()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.