cmds.DistroBase: Add class

Move most of CmdDistro into the new class DistroBase, meant to serve
as base class for distro-aware commands other than "distro".

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2026-03-05 10:54:32 +01:00
commit f94a2ac037
2 changed files with 49 additions and 35 deletions

View file

@ -1,48 +1,14 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import re, sys
from argparse import ArgumentParser from argparse import ArgumentParser
from ..App import App from ..App import App
from .Cmd import Cmd as CmdBase from .DistroBase import DistroBase as CmdBase
class CmdDistro(CmdBase): # export class CmdDistro(CmdBase): # export
def __init__(self, parent: App) -> None: def __init__(self, parent: App) -> None:
self.__id = None
super().__init__(parent, 'distro', help="System package manager wrapper") super().__init__(parent, 'distro', help="System package manager wrapper")
self._add_subcommands()
self.__interactive: bool|None = None
@property
def distro_id(self):
if self.__id is None:
if self.app.args.id is not None:
# The distribution ID requested by the command line
self.__id = self.app.args.id
else:
# The ID of the distribution we run on
self.__id = self.app.distro_id
return self.__id
def add_arguments(self, p: ArgumentParser) -> None: def add_arguments(self, p: ArgumentParser) -> None:
super().add_arguments(p) super().add_arguments(p)
p.add_argument('--id', default=None, help='Distribution ID (default is taken from /etc/os-release)')
p.add_argument('--interactive', choices=['true', 'false', 'auto'], default='true', help="Wait for user input or try to proceed unattended")
@property
def interactive(self) -> bool:
if self.__interactive is None:
match self.app.args.interactive:
case 'true':
self.__interactive = True
case 'false':
self.__interactive = False
case 'auto':
self.__interactive = sys.stdin.isatty()
return self.__interactive
async def _run(self, args):
# Missing subcommand
self.parser.print_help()
sys.exit(1)

View file

@ -0,0 +1,48 @@
# -*- coding: utf-8 -*-
import re, sys
from argparse import ArgumentParser
from ..App import App
from .Cmd import Cmd as Base
class DistroBase(Base): # export
def __init__(self, parent: App|Base, name: str, help: str) -> None:
self.__id = None
super().__init__(parent, name, help)
self._add_subcommands()
self.__interactive: bool|None = None
@property
def distro_id(self):
if self.__id is None:
if self.app.args.id is not None:
# The distribution ID requested by the command line
self.__id = self.app.args.id
else:
# The ID of the distribution we run on
self.__id = self.app.distro_id
return self.__id
def add_arguments(self, p: ArgumentParser) -> None:
super().add_arguments(p)
p.add_argument('--id', default=None, help='Distribution ID (default is taken from /etc/os-release)')
p.add_argument('--interactive', choices=['true', 'false', 'auto'], default='true', help="Wait for user input or try to proceed unattended")
@property
def interactive(self) -> bool:
if self.__interactive is None:
match self.app.args.interactive:
case 'true':
self.__interactive = True
case 'false':
self.__interactive = False
case 'auto':
self.__interactive = sys.stdin.isatty()
return self.__interactive
async def _run(self, args):
# Missing subcommand
self.parser.print_help()
sys.exit(1)