#Avoiding boilerplate when wrapping with_directory to set a default owner

1 messages · Page 1 of 1 (latest)

idle kite
#

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?

grizzled horizon
# idle kite Hi! I'm building a Dagger module that creates a non-root user (python, uid/gid 1...

@idle kite how about using the with_ helper functions in the SDKs for this.

You can do something like:

def guid_dir(uid,guid: str, dir: dagger.Directory):
    def _guid_dir(ctr: dagger.Container):
        return ctr.with_directory("/foo", dir, owner=f"{uid}:{uid}")

    return _guid_dir


@object_type
class Functest:
    @function
    def test(self) -> dagger.Container:
        return dag.container().with_(guid_dir("1000", "1000", dag.directory()))

idle kite
#

Hmm that clearly helps avoiding some boilerplate but not completely. I am beginning to think I would be better not having multiple dagger modules in my monorepo.

grizzled horizon
#

@sullen sparrow create an issue in the dagger repo for this.

sullen sparrowBOT
#

I could not prepare the sandbox for this thread. Admin has the error details.

idle kite
#

Having something like with_default_owner() would be great I think

grizzled horizon
sullen sparrowBOT
#

...

grizzled horizon
#

:leave

sullen sparrowBOT
#

Left this thread. Sandbox marked for deletion.

grizzled horizon
#

@sullen sparrow ping