Skip to content

📖 Add chainable utilities guide #5236

Closed

Description

@helderco

Motivation

All SDKs support adding a callback via With() to avoid breaking the chain:

ctr := client
    .Container()
    .From("alpine")

// breaks the chain!
ctr = AddEnv(ctr)
ctr = AddMounts(ctr, client)

out, err := ctr
    .WithExec([]string{"printenv"})
    .Stdout(ctx)

How?

The callbacks just have to return a function that receives the dagger.Container from the chain, and returns back a dagger.Container.

If more dependencies are required, use a factory.

Go

func main() {
    // ...
    out, err := client
        .Container()
        .From("alpine")
        .With(Env)
        .With(Mounts(client))
        .WithExec([]string{"printenv"})
        .Stdout(ctx)
    // ...
}

func Env(ctr *dagger.Container) *dagger.Container {
    return ctr.
        WithEnvVariable("FOO", "foo")
        WithEnvVariable("BAR", "bar")
}

func Mounts(client *dagger.Client) dagger.WithContainerFunc {
    return func(c *dagger.Container) *dagger.Container {
        return ctr
            .WithMountedDirectory("/foo", client.Host().Directory("foo"))
            .WithMountedDirectory("/bar", client.Host().Directory("bar"))
    }
}

Python

async def main():
    async with dagger.Connection as client:
        out = await (
            client.container()
            .from_("alpine")
            .with_(env)
            .with_(mounts(client))
            .with_exec(["printenv"])
            .stdout()
        )
    # ...

def env(ctr: dagger.Container):
    return (
        ctr
        .with_env_variable("FOO", "foo")
        .with_env_variable("BAR", "bar")
    )

def mounts(client: dagger.Client):
    def _mounts(ctr: dagger.Container):
        return (
            ctr
            .with_mounted_directory("/foo", client.host().directory("foo"))
            .with_mounted_directory("/bar", client.host().directory("bar"))
        )
    return _mounts

Node.js

connect(async (client: Client) => {
    out = await client
        .container()
        .from("alpine")
        .with(env)
        .with(mounts(client))
        .withExec(["printenv"])
        .stdout()
    // ...
})

function env(ctr: Container): Container {
    return ctr
        .withEnvVariable("FOO", "foo")
        .withEnvVariable("BAR", "bar")
}

function mounts(client: Client) {
    return (ctr: Container): Container => ctr
        .withMountedDirectory("/foo", client.host().directory("foo"))
        .withMountedDirectory("/bar", client.host().directory("bar"))
}

Examples

References

\cc @vikram-dagger @mircubed

Activity

  1. added
    area/documentation
    on Jun 1, 2023
  2. vikram-dagger commented on Aug 29, 2023

    @vikram-dagger
    Contributor

    Closed by #5653

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/documentation

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions