From 1187c10c86eb8b3308305db1be62ad01875e8ab9 Mon Sep 17 00:00:00 2001 From: Jan Lindemann Date: Wed, 19 Nov 2025 08:14:43 +0100 Subject: [PATCH] build.cmds.GetAuthInfo: Add module Add command get-auth-info to jw-projects.py. It is meant to retrieve JANWARE_USER from an already cloned jw-build tree. Signed-off-by: Jan Lindemann --- src/python/jw/build/cmds/CmdGetAuthInfo.py | 38 ++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/python/jw/build/cmds/CmdGetAuthInfo.py diff --git a/src/python/jw/build/cmds/CmdGetAuthInfo.py b/src/python/jw/build/cmds/CmdGetAuthInfo.py new file mode 100644 index 00000000..34ba32c3 --- /dev/null +++ b/src/python/jw/build/cmds/CmdGetAuthInfo.py @@ -0,0 +1,38 @@ +# -*- coding: utf-8 -*- + +import re +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 "="', 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: + remotes = run_cmd(['git', '-C', self.app.proj_dir('jw-build'), 'remote', '-v']) + 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: + if key in result and getattr(args, key, None): + if not args.only_values: + print(f'{key}=', end='') + print(result[key])