#Dagger function entries recursively listed?

1 messages · Page 1 of 1 (latest)

ivory mantle
#

Messing around with native go calls, not using grep/find etc.

I see I can pass dir-arg per demo but when I invoke entries, it's just the current directory without recursion and the DirectoryEntriesOpts{} doesn't give me any option to recurse.

Still wrapping my head around lifecycle 🙂

Am I supposed to use a different arg for passing through the directory with all subdirectories?

My goal was to match all with a pattern, using native dagger/go calls and then I'll upload each of the files using API calls after verifying they match.

Main two issues...

  • Recurse?
  • If want one func to be the helper method to lookup and return the list of files to iterate through, is it just a []string returned or do I pass a dagger result to another dagger function? I'm guessing I'll find the answer partially in: https://docs.dagger.io/manuals/developer/go/528510/chaining
const PatternDatadogServiceFiles = ".datadog.svcdef.json"

func (m *Daggerverse) GrepDefinitionFiles(ctx context.Context, dirArg *Directory) ([]string, error) {
    foundFiles := []string{}

    files, err := dirArg.Entries(ctx, dagger.DirectoryEntriesOpts{})
    if err != nil {
        return foundFiles, err
    }

    for _, f := range files {
        if strings.HasSuffix(f, PatternDatadogServiceFiles) {
            foundFiles = append(foundFiles, f)
        } else {
            foundFiles = append(foundFiles, "NOT MATCHED: "+f)
        }
    }

    return foundFiles, nil
}

Dagger Functions can return custom objects, which in turn can define new functions. This allows for "chaining" of functions in the same style as the core Dagger API.

lapis summit
#

Yeah there's no native support for walking the tree, I have also wished we had that

#

And indeed Entries() will only return a flat list of the top-level entries (file or directory) in the directory. It does not walk.

#
  • Stopgap: implement a walk() helper function, perhaps with a filter. Built on entries() + directory. Probably requires some parsing of the error message since we don't have a call for knowing the type of an entry either
#
  • Long-term solution: implement missing core calls. Best done after implementing the stopgap
ivory mantle
#

So if I do a stop gap... am I supposed to have access to the mounted directory in the dir-arg object so I can then just use native go walk func? Assuming all my actions are happening in that context, but not yet certain what's in scope (haven't gone through all the dag gen file to see.

lapis summit
#

@ivory mantle here you go 🙂

dagger call -m github.com/shykes/utils walk --dir .
#

Filtering is left as an exercise to the reader

#

To use in your Go module:

dagger install github.com/shykes/utils

Then in your code:

allTheFiles, err := dag.Utils().Walk(ctx, mydir)
uneven remnant
#
paths, err := dirArg.Glob(ctx, "**/"+PatternDatadogServiceFiles)
lapis summit
#

I had no idea what glob does

#

the name is very unintuitive

slim stratus
#

I like using it as a function or a core feature of Dagger to use cross-language. Relying on language-specific implementations seems wrong, especially with a top-level command entries being supported. (Or did I misunderstand, and you're recommending Solomon change his implementation in the utils module?) @uneven remnant

uneven remnant
#

It replaces Solomon's util, with the additional feature of matching by pattern.