From b3ead042ca748e424f8e61e85f4ff38ba153d7ec Mon Sep 17 00:00:00 2001 From: Guyzmo Date: Wed, 1 Feb 2017 17:13:47 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20more=20configuration=20options?= =?UTF-8?q?=20supported=20for=20config=20command?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - proxy support (using the gitconfig http configuration) - custom certificate CA bundle setting - support of scheme/port settings - custom SSH URI setup - and even a setting to define private repository upon creation kudos to @PyHedgehog to force me into implementing that ☺ Fixes: #107 Fixes: #106 Fixes: #81 Fixes: #88 Signed-off-by: Guyzmo --- git_repo/services/ext/gitlab.py | 5 ++- git_repo/services/service.py | 54 ++++++++++++++++++++++----------- 2 files changed, 41 insertions(+), 18 deletions(-) diff --git a/git_repo/services/ext/gitlab.py b/git_repo/services/ext/gitlab.py index 670e456..b0c5a2c 100644 --- a/git_repo/services/ext/gitlab.py +++ b/git_repo/services/ext/gitlab.py @@ -23,7 +23,10 @@ def __init__(self, *args, **kwarg): super().__init__(*args, **kwarg) def connect(self): - self.gl.ssl_verify = not self.insecure + self.gl.ssl_verify = self.session_certificate or not self.session_insecure + if self.session_proxy: + self.gl.session.proxies.update(self.session_proxy) + self.gl.set_url(self.url_ro) self.gl.set_token(self._privatekey) self.gl.token_auth() diff --git a/git_repo/services/service.py b/git_repo/services/service.py index 6987271..e24a134 100644 --- a/git_repo/services/service.py +++ b/git_repo/services/service.py @@ -59,7 +59,10 @@ class RepositoryService: # this symbol is made available for testing purposes _current = None - config_options = ['type', 'token', 'alias', 'fqdn'] + config_options = [ + 'type', 'token', 'alias', 'fqdn', 'remote', + 'port', 'scheme', 'insecure', + ] @classmethod def get_config_path(cls): @@ -89,7 +92,8 @@ def store_config(cls, config, **kwarg): for option, value in kwarg.items(): if option not in cls.config_options: raise ArgumentError('Option {} is invalid and cannot be setup.'.format(option)) - config.set_value(section, option, value) + if value != None: + config.set_value(section, option, value) @classmethod def set_alias(cls, config): @@ -111,6 +115,8 @@ def get_service(cls, repository, command): target = cls.command_map.get(command, command) conf_section = list(filter(lambda n: 'gitrepo' in n and target in n, config.sections())) + http_section = [config._sections[scheme] for scheme in ('http', 'https')] + # check configuration constraints if len(conf_section) == 0: if not target: @@ -133,25 +139,15 @@ def get_service(cls, repository, command): raise ValueError('Service type {} does not exists.'.format(config['type'])) service = cls.service_map.get(config['type'], cls) - cls._current = service(repository, config) + cls._current = service(repository, config, http_section) return cls._current @classmethod def get_auth_token(cls, login, password, prompt=None): raise NotImplementedError - def __init__(self, r=None, c=None): - ''' - :param r: git-python repository instance - :param c: configuration data - - Build a repository service instance, store configuration and parameters - And launch the connection to the service - ''' - - self.repository = r - self.config = c - + def load_configuration(self, c, hc): + CONFIG_TRUE=('on', 'true', 'yes', '1') # if there's a configuration file, update the names accordingly if c: name = ' '.join(c['__name__'].replace('"', '').split(' ')[1:]) @@ -170,8 +166,32 @@ def __init__(self, r=None, c=None): c.get('private_token', c.get('privatekey', None)))) self._alias = c.get('alias', self.name) - self.fqdn = c.get('fqdn', self.fqdn) - self.insecure = c.get('insecure', 'false').lower() in ('on', 'true', 'yes', '1') + + self.fqdn, port = c.get('fqdn', self.fqdn).split(':') + self.port = port or None + + self.default_create_private = c.get('default-create-private', 'false').lower() in CONFIG_TRUE + self.ssh_url = c.get('ssh-url', None) or self.fqdn + + self.session_insecure = c.get('insecure', 'false').lower() in CONFIG_TRUE + self.session_certificate = c.get('certificate', None) + self.session_proxy = {cf['__name__']: cf['proxy'] for cf in hc if cf.get('proxy', None)} + + + + def __init__(self, r=None, c=None, hc=None): + ''' + :param r: git-python repository instance + :param c: configuration data + + Build a repository service instance, store configuration and parameters + And launch the connection to the service + ''' + + self.repository = r + self.config = c + + self.load_configuration(c, hc) # if service has a repository configured, connect if r: