diff --git a/build/go_build.go b/build/go_build.go index c885ffd..340d8c8 100644 --- a/build/go_build.go +++ b/build/go_build.go @@ -6,7 +6,7 @@ import ( "path/filepath" "strings" - "golang.org/x/exp/slog" + "log/slog" ) const ( diff --git a/build/go_build_help.go b/build/go_build_help.go new file mode 100644 index 0000000..ebdc341 --- /dev/null +++ b/build/go_build_help.go @@ -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", + }, + }, + }, + }, + } +} diff --git a/go.mod b/go.mod index 4f0142e..2f9d5ec 100644 --- a/go.mod +++ b/go.mod @@ -3,13 +3,13 @@ module code.icb4dc0.de/buildr/golang-plugin go 1.20 require ( - code.icb4dc0.de/buildr/wasi-module-sdk-go v0.0.0-20230629182727-3bf4797b6abd - golang.org/x/exp v0.0.0-20230515195305-f3d0a9c9a5cc + code.icb4dc0.de/buildr/wasi-module-sdk-go v0.0.0-20230701111906-1f0c58b1c8a4 + golang.org/x/exp v0.0.0-20230809150735-7b3493d9a819 ) require ( 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 diff --git a/go.sum b/go.sum index a557773..c4a9298 100644 --- a/go.sum +++ b/go.sum @@ -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/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/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/tetratelabs/tinymem v0.1.0 h1:Qza1JAg9lquPPJ/CIei5qQYx7t18KLie83O2WR6CM58= 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-20230515195305-f3d0a9c9a5cc/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w= -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/exp v0.0.0-20230809150735-7b3493d9a819 h1:EDuYyU/MkFXllv9QF9819VlI9a4tzGuCbhG0ExK9o1U= +golang.org/x/exp v0.0.0-20230809150735-7b3493d9a819/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc= 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.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng= -google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= +google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= diff --git a/tool/go_tool.go b/tool/go_tool.go index 5e95271..c7dd8b9 100644 --- a/tool/go_tool.go +++ b/tool/go_tool.go @@ -1,11 +1,8 @@ package tool import ( - sdk "code.icb4dc0.de/buildr/wasi-module-sdk-go" - "code.icb4dc0.de/buildr/wasi-module-sdk-go/exec" "context" "fmt" - "golang.org/x/exp/slog" "net/url" "os" "path" @@ -13,6 +10,11 @@ import ( "regexp" "strings" "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 ( @@ -60,13 +62,19 @@ func (g GoTool) BinaryName() string { func (g GoTool) Execute(ctx sdk.ExecutionContext) error { var ( - binName = g.BinaryName() - stateKey = fmt.Sprintf("%s.state", binName) - logger = ctx.Logger().With(slog.String("tool_name", binName)) + binName = g.BinaryName() + stateKey = fmt.Sprintf("%s.state", 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) state = g.state() ) + logger.Info("Ensuring got tool is installed") + if p, err := exec.LookPath(binName); state == StateGlobal && err == nil { logger.Info( "Found tool installation", @@ -107,6 +115,8 @@ func (g GoTool) Execute(ctx sdk.ExecutionContext) error { args = append(args, g.BuildArgs...) 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( "go", args..., diff --git a/tool/go_tool_state.go b/tool/go_tool_state.go index a66157c..0cd4aa7 100644 --- a/tool/go_tool_state.go +++ b/tool/go_tool_state.go @@ -1,7 +1,8 @@ package tool import ( - "golang.org/x/exp/maps" + "maps" + "golang.org/x/exp/slices" )