greet/internal/build/stages.go

69 lines
2 KiB
Go

package main
import (
"context"
"fmt"
"log"
"dagger.io/dagger"
)
type stage struct {
name string
projectRoot string
stageFunc func(ctx context.Context, client *dagger.Client, projectRoot string, cache *dagger.CacheVolume) error
}
func (s *stage) run(ctx context.Context, client *dagger.Client, cache *dagger.CacheVolume) error {
log.Printf("Running stage: %s", s.name)
pipeline := client.Pipeline(s.name)
if err := s.stageFunc(ctx, pipeline, s.projectRoot, cache); 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, cache *dagger.CacheVolume) error {
image := "golang:1.21.0"
containerWorkspace := "/workspace"
containerWorkdir := containerWorkspace + "/internal/build"
_, err := 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"}).
WithMountedCache("/go/pkg/mod", cache).
Sync(ctx)
if err != nil {
return fmt.Errorf("error running the test stage; %w", err)
}
return nil
}
// lint is a stage that runs the linter using mage.
func lint(ctx context.Context, client *dagger.Client, projectRoot string, cache *dagger.CacheVolume) error {
image := "golangci/golangci-lint:v1.54.2-alpine"
containerWorkspace := "/workspace"
containerWorkdir := containerWorkspace + "/internal/build"
_, err := client.Container().
From(image).
WithMountedDirectory(containerWorkspace, client.Host().Directory(projectRoot)).
WithWorkdir(containerWorkdir).
WithExec([]string{"go", "run", "magefiles/main.go", "-v", "lint"}).
WithMountedCache("/go/pkg/mod", cache).
Sync(ctx)
if err != nil {
return fmt.Errorf("error running the lint stage; %w", err)
}
return nil
}