github.com/pojntfx/hydrapp/hydrapp@v0.0.0-20240516002902-d08759d6ca9f/pkg/builders/tests/builder.go (about)

     1  package tests
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"io"
     7  
     8  	"github.com/docker/docker/client"
     9  	"github.com/pojntfx/hydrapp/hydrapp/pkg/executors"
    10  )
    11  
    12  const (
    13  	Image = "ghcr.io/pojntfx/hydrapp-build-tests"
    14  )
    15  
    16  func NewBuilder(
    17  	ctx context.Context,
    18  	cli *client.Client,
    19  
    20  	image string, // OCI image to use
    21  	pull bool, // Whether to pull the image or not
    22  	src, // Input directory
    23  	dst string, // Output directory
    24  	onID func(id string), // Callback to handle container ID
    25  	stdout io.Writer, // Writer to handle container output
    26  	goFlags, // Flags to pass to the Go command
    27  	goGenerate, // Command to execute go generate with
    28  	goTests string, // Command to run tests with
    29  ) *Builder {
    30  	return &Builder{
    31  		ctx,
    32  		cli,
    33  
    34  		image,
    35  		pull,
    36  		src,
    37  		dst,
    38  		onID,
    39  		stdout,
    40  		goFlags,
    41  		goGenerate,
    42  		goTests,
    43  	}
    44  }
    45  
    46  type Builder struct {
    47  	ctx context.Context
    48  	cli *client.Client
    49  
    50  	image string
    51  	pull  bool
    52  	src,
    53  	dst string
    54  	onID   func(id string)
    55  	stdout io.Writer
    56  	goFlags,
    57  	goGenerate,
    58  	goTests string
    59  }
    60  
    61  func (b *Builder) Render(workdir string, ejecting bool) error {
    62  	return nil
    63  }
    64  
    65  func (b *Builder) Build() error {
    66  	return executors.DockerRunImage(
    67  		b.ctx,
    68  		b.cli,
    69  		b.image,
    70  		b.pull,
    71  		true,
    72  		b.src,
    73  		"",
    74  		b.onID,
    75  		b.stdout,
    76  		map[string]string{
    77  			"GOFLAGS": b.goFlags,
    78  		},
    79  		b.Render,
    80  		[]string{
    81  			"sh",
    82  			"-c",
    83  			fmt.Sprintf(`export GOPROXY='https://proxy.golang.org,direct' GOFLAGS="${GOFLAGS}" && cd /hydrapp/work && %v && cd /hydrapp/work && %v`, b.goGenerate, b.goTests),
    84  		},
    85  	)
    86  }