mirror of
ssh://git.janware.com/janware/proj/jw-pkg
synced 2026-04-25 09:35:54 +02:00
App: Support --verbose
Add the --verbose global option, which is made available as the App.verbose property. Some functions still take a verbose parameter, but the type of these parameters is converted from bool to bool|None. The idea is that, if they are None, their verbosity falls back to the global default. Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
525fa34387
commit
67a2931f5e
5 changed files with 28 additions and 11 deletions
|
|
@ -212,6 +212,7 @@ class App(Base):
|
|||
# -- Members without default values
|
||||
self.__opt_os: str|None = None
|
||||
self.__opt_interactive: bool|None = None
|
||||
self.__opt_verbose: bool|None = None
|
||||
self.__top_name: str|None = None
|
||||
self.__os_release: str|None = None
|
||||
self.__distro_id: str|None = None
|
||||
|
|
@ -238,6 +239,7 @@ class App(Base):
|
|||
parser.add_argument('-O', '--os', default = None, help='Target operating system')
|
||||
parser.add_argument('--distro-id', default=None, help='Distribution ID (default is taken from /etc/os-release)')
|
||||
parser.add_argument('--interactive', choices=['true', 'false', 'auto'], default='true', help="Wait for user input or try to proceed unattended")
|
||||
parser.add_argument('--verbose', action='store_true', default=False, help="Be verbose on stderr about what's being done on the distro level")
|
||||
|
||||
async def _run(self, args: argparse.Namespace) -> None:
|
||||
self.__opt_os = args.os
|
||||
|
|
@ -265,11 +267,17 @@ class App(Base):
|
|||
self.__opt_interactive = sys.stdin.isatty()
|
||||
return self.__opt_interactive
|
||||
|
||||
@property
|
||||
def verbose(self) -> bool:
|
||||
if self.__opt_verbose is None:
|
||||
self.__opt_verbose = self.args.verbose
|
||||
return self.__opt_verbose
|
||||
|
||||
@property
|
||||
def exec_context(self) -> str:
|
||||
if self.__exec_context is None:
|
||||
from .lib.ec.Local import Local
|
||||
self.__exec_context = Local(interactive=self.interactive)
|
||||
self.__exec_context = Local(interactive=self.interactive, verbose_default=self.verbose)
|
||||
return self.__exec_context
|
||||
|
||||
@property
|
||||
|
|
|
|||
|
|
@ -12,7 +12,6 @@ class CmdRebootRequired(Cmd): # export
|
|||
|
||||
def add_arguments(self, parser: ArgumentParser) -> None:
|
||||
super().add_arguments(parser)
|
||||
parser.add_argument('--verbose', default=False, action='store_true', help='Be chatty about the check')
|
||||
|
||||
async def _run(self, args: Namespace) -> None:
|
||||
return await self.distro.reboot_required(verbose=args.verbose)
|
||||
return await self.distro.reboot_required()
|
||||
|
|
|
|||
|
|
@ -82,7 +82,9 @@ class Distro(abc.ABC):
|
|||
async def _reboot_required(self, verbose: bool) -> bool:
|
||||
pass
|
||||
|
||||
async def reboot_required(self, verbose: bool=False) -> bool:
|
||||
async def reboot_required(self, verbose: bool|None=None) -> bool:
|
||||
if verbose is None:
|
||||
verbose = self.ctx.verbose_default
|
||||
return await self._reboot_required(verbose=verbose)
|
||||
|
||||
# -- select
|
||||
|
|
|
|||
|
|
@ -16,15 +16,19 @@ class ExecContext(abc.ABC):
|
|||
self.__verbose_default = verbose_default
|
||||
assert verbose_default is not None
|
||||
|
||||
def _verbose(self, verbose: bool|None):
|
||||
def _verbose(self, verbose: bool|None) -> bool:
|
||||
if verbose is not None:
|
||||
return verbose
|
||||
return self.__verbose_default
|
||||
|
||||
@property
|
||||
def interactive(self):
|
||||
def interactive(self) -> bool:
|
||||
return self.__interactive
|
||||
|
||||
@property
|
||||
def verbose_default(self) -> bool:
|
||||
return self.__verbose_default
|
||||
|
||||
@abc.abstractmethod
|
||||
async def _run(self, *args, **kwargs) -> Result:
|
||||
pass
|
||||
|
|
|
|||
|
|
@ -31,14 +31,18 @@ def pretty_cmd(cmd: list[str], wd=None):
|
|||
ret += f' in {wd}'
|
||||
return ret
|
||||
|
||||
# See ec.Local.run() for what this function does
|
||||
async def run_cmd(*args, ec: ExecContext|None=None, **kwargs) -> tuple[str|bytes|None, str|bytes|None]:
|
||||
# See ExecContext.run() for what this function does
|
||||
async def run_cmd(*args, ec: ExecContext|None=None, verbose: bool|None=None, **kwargs) -> tuple[str|bytes|None, str|bytes|None]:
|
||||
if verbose is None:
|
||||
verbose = False if ec is None else ec.verbose_default
|
||||
if ec is None:
|
||||
from .ec.Local import Local
|
||||
ec = Local()
|
||||
return await ec.run(*args, **kwargs)
|
||||
ec = Local(verbose_default=verbose)
|
||||
return await ec.run(verbose=verbose, *args, **kwargs)
|
||||
|
||||
async def run_curl(args: list[str], parse_json: bool=True, wd=None, throw=None, verbose=False, cmd_input=None, ec: ExecContext|None=None) -> dict|str: # export
|
||||
async def run_curl(args: list[str], parse_json: bool=True, wd=None, throw=None, verbose=None, cmd_input=None, ec: ExecContext|None=None) -> dict|str: # export
|
||||
if verbose is None:
|
||||
verbose = False if ec is None else ec.verbose_default
|
||||
cmd = ['curl']
|
||||
if not verbose:
|
||||
cmd.append('-s')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue