Skip to content

Commit 725f235

Browse files
committed
🚧 configuration loop can now configure custom services
Signed-off-by: Guyzmo <[email protected]>
1 parent 3430cf0 commit 725f235

File tree

4 files changed

+122
-19
lines changed

4 files changed

+122
-19
lines changed

git_repo/repo.py

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -503,21 +503,54 @@ def loop_input(*args, method=input, **kwarg):
503503
return out
504504

505505
def setup_service(service):
506+
new_conf = dict(
507+
fqdn=None,
508+
remote=None,
509+
)
506510
conf = service.get_config(self.config)
507511
if 'token' in conf:
508512
raise Exception('A token has been generated for this service. Please revoke and delete before proceeding.')
509513

514+
print('Is your service self-hosted?')
515+
if 'y' in input(' [yN]> ').lower():
516+
new_conf['type'] = service.name
517+
print('What name do you want to give this service?')
518+
new_conf['name'] = input('[{}]> '.format(service.name))
519+
new_conf['command'] = new_conf['name']
520+
service.name, service.command = new_conf['name'], new_conf['command']
521+
print('Enter the service\'s domain name:')
522+
new_conf['fqdn'] = input('[{}]> '.format(service.fqdn))
523+
print('Enter the service\'s port:')
524+
new_conf['port'] = input('[443]> ') or 443
525+
print('Are you connecting using HTTPS? (you should):')
526+
if 'n' in input(' [Yn]> ').lower():
527+
new_conf['scheme'] = 'http'
528+
else:
529+
new_conf['scheme'] = 'https'
530+
print('Do you need to use an insecure connection? (you shouldn\'t):')
531+
new_conf['insecure'] = 'y' in input(' [yN]> ').lower()
532+
service.session_insecure = new_conf['insecure']
533+
if not new_conf['insecure']:
534+
print('Do you want to setup the path to custom certificate?:')
535+
if 'y' in input(' [yN]> ').lower():
536+
new_conf['server-cert'] = loop_input('/path/to/certbundle.pem []> ')
537+
service.session_certificate = new_conf['server-cert']
538+
539+
service.fqdn = new_conf['fqdn']
540+
service.port = new_conf['port']
541+
service.scheme = new_conf['scheme']
542+
510543
print('Please enter your credentials to connect to the service:')
511544
username = loop_input('username> ')
512545
password = loop_input('password> ', method=getpass)
513546

514-
token = service.get_auth_token(username, password, prompt=loop_input)
547+
new_conf['token'] = service.get_auth_token(username, password, prompt=loop_input)
515548
print('Great! You\'ve been identified 🍻')
516549

517550
print('Do you want to give a custom name for this service\'s remote?')
518551
if 'y' in input(' [yN]> ').lower():
519552
print('Enter the remote\'s name:')
520-
loop_input('[{}]> '.format(service.name))
553+
new_conf['remote'] = loop_input('[{}]> '.format(service.name))
521554

522555
print('Do you want to configure a git alias?')
523556
print('N.B.: instead of typing `git repo {0}` you\'ll be able to type `git {0}`'.format(service.command))
@@ -526,7 +559,7 @@ def setup_service(service):
526559
else:
527560
set_alias = True
528561

529-
service.store_config(self.config, token=token)
562+
service.store_config(self.config, **new_conf)
530563
if set_alias:
531564
service.set_alias(self.config)
532565

git_repo/services/service.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ class RepositoryService:
6161

6262
config_options = [
6363
'type', 'token', 'alias', 'fqdn', 'remote',
64-
'port', 'scheme', 'insecure',
64+
'port', 'scheme', 'insecure', 'name', 'command',
65+
'server-cert'
6566
]
6667

6768
@classmethod
@@ -88,7 +89,7 @@ def get_config(cls, config):
8889
@classmethod
8990
def store_config(cls, config, **kwarg):
9091
with git_config.GitConfigParser(config, read_only=False) as config:
91-
section = 'gitrepo "{}"'.format(cls.name)
92+
section = 'gitrepo "{}"'.format(kwarg.get('name', cls.name))
9293
for option, value in kwarg.items():
9394
if option not in cls.config_options:
9495
raise ArgumentError('Option {} is invalid and cannot be setup.'.format(option))
@@ -109,13 +110,13 @@ def get_service(cls, repository, command):
109110
:return: instance for using the service
110111
'''
111112
if not repository:
112-
config = git_config.GitConfigParser(self.get_config_path())
113+
config = git_config.GitConfigParser(cls.get_config_path())
113114
else:
114115
config = repository.config_reader()
115116
target = cls.command_map.get(command, command)
116117
conf_section = list(filter(lambda n: 'gitrepo' in n and target in n, config.sections()))
117118

118-
http_section = [config._sections[scheme] for scheme in ('http', 'https')]
119+
http_section = [config._sections[scheme] for scheme in ('http', 'https') if scheme in config.sections()]
119120

120121
# check configuration constraints
121122
if len(conf_section) == 0:
@@ -146,7 +147,7 @@ def get_service(cls, repository, command):
146147
def get_auth_token(cls, login, password, prompt=None):
147148
raise NotImplementedError
148149

149-
def load_configuration(self, c, hc):
150+
def load_configuration(self, c, hc=[]):
150151
CONFIG_TRUE=('on', 'true', 'yes', '1')
151152
# if there's a configuration file, update the names accordingly
152153
if c:
@@ -167,19 +168,18 @@ def load_configuration(self, c, hc):
167168
c.get('privatekey', None))))
168169
self._alias = c.get('alias', self.name)
169170

170-
self.fqdn, port = c.get('fqdn', self.fqdn).split(':')
171-
self.port = port or None
171+
self.fqdn = c.get('fqdn', self.fqdn)
172+
self.scheme = c.get('scheme', 'https')
173+
self.port = c.get('port', '443')
172174

173-
self.default_create_private = c.get('default-create-private', 'false').lower() in CONFIG_TRUE
174-
self.ssh_url = c.get('ssh-url', None) or self.fqdn
175+
self.default_create_private = c.get('default-create-private', 'n').lower() in CONFIG_TRUE
176+
self.ssh_url = c.get('ssh-url', self.fqdn)
175177

176178
self.session_insecure = c.get('insecure', 'false').lower() in CONFIG_TRUE
177179
self.session_certificate = c.get('certificate', None)
178180
self.session_proxy = {cf['__name__']: cf['proxy'] for cf in hc if cf.get('proxy', None)}
179181

180-
181-
182-
def __init__(self, r=None, c=None, hc=None):
182+
def __init__(self, r=None, c=None, hc=[]):
183183
'''
184184
:param r: git-python repository instance
185185
:param c: configuration data

tests/helpers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def mockup_git(self, namespace, repository, url=None):
4646
class RepositoryMockup(RepositoryService):
4747
name = 'test_name'
4848
command = 'test_command'
49-
fqdn = 'http://example.org'
49+
fqdn = 'example.org'
5050
def __init__(self, *args, **kwarg):
5151
super(RepositoryMockup, self).__init__(*args, **kwarg)
5252
self._did_pull = None
@@ -359,6 +359,7 @@ def main_open(self, repo=None, rc=0, args={}):
359359
return RepositoryService._current._did_open
360360

361361
def main_config(self, target, rc=0, args={}):
362+
self.target = target
362363
assert rc == main(self.setup_args({
363364
'config': True,
364365
'--config': os.path.join(self.tempdir.name, 'gitconfig')

tests/integration/test_main.py

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -576,13 +576,82 @@ def test_request_create__no_repo_slug(self, capsys, caplog):
576576
def test_config(self, capsys, caplog):
577577
import sys, io, getpass
578578
getpass.getpass = input
579-
sys.stdin = io.StringIO('\n'.join(['y', 'user', 'pass', 'y', 'fubar', 'y']))
579+
sys.stdin = io.StringIO('\n'.join(['y', 'n', 'user', 'pass', 'y', 'fubar', 'y']))
580580
#
581581
conf = self.main_config(target='hub', rc=0)
582-
assert ['[gitrepo "github"]\n',
582+
assert sorted(['[gitrepo "github"]\n',
583+
'\tremote = fubar\n',
583584
'\ttoken = user:pass\n',
584585
'[alias]\n',
585-
'\ttest_command = repo test_command\n'] == conf
586+
'\ttest_command = repo test_command\n']) == sorted(conf)
587+
last_conf = conf
588+
589+
sys.stdin = io.StringIO('\n'.join([
590+
'y', 'y', 'frama', 'framagit.org', '', '', '', '', 'user', 'pass', 'y', 'framagit', 'y']))
591+
#
592+
conf = self.main_config(target='lab', rc=0)
593+
assert sorted(last_conf+[
594+
'[gitrepo "frama"]\n',
595+
'\tfqdn = framagit.org\n',
596+
'\tremote = framagit\n',
597+
'\ttype = gitlab\n',
598+
'\tname = frama\n',
599+
'\tcommand = frama\n',
600+
'\tport = 443\n',
601+
'\tscheme = https\n',
602+
'\tinsecure = False\n',
603+
'\ttoken = user:pass\n', ]) == sorted(conf)
604+
last_conf = conf
605+
606+
sys.stdin = io.StringIO('\n'.join([
607+
'y', 'y', 'test0r', 'localhost', '8080', 'n', 'user', 'pass', 'y', 'test', 'y']))
608+
#
609+
conf = self.main_config(target='lab', rc=0)
610+
assert sorted(last_conf+[
611+
'[gitrepo "test0r"]\n',
612+
'\tfqdn = localhost\n',
613+
'\tremote = test\n',
614+
'\ttype = gitlab\n',
615+
'\tname = test0r\n',
616+
'\tcommand = test0r\n',
617+
'\tport = 8080\n',
618+
'\tscheme = http\n',
619+
'\ttoken = user:pass\n', ]) == sorted(conf)
620+
last_conf = conf
621+
622+
sys.stdin = io.StringIO('\n'.join([
623+
'y', 'y', 'selfsigned', 'server.local', '8443',
624+
'', '', 'y', '/etc/server.pem', 'user', 'pass', 'y', 'self', 'y']))
625+
#
626+
conf = self.main_config(target='lab', rc=0)
627+
assert sorted(last_conf+['[gitrepo "selfsigned"]\n',
628+
'\tfqdn = server.local\n',
629+
'\tremote = self\n',
630+
'\ttype = gitlab\n',
631+
'\tname = selfsigned\n',
632+
'\tcommand = selfsigned\n',
633+
'\tport = 8443\n',
634+
'\tscheme = https\n',
635+
'\tinsecure = False\n',
636+
'\tserver-cert = /etc/server.pem\n',
637+
'\ttoken = user:pass\n', ]) == sorted(conf)
638+
last_conf = conf
639+
640+
sys.stdin = io.StringIO('\n'.join([
641+
'y', 'y', 'lamer', 'pwnme.com', '',
642+
'', 'y', 'user', 'pass', 'y', 'l', 'y']))
643+
#
644+
conf = self.main_config(target='lab', rc=0)
645+
assert sorted(last_conf+['[gitrepo "lamer"]\n',
646+
'\tfqdn = pwnme.com\n',
647+
'\tremote = l\n',
648+
'\ttype = gitlab\n',
649+
'\tname = lamer\n',
650+
'\tcommand = lamer\n',
651+
'\tport = 443\n',
652+
'\tscheme = https\n',
653+
'\tinsecure = True\n',
654+
'\ttoken = user:pass\n', ]) == sorted(conf)
586655

587656
def test_z_noop(self):
588657
self.main_noop('guyzmo/git-repo', 1)

0 commit comments

Comments
 (0)