lib.util.run_cmd(): Rewrite it to be async

run_cmd() is synchronous. Now that all commands are asynchronous, we
can await it, so rewrite it to be asynchronous, too.

Other changes:

  - Make it return stderr as well in case its needed

  - Drop into a pseuto-tty if
    - cmd_input == "mode:interactive" or
    - cmd_input == "mode:auto" and stdin is a TTY

  - Add argument env, defaulting to None. If it's a dict, it will be
    the environment the command is run in

This entails making all functions using run_cmd() async, too,
including run_curl(), get_username() and get_password().

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2026-01-27 16:20:57 +01:00
commit 4274a71c62
4 changed files with 162 additions and 44 deletions

View file

@ -29,7 +29,7 @@ class CmdGetAuthInfo(Cmd): # export
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 = run_cmd(['git', '-C', jw_pkg_dir, 'remote', '-v'])
remotes, stderr = await run_cmd(['git', '-C', jw_pkg_dir, 'remote', '-v'])
result: dict[str, str] = {}
for line in remotes.split('\n'):
if re.match(r'^\s*$', line):

View file

@ -25,10 +25,10 @@ class CmdListRepos(Cmd): # export
from urllib.parse import urlparse
url = urlparse(args.base_url)
askpass_env=['GIT_ASKPASS', 'SSH_ASKPASS']
username = get_username(args=args, url=args.base_url, askpass_env=askpass_env)
username = await get_username(args=args, url=args.base_url, askpass_env=askpass_env)
password = None
if username is not None:
password = get_password(args=args, url=args.base_url, askpass_env=askpass_env)
password = await get_password(args=args, url=args.base_url, askpass_env=askpass_env)
match url.scheme:
case 'ssh':
if re.match(r'ssh://.*git\.janware\.com/', args.base_url):
@ -39,7 +39,7 @@ class CmdListRepos(Cmd): # export
if password is not None:
ssh.set_password(password)
cmd = f'/opt/jw-pkg/bin/git-srv-admin.sh -u {args.from_user} -j list-personal-projects'
out = ssh.run_cmd(cmd)
out = await ssh.run_cmd(cmd)
print(out)
return
case 'https':
@ -55,7 +55,7 @@ class CmdListRepos(Cmd): # export
cmd_input = (f'-u {username}:{password}').encode('utf-8')
curl_args.extend(['-K-'])
curl_args.append(f'https://api.github.com/users/{args.from_user}/repos')
repos = run_curl(curl_args, cmd_input=cmd_input)
repos = await run_curl(curl_args, cmd_input=cmd_input)
for repo in repos:
print(repo['name'])
return
@ -71,7 +71,7 @@ class CmdListRepos(Cmd): # export
curl_args.extend([
f'https://{url.hostname}/code/api/v1/orgs/{args.from_user}/repos'
])
repos = run_curl(curl_args, cmd_input=cmd_input)
repos = await run_curl(curl_args, cmd_input=cmd_input)
for repo in repos:
print(repo['name'])
return