package main import ( "log" "context" "fmt" "dagger.io/dagger" ) type stage struct { name string projectRoot string stageFunc func(ctx context.Context, client *dagger.Client, projectRoot string) error } func (s *stage) run(ctx context.Context, client *dagger.Client) error { log.Printf("Running stage: %s", s.name) if err := s.stageFunc(ctx, client, s.projectRoot); err != nil { return fmt.Errorf("error running stage '%s'; %w", s.name, err) } return nil } // test is a stage that runs the test suite using mage func test(ctx context.Context, client *dagger.Client, projectRoot string) error { image := "golang:1.20.4" containerWorkspace := "/workspace" containerWorkdir := containerWorkspace + "/internal/build" runner := client.Container(). From(image). WithMountedDirectory(containerWorkspace, client.Host().Directory(projectRoot)). WithWorkdir(containerWorkdir). WithEnvVariable("GO_TEST_VERBOSE", "1"). WithEnvVariable("GO_TEST_COVER", "1"). WithExec([]string{"go", "run", "magefiles/main.go", "-v", "test"}) _, err := runner.Stdout(ctx) return err } // lint is a stage that runs the linter using mage func lint(ctx context.Context, client *dagger.Client, projectRoot string) error { image := "golangci/golangci-lint:v1.52.2-alpine" containerWorkspace := "/workspace" containerWorkdir := containerWorkspace + "/internal/build" runner := client.Container(). From(image). WithMountedDirectory(containerWorkspace, client.Host().Directory(projectRoot)). WithWorkdir(containerWorkdir). WithExec([]string{"go", "run", "magefiles/main.go", "-v", "lint"}) _, err := runner.Stdout(ctx) return err }