CmdGetAuthInfo: Code beautification

Fix clumsy code printing auth info.

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2025-11-20 10:26:16 +01:00
commit 9d06e0bf2e

View file

@ -19,13 +19,15 @@ class CmdGetAuthInfo(Cmd): # export
parser.add_argument('--password', help='Show password', action='store_true', default=False) parser.add_argument('--password', help='Show password', action='store_true', default=False)
def _run(self, args: Namespace) -> None: def _run(self, args: Namespace) -> None:
keys = ['username', 'password']
# --- Milk jw-build repo
jw_build_dir = self.app.proj_dir('jw-build') jw_build_dir = self.app.proj_dir('jw-build')
if not os.path.isdir(jw_build_dir + '/.git'): if not os.path.isdir(jw_build_dir + '/.git'):
self.app.debug(f'jw-build directory is not a Git repo: {jw_build_dir}') self.app.debug(f'jw-build directory is not a Git repo: {jw_build_dir}')
return return
remotes = run_cmd(['git', '-C', jw_build_dir, 'remote', '-v']) remotes = run_cmd(['git', '-C', jw_build_dir, 'remote', '-v'])
result: dict[str, str] = {} result: dict[str, str] = {}
keys = ['username', 'password']
for line in remotes.split('\n'): for line in remotes.split('\n'):
if re.match(r'^\s*$', line): if re.match(r'^\s*$', line):
continue continue
@ -35,11 +37,14 @@ class CmdGetAuthInfo(Cmd): # export
for key in keys: for key in keys:
result[key] = getattr(parsed, key) result[key] = getattr(parsed, key)
break break
for key in keys:
if key in result and getattr(args, key, None) == True: # --- Print results
if key is None: for key, val in result.items():
continue if getattr(args, key, None) != True:
if args.only_values: continue
print(result[key]) if val is None:
continue continue
print(f'{key}="{result[key]}"') if args.only_values:
print(val)
continue
print(f'{key}="{val}"')