#Using Dagger via SDK but interactive

1 messages · Page 1 of 1 (latest)

tame cove
#

I want to use Dagger within a magefile. I was able to run some tasks. Though I cannot figure out how to make a dagger client that has interactive capabilities. I have the impression that I would need to configure the dagger engine my self - I think this is were I configure interactivity - and configure the dagger client to use it.

Am I correct so far? Does anyone have a working code example?

patent pine
#

given that you're using Dagger as an SDK, you have complete control over you program to add the level of interactivity that you need

tame cove
#
//go:build mage
// +build mage

package main

import (
    "context"
    "dagger.io/dagger"
    "fmt"
    "os"
)

func OsRelease(ctx context.Context) {
    client, err := dagger.Connect(ctx,
        dagger.WithLogOutput(os.Stdout),
    )

    if err != nil {
        panic(err)
    }
    defer client.Close()

    client.Container().
        From("alpine:latest").
        WithExec([]string{"apk", "--no-cache", "add", "less"}).
        Terminal(dagger.ContainerTerminalOpts{Cmd: []string{"less", "/etc/os-release"}}).
        Stdout(ctx)
}

That's just an example of course. You would run it by executing mage OsRelease. Though I think it'd be the same if I was just running it using go run magefile.go.

#

This fails with ! failed to open terminal: failed to open terminal: rpc error: code = Unknown desc = running shell without the TUI is not supported

patent pine
tame cove
#

that is sadly not how mage works

#

Too bad, I was really looking forward to be using dagger.

But that I cannot just pass host environment variables (at least not without having to pass them parameter by parameter via cli) and not being able to integrate it via mage (that would at least solve the environment variable problem) and have an interactive terminal kind of makes this a dead end 😕

patent pine
# tame cove that is sadly not how mage works

not sure what you mean. The component that handles the terminal interactivity is the Dagger Client. So you have two options if you want Terminal to work:

  1. The easy way is to wrap your command with dagger run as I mentioned above
  2. The super hard way is to take that logic from the Dagger CLI code and make it work in your magefile entrypoint
tame cove
#

I was working on option two it seems. Though when importing github.com/dagger/dagger (after inserting the mandatory replace for distconsts) it does not seem to be possible to compile dagger.

# github.com/dagger/dagger/engine/client
../../../../go/pkg/mod/github.com/dagger/dagger@v0.18.7/engine/client/client.go:355:79: too many arguments in call to authprovider.NewDockerAuthProvider
        have (*configfile.ConfigFile, nil)
        want (authprovider.DockerAuthProviderConfig)
Error: error compiling magefiles
patent pine
#

But that I cannot just pass host environment variables (at least not without having to pass them parameter by parameter via cli)

We know this is an invonvenience in some cases. Take into account that the main reason why we don't support this out of the box is mostly for security reasons. Allowing modules to arbitrarily read env vars from your machine might allow someone potentially creating a rouge module which can exfiliate or even use your env variable credentials in an unsafe way

tame cove
#

I totally understand and appreciate that. If I were able to explicitly define which variables are passed that would already help a lot and also make it less arbitrary. I have as well read the issue you are referencing already.

I will think about calling dagger via the sh helper library of mage (so I basically call dagger -c from mage).

The prior approach would have made it easier to distribute and integrate dagger into our setup without breaking anything though.

#

Is there any chance to make the direct go integration interactive in a future release?

patent pine
# tame cove I totally understand and appreciate that. If I were able to explicitly define wh...

The prior approach would have made it easier to distribute and integrate dagger into our setup without breaking anything though.

I see that makes sense, sorry for the inconvenience.

Is there any chance to make the direct go integration interactive in a future release?

That will be challenging since it means that we'll have to support the same type of integration for each of the SDKs which as you know.. it's a ton of work. That's mostly why design is like it is today.

Having said that, I can try to see if there might be a way where we can get the dagger/dagger/engine/client package to work for your specific magefile use-case

#

I can't promise anything but I'll add this to my "context switch" backlog for things to check this week