mirror of
ssh://git.janware.com/janware/proj/jw-pkg
synced 2026-04-24 17:23:36 +02:00
cmds.secrets.CmdCompileTemplates: Support -ogm
Add support for the -o (--owner) -g (--group) -m (--mode) options. They allow to specify a default for compiling templates, but _don't_ override what's in the #conf: specification line in .jw-tmpl or .jw-secret files. Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
1ffac7b365
commit
f3c4a51b85
2 changed files with 36 additions and 6 deletions
|
|
@ -22,11 +22,33 @@ class Cmd(Base): # export
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class Attrs:
|
class Attrs:
|
||||||
|
|
||||||
mode: int | None = None
|
mode: int | None = None
|
||||||
owner: str | None = None
|
owner: str | None = None
|
||||||
group: str | None = None
|
group: str | None = None
|
||||||
conf: str | None = None
|
conf: str | None = None
|
||||||
|
|
||||||
|
def update(self, rhs: Args|None) -> Args:
|
||||||
|
if rhs is not None:
|
||||||
|
if rhs.mode:
|
||||||
|
self.mode = rhs.mode
|
||||||
|
if rhs.owner:
|
||||||
|
self.owner = rhs.owner
|
||||||
|
if rhs.group:
|
||||||
|
self.group = rhs.group
|
||||||
|
if rhs.conf:
|
||||||
|
self.conf = rhs.conf
|
||||||
|
return self
|
||||||
|
|
||||||
|
def emtpy(self) -> bool:
|
||||||
|
if self.mode is not None:
|
||||||
|
return False
|
||||||
|
if self.owner is not None:
|
||||||
|
return False
|
||||||
|
if self.group is not None:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
def __read_key_value_file(self, path: str) -> dict[str, str]:
|
def __read_key_value_file(self, path: str) -> dict[str, str]:
|
||||||
ret: dict[str, str] = {}
|
ret: dict[str, str] = {}
|
||||||
with open(path, "r", encoding="utf-8") as f:
|
with open(path, "r", encoding="utf-8") as f:
|
||||||
|
|
@ -230,17 +252,18 @@ class Cmd(Base): # export
|
||||||
log(NOTICE, f'Removing {path}')
|
log(NOTICE, f'Removing {path}')
|
||||||
os.unlink(path)
|
os.unlink(path)
|
||||||
|
|
||||||
async def _compile_template_files(self, packages: Iterable[str], attrs: str|Attrs=None) -> list[str]:
|
async def _compile_template_files(self, packages: Iterable[str], default_attrs: Attrs) -> list[str]:
|
||||||
|
if default_attrs is None:
|
||||||
|
default_attrs = Attrs()
|
||||||
for target in await self._list_compilation_targets(packages):
|
for target in await self._list_compilation_targets(packages):
|
||||||
default_attrs = self.__read_attributes([target + '.jw-tmpl'])
|
attrs = default_attrs
|
||||||
if default_attrs is None:
|
attrs.update(self.__read_attributes([target + '.jw-tmpl']))
|
||||||
default_attrs = attrs
|
|
||||||
secret = target + '.jw-secret'
|
secret = target + '.jw-secret'
|
||||||
replace = {} if not os.path.exists(secret) else self.__read_key_value_file(secret)
|
replace = {} if not os.path.exists(secret) else self.__read_key_value_file(secret)
|
||||||
for ext in [ '.jw-secret-file', '.jw-tmpl' ]:
|
for ext in [ '.jw-secret-file', '.jw-tmpl' ]:
|
||||||
src = target + ext
|
src = target + ext
|
||||||
if os.path.exists(src):
|
if os.path.exists(src):
|
||||||
self.__copy_securely(src=src, dst=target, default_attrs=default_attrs, replace=replace)
|
self.__copy_securely(src=src, dst=target, default_attrs=attrs, replace=replace)
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
log(WARNING, f'No secret found for target {target}, not compiling')
|
log(WARNING, f'No secret found for target {target}, not compiling')
|
||||||
|
|
|
||||||
|
|
@ -15,4 +15,11 @@ class CmdCompileTemplates(Cmd): # export
|
||||||
super().__init__(parent, 'compile-templates', help="Compile package template files")
|
super().__init__(parent, 'compile-templates', help="Compile package template files")
|
||||||
|
|
||||||
async def _run(self, args: Namespace) -> None:
|
async def _run(self, args: Namespace) -> None:
|
||||||
await self._compile_template_files(args.packages)
|
attrs = Cmd.Attrs(args.mode, args.owner, args.group, None)
|
||||||
|
await self._compile_template_files(args.packages, attrs)
|
||||||
|
|
||||||
|
def add_arguments(self, parser: ArgumentParser) -> None:
|
||||||
|
super().add_arguments(parser)
|
||||||
|
parser.add_argument('--owner', '-o', default=None, help='Default output file owner')
|
||||||
|
parser.add_argument('--group', '-g', default=None, help='Default output file group')
|
||||||
|
parser.add_argument('--mode', '-m', default=None, help='Default output file mode')
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue