#Debugging bats module

1 messages ยท Page 1 of 1 (latest)

frail onyx
#

Starting a thread ๐Ÿ™‚

#

I don't see anything immediately wrong with your code

#

Note, you can drop the context + error in dagger functions if you don't need them

#

ie. you can do func (dir *Directory) Bats (args []string) *Container

sly dawn
#

I even added an extra Foo method to test if the Directory type needs to appear in return types, not in the argument list

frail onyx
#

It's neat-o, which rhymes with vit-o

#

Btw, lately I've found that I rely on extending core types less than before. Instead I create custom types that wrap/embed core types as needed. Feels more powerful in the end

sly dawn
#

I used my stack overflow keyboard to create this module from your make and Kyle's golang module, so ๐Ÿคทโ€โ™‚๏ธ

frail onyx
#

So I stay in control of my chaining that way

sly dawn
#

That's what I wanted to do, just couldn't figure out at first how to pass a directory to a function.

#

so bats(dir: ???) { stdout }

#

Honestly, I haven't really used the graphql API directly so far, so it doesn't feel that natural yet, but I can see how it makes debugging easier than writing test code for the module.

frail onyx
#

@sly dawn you can just take a *Directory as argument

sly dawn
#

Yeah, but how do I query that? What do I put in the query?

frail onyx
#

and you can keep that *Directory as a public field of your custom struct

sly dawn
#

huh, field?

#

I have this function already:

func (m *Bats) Bats(ctx context.Context, dir *Directory, args []string) (*Container, error) {
    return run(dir, args), nil
}
frail onyx
#

I would do something like:


// Your module top-level type
type Bats struct {}

func (b *Bats) Project(dir *Directory) *Project {
 return &Project{Dir: dir}
}

// A BATS project
type Project struct {
  Dir *Directory
}

// Do something in the project
func (p *Project) Foo(args []string) *Container {
    return dag.Container().From("alpine").WithMountedDirectory("/bats", p.Dir).WithExec([]string{"bats", "foo"})
}
#

Then from there you can add "with"-style chaining:


func (p *Project) WithFoo(args []string) *Project {
  return &Project{
    Dir: dag.Container().From("alpine").WithDirectory("/bats", p.Dir).WithExec([]string{"bats", "foo"}).Directory("/bats"),
  }
}
#

As long as the fields in Project (your custom struct type) are public, the Go SDK will json-marshal them and preserve their state in between function calls, so chaining will work as you expect

sly dawn
#

Hm, okay. I guess that works. Now, the only thing I'm curious about is how you call that with graphql. ๐Ÿ˜„

There is not a single example for that in any of the existing modules.

#

How do you pass a directory to this module?

frail onyx
#

Of course we're missing convenience to pass that ID ๐Ÿ™‚ You can copy paste it from another gql query for now

#

OR you can call it from another module

sly dawn
#

That's the piece I've been missing ๐Ÿ˜„

frail onyx
#

OR you can wait for dagger CLI to add convenience to pass local directories as input

sly dawn
#

And probably that's why I ended up trying to extend the directory type

frail onyx
#

Yeah, makes sense

#

btw extending core type should still work - I don't know why it doesn't

#

but you're right that it does make it much easier to bootstrap from a graphql query

sly dawn
#

yeah

frail onyx
#

Flagging @heady halo and @latent quest ๐Ÿ™‚

#

We went on a tangent, but the core issue is that extending core type fails in @sly dawn 's bats module

craggy siren
#

Yep, I couldn't get it to work in a minimal case either

package main

import (
    "context"
)

type Bats struct{}

func (m *Bats) Foo(ctx context.Context) (*Directory, error) {
    return nil, nil
}

func (dir *Directory) Bats(ctx context.Context) (*Container, error) {
    return dag.Container().
        From("bats/bats:v1.10.0").
        WithEntrypoint([]string{""}).
        WithMountedDirectory("/src", dir).
        WithExec([]string{"/usr/local/bin/bats", "-r", "/src"}).Sync(ctx)
}
#
dagger query <<EOF
query test {
    git(url: "https://github.com/sagikazarmark/distribution") {
        branch(name: "main") {
            tree {
                bats {
                    stdout
                }
            }
        }
    }
}
EOF
#
distribution โžค docker run -it --rm --workdir /src -v $PWD:/src --entrypoint "" bats/bats:v1.10.0 bats -r /src

0 tests, 0 failures
#

Interesting errors when run from Zenith:

init
โ”‚ โ–ˆ [0.49s] connect
โ”‚ โ”ฃ [0.27s] starting engine
โ”‚ โ”ฃ [0.21s] starting session
โ”‚ โ”ƒ Failed to connect; retrying... name:"error"  value:"make request: Post \"http://dagger
โ”‚ โ”ƒ /query\": rpc error: code = Unknown desc = server \"h1sj3zf6rtmd6i2fzqlmdifh0\" not fo
โ”‚ โ”ƒ und"
โ”‚ โ”ƒ OK!
โ–ˆโ—€โ”€โ”€โ”ดโ”€โ•ฏ CACHED exec dagger mod sync bust-hack:1
โ–ˆ CACHED exec go build -o /runtime -ldflags -s -d -w .
โ–ˆ CACHED exec /runtime
โ–ˆ [1.27s] ERROR dagger query
โ”ฃ [1.27s] loading module
โ–ˆ [0.00s] ERROR query
โ”ป
WARNING: Using development engine; skipping version compatibility check.
โ€ข Cloud URL: https://dagger.cloud/runs/30c5383a-83ef-4088-9597-5a70631a687b
โ€ข Engine: 55e92bcabacc (version devel ())
โง— 1.77s โœ” 15 โˆ… 5 โœ˜ 2
Error: make request: input:5: Cannot query field "bats" on type "Directory".
sly dawn
#

Is this only a problem with my module? Or something broke and type extensions don't work at all at the moment?

craggy siren
heady halo