#I'm confused on exporting files & debugging this

1 messages ยท Page 1 of 1 (latest)

stiff sapphire
#

I don't get it ๐Ÿ™‚

I'm trying to create files as normal, such as os.WriteFile, and then at the end of this export the artifact directory I dump all my various files.

This doesn't seem to work.
Instead I seem to only get results with specifying directory withfile and having to create.

  • My code doesn't need any other containers, no exec, pure Go SDK code.
  • I want to output files to /tmp/artifacts which I've tried creating via standard go code and with dagger.Directory.

One of my steps can't provide string content for file, as binary, so trying to provide the content via WithNewFile works but corrupts it.

I'll provide some code below, can you help?
All the examples in the cookbook don't seem to handle raw Go code seem to focus more on invoking wrapped calls with container. I want to use pure Go and have dagger execute.

#
func (s *Snyk) Issues(
    ctx context.Context,
    // orgID is the snyk org ID, or pass "all" to do against everything in the org.
    orgID string,
    token *dagger.Secret,
    // +optional
    // format accepts xlsx or csv to output an additional report
    format string,
) (*dagger.Directory, error) {
    _artifactsDirectory = "/tmp/artifacts"
    _reportJSON := filepath.Join(_artifactsDirectory, "report.json")

    dir := dag.Directory().
        WithNewDirectory(_artifactsDirectory, dagger.DirectoryWithNewDirectoryOpts{
            Permissions: os.Getegid(),
        })

    // NOTE: I want to just do this and work in side the container, until I need to export the directory
    // but having issues with this
    if _, err := os.Stat(_artifactsDirectory); os.IsNotExist(err) {
        if err := os.MkdirAll(_artifactsDirectory, os.ModePerm); err != nil {
            return nil, fmt.Errorf("failed to create artifacts directory: %w", err)
        }
    }
    // NOTE: I want to just do this
    filew := os.WriteFile(_reportJSON, bi, 0o644)
    if filew != nil {
        return nil, filew
    }
    // NOTE: then I want to just mention it, BUT rather would just have the artifacxt
    // directory containing it be used so I can export the contents without having to mention files directxly
    dir.File(_reportJSON)
    // NOTE: this is required for me to see them at the end
    dir = dir.WithNewFile(_errorReportJSON, string(bierr), dagger.DirectoryWithNewFileOpts{})

    // NOTE: this is all i want to have to do, take the dagger containerized directory I've been writing files to
    // and make sure that gets exported
    dir = dir.WithDirectory(_artifactsDirectory, dir)
    return dir, nil
}
cinder palm
#

I think the trick to do here is to place things in ./artifacts instead of /tmp/artifacts and then use dag.CurrentModule().Workdir("./artifacts"): https://docs.dagger.io/api/types/#currentmodule

In addition to basic types (string, boolean, integer, arrays...), the Dagger API also provides powerful core types which serve as both arguments and return values for Dagger Functions.

stiff sapphire
#

Will give it a try

stiff sapphire
#

@cinder palm so I think that simplified working with my local artifacts directory. Basically CurrentModule.WorkDir is references the "blackbox" of what my module is executing in, and therefore when I return it with an export from the CLI i get whatever I dropped into that directory, right?

Thanks for this!