2025-11-19 08:14:43 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
2025-11-19 09:15:13 +01:00
|
|
|
import re, os
|
2025-11-19 08:14:43 +01:00
|
|
|
from argparse import Namespace, ArgumentParser
|
|
|
|
|
from urllib.parse import urlparse
|
|
|
|
|
|
|
|
|
|
from ..Cmd import Cmd
|
|
|
|
|
from ..lib.util import run_cmd
|
|
|
|
|
|
|
|
|
|
class CmdGetAuthInfo(Cmd): # export
|
|
|
|
|
|
|
|
|
|
def __init__(self) -> None:
|
|
|
|
|
super().__init__('get-auth-info', help='Try to retrieve authentication information from the source tree')
|
|
|
|
|
|
|
|
|
|
def add_arguments(self, parser: ArgumentParser) -> None:
|
|
|
|
|
super().add_arguments(parser)
|
|
|
|
|
parser.add_argument('--only-values', help='Don\'t prefix values by "<field-name>="', action='store_true', default=False)
|
|
|
|
|
parser.add_argument('--username', help='Show user name', action='store_true', default=False)
|
|
|
|
|
parser.add_argument('--password', help='Show password', action='store_true', default=False)
|
|
|
|
|
|
|
|
|
|
def _run(self, args: Namespace) -> None:
|
2025-11-19 09:15:13 +01:00
|
|
|
jw_build_dir = self.app.proj_dir('jw-build')
|
|
|
|
|
if not os.path.isdir(jw_build_dir + '/.git'):
|
|
|
|
|
self.app.debug(f'jw-build directory is not a Git repo: {jw_build_dir}')
|
|
|
|
|
return
|
|
|
|
|
remotes = run_cmd(['git', '-C', jw_build_dir, 'remote', '-v'])
|
2025-11-19 08:14:43 +01:00
|
|
|
result: dict[str, str] = {}
|
|
|
|
|
keys = ['username', 'password']
|
|
|
|
|
for line in remotes.split('\n'):
|
|
|
|
|
if re.match(r'^\s*$', line):
|
|
|
|
|
continue
|
|
|
|
|
name, url, typ = re.split(r'\s+', line)
|
|
|
|
|
if name == 'origin' and typ == '(push)':
|
|
|
|
|
parsed = urlparse(url)
|
|
|
|
|
for key in keys:
|
|
|
|
|
result[key] = getattr(parsed, key)
|
|
|
|
|
break
|
|
|
|
|
for key in keys:
|
2025-11-19 20:00:13 +01:00
|
|
|
if key in result and getattr(args, key, None) == True:
|
|
|
|
|
if key is None:
|
|
|
|
|
continue
|
|
|
|
|
if args.only_values:
|
|
|
|
|
print(result[key])
|
|
|
|
|
continue
|
|
|
|
|
print(f'{key}="{result[key]}"')
|