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

     1  package binaries
     2  
     3  import (
     4  	"context"
     5  	"encoding/base64"
     6  	"fmt"
     7  	"io"
     8  	"strings"
     9  	"time"
    10  
    11  	"github.com/docker/docker/client"
    12  	"github.com/pojntfx/hydrapp/hydrapp/pkg/builders"
    13  	"github.com/pojntfx/hydrapp/hydrapp/pkg/executors"
    14  )
    15  
    16  const (
    17  	Image = "ghcr.io/pojntfx/hydrapp-build-binaries"
    18  )
    19  
    20  func NewBuilder(
    21  	ctx context.Context,
    22  	cli *client.Client,
    23  
    24  	image string, // OCI image to use
    25  	pull bool, // Whether to pull the image or not
    26  	src, // Input directory
    27  	dst string, // Output directory
    28  	onID func(id string), // Callback to handle container ID
    29  	stdout io.Writer, // Writer to handle container output
    30  	appID string, // App ID to use
    31  	pgpKey []byte, // PGP key contents
    32  	pgpKeyPassword, // password for the PGP key
    33  	appName string, // App name
    34  	branchID, // Branch ID
    35  	branchName string, // Branch name
    36  	branchTimestamp time.Time, // Branch timestamp
    37  	goMain, // Directory with the main package to build
    38  	goFlags, // Flags to pass to the Go command
    39  	goGenerate, // Command to execute go generate with
    40  	goExclude string, // Regex of platforms to ignore
    41  	hostPackages []string, // Debian packages to install before building
    42  ) *Builder {
    43  	return &Builder{
    44  		ctx,
    45  		cli,
    46  
    47  		image,
    48  		pull,
    49  		src,
    50  		dst,
    51  		onID,
    52  		stdout,
    53  		appID,
    54  		base64.StdEncoding.EncodeToString(pgpKey),
    55  		pgpKeyPassword,
    56  		appName,
    57  		branchID,
    58  		branchName,
    59  		branchTimestamp,
    60  		goMain,
    61  		goFlags,
    62  		goGenerate,
    63  		goExclude,
    64  		hostPackages,
    65  	}
    66  }
    67  
    68  type Builder struct {
    69  	ctx context.Context
    70  	cli *client.Client
    71  
    72  	image string
    73  	pull  bool
    74  	src,
    75  	dst string
    76  	onID   func(id string)
    77  	stdout io.Writer
    78  	appID,
    79  	pgpKey,
    80  	pgpKeyPassword,
    81  	appName,
    82  	branchID,
    83  	branchName string
    84  	branchTimestamp time.Time
    85  	goMain,
    86  	goFlags,
    87  	goGenerate,
    88  	goExclude string
    89  	hostPackages []string
    90  }
    91  
    92  func (b *Builder) Render(workdir string, ejecting bool) error {
    93  	return nil
    94  }
    95  
    96  func (b *Builder) Build() error {
    97  	dst := builders.GetFilepathForBranch(b.dst, b.branchID)
    98  	appID := builders.GetAppIDForBranch(b.appID, b.branchID)
    99  	appName := builders.GetAppNameForBranch(b.appName, b.branchName)
   100  
   101  	return executors.DockerRunImage(
   102  		b.ctx,
   103  		b.cli,
   104  		b.image,
   105  		b.pull,
   106  		true,
   107  		b.src,
   108  		dst,
   109  		b.onID,
   110  		b.stdout,
   111  		map[string]string{
   112  			"APP_ID":                   appID,
   113  			"PGP_KEY":                  b.pgpKey,
   114  			"PGP_KEY_PASSWORD":         b.pgpKeyPassword,
   115  			"APP_NAME":                 appName,
   116  			"GOMAIN":                   b.goMain,
   117  			"GOFLAGS":                  b.goFlags,
   118  			"GOGENERATE":               b.goGenerate,
   119  			"GOEXCLUDE":                b.goExclude,
   120  			"HOST_PACKAGES":            strings.Join(b.hostPackages, " "),
   121  			"BRANCH_ID":                b.branchID,
   122  			"BRANCH_TIMESTAMP_RFC3339": fmt.Sprintf("%v", b.branchTimestamp.Format(time.RFC3339)),
   123  		},
   124  		b.Render,
   125  		[]string{},
   126  	)
   127  }