mirror of
ssh://git.janware.com/janware/proj/jw-pkg
synced 2026-04-28 13:55:24 +02:00
This is a code maintenance commit: some run_xxx() helper functions take a string, some a list, and some just digest all arguments and pass them on as a list to exec() to be executed. That's highly inconsistent. This commit changes that to list-only. Except for the run_cmd() method of SSHClient, which is still run as a shell method, because, erm, it's a shell. Might be changed in the future for consistency reasons. Signed-off-by: Jan Lindemann <jan@janware.com>
55 lines
2.2 KiB
Python
55 lines
2.2 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
import re, os
|
|
from argparse import Namespace, ArgumentParser
|
|
from urllib.parse import urlparse
|
|
|
|
from ...lib.log import *
|
|
from ...lib.util import run_cmd
|
|
from ..Cmd import Cmd
|
|
from ..CmdProjects import CmdProjects
|
|
|
|
class CmdGetAuthInfo(Cmd): # export
|
|
|
|
def __init__(self, parent: CmdProjects) -> None:
|
|
super().__init__(parent, '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)
|
|
parser.add_argument('--remote-base', help='Show remote base URL', action='store_true', default=False)
|
|
|
|
async def _run(self, args: Namespace) -> None:
|
|
keys = ['username', 'password']
|
|
|
|
# --- Milk jw-pkg repo
|
|
jw_pkg_dir = self.app.find_dir('jw-pkg', pretty=False)
|
|
if not os.path.isdir(jw_pkg_dir + '/.git'):
|
|
log(DEBUG, f'jw-pkg directory is not a Git repo: {jw_pkg_dir}')
|
|
return
|
|
remotes, stderr = await run_cmd(['git', '-C', jw_pkg_dir, 'remote', '-v'])
|
|
result: dict[str, str] = {}
|
|
for line in remotes.splitlines():
|
|
name, url, typ = re.split(r'\s+', line)
|
|
if name == 'origin' and typ in ['(pull)', '(fetch)']: # TODO: Use other remotes, too?
|
|
parsed = urlparse(url)
|
|
for key in keys:
|
|
result[key] = getattr(parsed, key)
|
|
base = parsed.geturl()
|
|
base = re.sub(r'/jw-pkg', '', base)
|
|
base = re.sub(r'/proj$', '', base)
|
|
result['remote_base'] = base
|
|
break
|
|
|
|
# --- Print results
|
|
for key, val in result.items():
|
|
if getattr(args, key, None) != True:
|
|
continue
|
|
if val is None:
|
|
continue
|
|
if args.only_values:
|
|
print(val)
|
|
continue
|
|
print(f'{key}="{val}"')
|