feat: initial version of Go plugin

This commit is contained in:
Peter 2023-08-15 21:46:36 +02:00
parent c2eb3a6a72
commit c3490fe51b
Signed by: prskr
GPG key ID: C1DB5D2E8DB512F9
6 changed files with 135 additions and 24 deletions

View file

@ -6,7 +6,7 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"golang.org/x/exp/slog" "log/slog"
) )
const ( const (

109
build/go_build_help.go Normal file
View file

@ -0,0 +1,109 @@
package build
import (
sdk "code.icb4dc0.de/buildr/wasi-module-sdk-go"
rpcv1 "code.icb4dc0.de/buildr/wasi-module-sdk-go/protocol/generated/rpc/v1"
)
func (g GoBuild) Help() sdk.Help {
return sdk.Help{
Name: "Go build - build Go binaries",
Description: `This module helps to build Go binaries.
It abstracts common build parameters like GOOS and GOARCH.
Less common parameters can be specified e.g. with ` + "`flags`" + ` or ` + "`ldflags`" + `.
Builds - as every other task - are executed in parallel.`,
Examples: []sdk.Example{
{
Name: "Simple build",
Description: "Simplest possible example of a build",
Spec: sdk.TaskSpec[sdk.Module]{
ModuleName: "buildr_linux_amd64",
Module: GoBuild{
Binary: "buildr",
Main: ".",
GoOS: "linux",
GoArch: "amd64",
},
},
},
{
Name: "Build with flags",
Description: `Specify normal build flags as well as ldflags.`,
Spec: sdk.TaskSpec[sdk.Module]{
ModuleName: "buildr_linux_amd64",
Module: GoBuild{
Binary: "buildr",
Main: ".",
GoOS: "linux",
GoArch: "amd64",
Flags: []string{
"-v",
"-trimpath",
"-a",
"-installsuffix=cgo",
},
LdFlags: []string{
"-w -s",
"-X 'code.icb4dc0.de/buildr/buildr/cmd.CurrentVersion=main'",
},
},
},
},
{
Name: "Build with environment variables",
Description: `Specify additional environment variables.`,
Spec: sdk.TaskSpec[sdk.Module]{
ModuleName: "buildr_linux_amd64",
Module: GoBuild{
Binary: "buildr",
Main: ".",
GoOS: "linux",
GoArch: "amd64",
Env: map[string]string{
"CGO_ENABLED": "0",
},
},
},
},
{
Name: "Containerize build process",
Description: `It is also possible to run the build in a container.
Although it is not very effective because the build process always has to download all dependencies if they are not cached.`,
Spec: sdk.TaskSpec[sdk.Module]{
ModuleName: "buildr_linux_amd64",
Container: &rpcv1.ContainerSpec{
Image: "golang:alpine",
},
Module: GoBuild{
Binary: "buildr",
Main: ".",
GoOS: "linux",
GoArch: "amd64",
},
},
},
{
Name: "Containerize build process - with module cache volume",
Description: `Specify additional environment variables.`,
Spec: sdk.TaskSpec[sdk.Module]{
ModuleName: "buildr_linux_amd64",
Container: &rpcv1.ContainerSpec{
Image: "golang:alpine",
VolumeMounts: []*rpcv1.ContainerVolumeMount{
{
Target: "/go/pkg/mod",
Name: "modcache",
},
},
},
Module: GoBuild{
Binary: "buildr",
Main: ".",
GoOS: "linux",
GoArch: "amd64",
},
},
},
},
}
}

6
go.mod
View file

@ -3,13 +3,13 @@ module code.icb4dc0.de/buildr/golang-plugin
go 1.20 go 1.20
require ( require (
code.icb4dc0.de/buildr/wasi-module-sdk-go v0.0.0-20230629182727-3bf4797b6abd code.icb4dc0.de/buildr/wasi-module-sdk-go v0.0.0-20230701111906-1f0c58b1c8a4
golang.org/x/exp v0.0.0-20230515195305-f3d0a9c9a5cc golang.org/x/exp v0.0.0-20230809150735-7b3493d9a819
) )
require ( require (
github.com/tetratelabs/tinymem v0.1.0 // indirect github.com/tetratelabs/tinymem v0.1.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect google.golang.org/protobuf v1.31.0 // indirect
) )
replace code.icb4dc0.de/buildr/wasi-module-sdk-go => ../wasi-module-sdk-go replace code.icb4dc0.de/buildr/wasi-module-sdk-go => ../wasi-module-sdk-go

17
go.sum
View file

@ -1,20 +1,11 @@
github.com/golang/protobuf v1.5.0 h1:LUVKkCeviFUMKqHa4tXIIij/lbhnMbP7Fn5wKdKkRh4=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/tetratelabs/tinymem v0.1.0 h1:Qza1JAg9lquPPJ/CIei5qQYx7t18KLie83O2WR6CM58= github.com/tetratelabs/tinymem v0.1.0 h1:Qza1JAg9lquPPJ/CIei5qQYx7t18KLie83O2WR6CM58=
github.com/tetratelabs/tinymem v0.1.0/go.mod h1:WFFTZFhLod6lTL+UetFAopVbGaB+KFsVcIY+RUv7NeY= github.com/tetratelabs/tinymem v0.1.0/go.mod h1:WFFTZFhLod6lTL+UetFAopVbGaB+KFsVcIY+RUv7NeY=
golang.org/x/exp v0.0.0-20230515195305-f3d0a9c9a5cc h1:mCRnTeVUjcrhlRmO0VK8a6k6Rrf6TF9htwo2pJVSjIU= golang.org/x/exp v0.0.0-20230809150735-7b3493d9a819 h1:EDuYyU/MkFXllv9QF9819VlI9a4tzGuCbhG0ExK9o1U=
golang.org/x/exp v0.0.0-20230515195305-f3d0a9c9a5cc/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w= golang.org/x/exp v0.0.0-20230809150735-7b3493d9a819/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
golang.org/x/mod v0.6.0 h1:b9gGHsz9/HhJ3HF5DHQytPpuwocVTChQJK3AvoLRD5I=
golang.org/x/mod v0.6.0/go.mod h1:4mET923SAdbXp2ki8ey+zGs1SLqsuM2Y0uvdZR/fUNI=
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/tools v0.2.0 h1:G6AHpWxTMGY1KyEYoAQ5WTtIekUUvDNjan3ugu60JvE=
golang.org/x/tools v0.2.0/go.mod h1:y4OqIKeOV/fWJetJ8bXPU1sEVniLMIyDAZWeHdV+NTA=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng= google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=

View file

@ -1,11 +1,8 @@
package tool package tool
import ( import (
sdk "code.icb4dc0.de/buildr/wasi-module-sdk-go"
"code.icb4dc0.de/buildr/wasi-module-sdk-go/exec"
"context" "context"
"fmt" "fmt"
"golang.org/x/exp/slog"
"net/url" "net/url"
"os" "os"
"path" "path"
@ -13,6 +10,11 @@ import (
"regexp" "regexp"
"strings" "strings"
"time" "time"
"golang.org/x/exp/slog"
sdk "code.icb4dc0.de/buildr/wasi-module-sdk-go"
"code.icb4dc0.de/buildr/wasi-module-sdk-go/exec"
) )
const ( const (
@ -60,13 +62,19 @@ func (g GoTool) BinaryName() string {
func (g GoTool) Execute(ctx sdk.ExecutionContext) error { func (g GoTool) Execute(ctx sdk.ExecutionContext) error {
var ( var (
binName = g.BinaryName() binName = g.BinaryName()
stateKey = fmt.Sprintf("%s.state", binName) stateKey = fmt.Sprintf("%s.state", binName)
logger = ctx.Logger().With(slog.String("tool_name", binName)) logger = ctx.Logger().With(
slog.String("tool_name", binName),
slog.String("repository", g.Repository),
slog.String("version", g.Version),
)
stateEncoder = sdk.NewJSONStateEncoder[GoToolState](ctx) stateEncoder = sdk.NewJSONStateEncoder[GoToolState](ctx)
state = g.state() state = g.state()
) )
logger.Info("Ensuring got tool is installed")
if p, err := exec.LookPath(binName); state == StateGlobal && err == nil { if p, err := exec.LookPath(binName); state == StateGlobal && err == nil {
logger.Info( logger.Info(
"Found tool installation", "Found tool installation",
@ -107,6 +115,8 @@ func (g GoTool) Execute(ctx sdk.ExecutionContext) error {
args = append(args, g.BuildArgs...) args = append(args, g.BuildArgs...)
args = append(args, fmt.Sprintf("%s@%s", g.Repository, g.version())) args = append(args, fmt.Sprintf("%s@%s", g.Repository, g.version()))
logger.Debug("Installing Go tool", slog.String("args", strings.Join(args, ", ")))
cmd := exec.NewCommand( cmd := exec.NewCommand(
"go", "go",
args..., args...,

View file

@ -1,7 +1,8 @@
package tool package tool
import ( import (
"golang.org/x/exp/maps" "maps"
"golang.org/x/exp/slices" "golang.org/x/exp/slices"
) )