Created
March 7, 2014 22:25
-
-
Save clarete/9421480 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import argparse | |
import gitlab3 | |
import io | |
import os | |
import sys | |
def parse_command_line_arguments(args): | |
parser = argparse.ArgumentParser(description='Manage GitLab deploy keys') | |
subparsers = parser.add_subparsers() | |
parser.add_argument( | |
'-u', '--url', required=True, | |
help='URL of the GitLab instance you want to access') | |
parser.add_argument( | |
'-t', '--token', required=True, | |
help='GitLab token used to authenticate the client') | |
# Add the list subcommand | |
subparser = subparsers.add_parser('list', help='List deploy keys from a given project in gitlab') | |
subparser.set_defaults(command='list') | |
subparser.add_argument( | |
'-p', '--project-name', metavar='PROJECT', required=True, | |
help='Name of the project') | |
# Add the add subcommand | |
subparser = subparsers.add_parser('add', help='Manage GitLab deploy keys') | |
subparser.set_defaults(command='add') | |
subparser.add_argument( | |
'-p', '--project-name', metavar='PROJECT', required=True, | |
help='Name of the project') | |
subparser.add_argument( | |
'-k', '--key-title', required=True, | |
help='Title of the key you want to upload') | |
subparser.add_argument( | |
'-f', '--key-file', required=True, | |
help='A valid SSH key file') | |
return parser.parse_args(args) | |
def main(args=sys.argv[1:]): | |
options = parse_command_line_arguments(args) | |
# The client and commands we support | |
client = gitlab3.GitLab(options.url, options.token) | |
def list_keys(options): | |
project = client.find_project(name=options.project_name) | |
for key in project.deploy_keys(): | |
print(key) | |
def add_key(options): | |
key = io.open(os.path.abspath(os.path.expanduser(options.key_file))).read() | |
project = client.find_project(name=options.project_name) | |
project.add_deploy_key(options.key_title, key.strip()) | |
print 'Done adding the key {0} to the project {1}'.format( | |
options.key_title, options.project_name) | |
command = { | |
'list': list_keys, | |
'add': add_key, | |
}[options.command](options) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment