#Log Exec commands
1 messages · Page 1 of 1 (latest)
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
}
Yeah, that's how I did it
^ 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
}