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