Hi! I'm building a Dagger module that creates a non-root user (python, uid/gid 1234) and I want every with_directory call to automatically use owner="1234:1234" without
callers having to pass it explicitly.
My current solution is to wrap Container in a custom PythonDocker type that holds uid, gid, and app_dir as fields, and exposes a with_directory method that injects owner
automatically:
@object_type
class PythonDocker:
container: Container = field()
uid: int = field()
gid: int = field()
app_dir: str = field()
@function
def with_directory(self, source: Directory, path: str | None = None, include: list[str] | None = None) -> "PythonDocker":
return PythonDocker(
uid=self.uid, gid=self.gid, app_dir=self.app_dir,
container=self.container.with_directory(
path or self.app_dir, source, include=include, owner=f"{self.uid}:{self.gid}"
)
)
That's a lot of boilerplate just for one parameter. The alternative I considered was reading uid and gid directly from the caller side (a separate module):
uid, gid = await asyncio.gather(env.uid(), env.gid())
container.with_directory(path, source, owner=f"{uid}:{gid}")
But we lose lazyness. Is there a cleaner pattern for this?