#Log Exec commands

1 messages · Page 1 of 1 (latest)

drifting depot
#

Using dagger golang SDK, I'm calling
dagger.WithLogOutput(os.Stderr)
.WithExec([]string{"go", "mod", "download"}). WithExec([]string{"go", "build"})
Is there a way to log exec commands?

I want to get something like this in Stderr:

#2 build
#2 DONE 0.0s

go build
#2 DONE 14.4s```
drifting depot
#

I ended up implementing the following helper function:

    cmd := strings.Join(args, " ")
    fmt.Fprintln(os.Stderr, "RUN", cmd)

    ctr = ctr.WithExec(args, opts...)

    out, err := ctr.Stdout(ctx)
    if err != nil {
        return nil, err
    }
    // Print stdout to console.
    fmt.Println(out)

    return ctr, nil
}
swift ravine
#

Yeah, that's how I did it

ripe phoenix
#

^ there's a downside about this approach which means that you're unnecessarily forcing the pipeline DAG resolution multiple times.

i.e: if you have the following example:

c = withExecLog([]string{"echo", "foo"})
c = withExecLog([]string{"echo", "bar").ExitCode(ctx)

^ that will cause 3 DAG resolutions instead of one since each withExecLog will attempt to resolve the pipelne in the Stdout call.

Not sure if something can be enabled at the session handler / buildkit level to make this work.

cc @novel heart

#

One thing I'm thinking can improve this is just echoing the args inside the container instead of calling fmt.Println i.e:

func withExecLog(ctx context.Context, ctr *dagger.Container, args []string, opts ...dagger.ContainerWithExecOpts) *dagger.Container {
    cmd := strings.Join(args, " ")

    ctr = ctr.WithExec([]string{"echo", cmd}, opts...)

    ctr = ctr.WithExec(args, opts...)
    return ctr
}