From 51889e1102d87312e0190002605b82fd071eefe4 Mon Sep 17 00:00:00 2001 From: Robert Kaussow Date: Fri, 17 May 2024 21:55:16 +0200 Subject: [PATCH] chore: migrate to wp-plugin-go v3 (#181) --- docker/daemon.go | 49 ++++++++++++++++++++++++++----------------- docker/docker.go | 43 +++++++++++++++++++++---------------- go.mod | 4 ++-- go.sum | 4 ++-- internal/docs/main.go | 6 +++--- plugin/impl.go | 23 ++++++++++---------- plugin/plugin.go | 18 ++++++++-------- 7 files changed, 83 insertions(+), 64 deletions(-) diff --git a/docker/daemon.go b/docker/daemon.go index bc64cde..eb3acac 100644 --- a/docker/daemon.go +++ b/docker/daemon.go @@ -1,11 +1,10 @@ package docker import ( - "os/exec" + "os" - "github.com/thegeeklab/wp-plugin-go/v2/types" + plugin_exec "github.com/thegeeklab/wp-plugin-go/v3/exec" "github.com/urfave/cli/v2" - "golang.org/x/sys/execabs" ) const dockerdBin = "/usr/local/bin/dockerd" @@ -30,7 +29,7 @@ type Daemon struct { } // helper function to create the docker daemon command. -func (d *Daemon) Start() *types.Cmd { +func (d *Daemon) Start() *plugin_exec.Cmd { args := []string{ "--data-root", d.StoragePath, "--host=unix:///var/run/docker.sock", @@ -76,13 +75,17 @@ func (d *Daemon) Start() *types.Cmd { args = append(args, "--max-concurrent-uploads", d.MaxConcurrentUploads) } - return &types.Cmd{ - Cmd: execabs.Command(dockerdBin, args...), - Private: !d.Debug, + cmd := plugin_exec.Command(dockerdBin, args...) + + if d.Debug { + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr } + + return cmd } -func (d *Daemon) CreateBuilder() *types.Cmd { +func (d *Daemon) CreateBuilder() *plugin_exec.Cmd { args := []string{ "buildx", "create", @@ -93,20 +96,28 @@ func (d *Daemon) CreateBuilder() *types.Cmd { args = append(args, "--config", d.BuildkitConfigFile) } - return &types.Cmd{ - Cmd: execabs.Command(dockerBin, args...), - } + cmd := plugin_exec.Command(dockerBin, args...) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + + return cmd } -func (d *Daemon) ListBuilder() *types.Cmd { - return &types.Cmd{ - Cmd: execabs.Command(dockerBin, "buildx", "ls"), - } +func (d *Daemon) ListBuilder() *plugin_exec.Cmd { + cmd := plugin_exec.Command(dockerBin, "buildx", "ls") + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + + return cmd } -func (d *Daemon) StartCoreDNS() *types.Cmd { - return &types.Cmd{ - Cmd: exec.Command("coredns", "-conf", "/etc/coredns/Corefile"), - Private: !d.Debug, +func (d *Daemon) StartCoreDNS() *plugin_exec.Cmd { + cmd := plugin_exec.Command("coredns", "-conf", "/etc/coredns/Corefile") + + if d.Debug { + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr } + + return cmd } diff --git a/docker/docker.go b/docker/docker.go index bb064f3..f2777e6 100644 --- a/docker/docker.go +++ b/docker/docker.go @@ -6,9 +6,8 @@ import ( "strings" "time" - "github.com/thegeeklab/wp-plugin-go/v2/types" + plugin_exec "github.com/thegeeklab/wp-plugin-go/v3/exec" "github.com/urfave/cli/v2" - "golang.org/x/sys/execabs" ) const dockerBin = "/usr/local/bin/docker" @@ -54,7 +53,7 @@ type Build struct { } // helper function to create the docker login command. -func (r *Registry) Login() *types.Cmd { +func (r *Registry) Login() *plugin_exec.Cmd { args := []string{ "login", "-u", r.Username, @@ -67,27 +66,33 @@ func (r *Registry) Login() *types.Cmd { args = append(args, r.Address) - return &types.Cmd{ - Cmd: execabs.Command(dockerBin, args...), - } + cmd := plugin_exec.Command(dockerBin, args...) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + + return cmd } // helper function to create the docker info command. -func Version() *types.Cmd { - return &types.Cmd{ - Cmd: execabs.Command(dockerBin, "version"), - } +func Version() *plugin_exec.Cmd { + cmd := plugin_exec.Command(dockerBin, "version") + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + + return cmd } // helper function to create the docker info command. -func Info() *types.Cmd { - return &types.Cmd{ - Cmd: execabs.Command(dockerBin, "info"), - } +func Info() *plugin_exec.Cmd { + cmd := plugin_exec.Command(dockerBin, "info") + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + + return cmd } // helper function to create the docker build command. -func (b *Build) Run() *types.Cmd { +func (b *Build) Run() *plugin_exec.Cmd { args := []string{ "buildx", "build", @@ -180,9 +185,11 @@ func (b *Build) Run() *types.Cmd { args = append(args, "--secret", secret) } - return &types.Cmd{ - Cmd: execabs.Command(dockerBin, args...), - } + cmd := plugin_exec.Command(dockerBin, args...) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + + return cmd } // helper function to add proxy values from the environment. diff --git a/go.mod b/go.mod index 5047262..51a70f1 100644 --- a/go.mod +++ b/go.mod @@ -5,9 +5,8 @@ go 1.22 require ( github.com/cenkalti/backoff/v4 v4.3.0 github.com/rs/zerolog v1.32.0 - github.com/thegeeklab/wp-plugin-go/v2 v2.3.1 + github.com/thegeeklab/wp-plugin-go/v3 v3.0.2 github.com/urfave/cli/v2 v2.27.2 - golang.org/x/sys v0.20.0 ) require ( @@ -29,4 +28,5 @@ require ( github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 // indirect golang.org/x/crypto v0.23.0 // indirect golang.org/x/net v0.25.0 // indirect + golang.org/x/sys v0.20.0 // indirect ) diff --git a/go.sum b/go.sum index 31e52ef..1b7e6c3 100644 --- a/go.sum +++ b/go.sum @@ -48,8 +48,8 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= -github.com/thegeeklab/wp-plugin-go/v2 v2.3.1 h1:ARwYgTPZ5iPsmOenmqcCf8TjiEe8wBOHKO7H/Xshe48= -github.com/thegeeklab/wp-plugin-go/v2 v2.3.1/go.mod h1:0t8M8txtEFiaB6RqLX8vLrxkqAo5FT5Hx7dztN592D4= +github.com/thegeeklab/wp-plugin-go/v3 v3.0.2 h1:Mv5i8S1WY+BUNjTjX6lOnB3p8S9mvM+XwfY4R98gx0g= +github.com/thegeeklab/wp-plugin-go/v3 v3.0.2/go.mod h1:ij1iJcAVgzerBTqXnmq0bu1VA+hhVVwzXKqiqfoGjjg= github.com/urfave/cli/v2 v2.27.2 h1:6e0H+AkS+zDckwPCUrZkKX38mRaau4nL2uipkJpbkcI= github.com/urfave/cli/v2 v2.27.2/go.mod h1:g0+79LmHHATl7DAcHO99smiR/T7uGLw84w8Y42x+4eM= github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 h1:+qGGcbkzsfDQNPPe9UDgpxAWQrhbbBXOYJFQDq/dtJw= diff --git a/internal/docs/main.go b/internal/docs/main.go index d4b6138..5b09a59 100644 --- a/internal/docs/main.go +++ b/internal/docs/main.go @@ -11,8 +11,8 @@ import ( "time" "github.com/thegeeklab/wp-docker-buildx/plugin" - "github.com/thegeeklab/wp-plugin-go/v2/docs" - "github.com/thegeeklab/wp-plugin-go/v2/template" + plugin_docs "github.com/thegeeklab/wp-plugin-go/v3/docs" + plugin_template "github.com/thegeeklab/wp-plugin-go/v3/template" ) func main() { @@ -23,7 +23,7 @@ func main() { p := plugin.New(nil) - out, err := template.Render(context.Background(), client, tmpl, docs.GetTemplateData(p.App)) + out, err := plugin_template.Render(context.Background(), client, tmpl, plugin_docs.GetTemplateData(p.App)) if err != nil { panic(err) } diff --git a/plugin/impl.go b/plugin/impl.go index 920e9bc..9165d74 100644 --- a/plugin/impl.go +++ b/plugin/impl.go @@ -10,10 +10,11 @@ import ( "github.com/cenkalti/backoff/v4" "github.com/rs/zerolog/log" "github.com/thegeeklab/wp-docker-buildx/docker" - "github.com/thegeeklab/wp-plugin-go/v2/file" - "github.com/thegeeklab/wp-plugin-go/v2/tag" - "github.com/thegeeklab/wp-plugin-go/v2/types" - "github.com/thegeeklab/wp-plugin-go/v2/util" + plugin_exec "github.com/thegeeklab/wp-plugin-go/v3/exec" + plugin_file "github.com/thegeeklab/wp-plugin-go/v3/file" + plugin_tag "github.com/thegeeklab/wp-plugin-go/v3/tag" + plugin_types "github.com/thegeeklab/wp-plugin-go/v3/types" + plugin_util "github.com/thegeeklab/wp-plugin-go/v3/util" "github.com/urfave/cli/v2" ) @@ -51,11 +52,11 @@ func (p *Plugin) Validate() error { if p.Settings.Build.TagsAuto { // return true if tag event or default branch - if tag.IsTaggable( + if plugin_tag.IsTaggable( p.Settings.Build.Ref, p.Settings.Build.Branch, ) { - tag, err := tag.SemverTagSuffix( + tag, err := plugin_tag.SemverTagSuffix( p.Settings.Build.Ref, p.Settings.Build.TagsSuffix, true, @@ -81,8 +82,8 @@ func (p *Plugin) Validate() error { func (p *Plugin) Execute() error { var err error - homeDir := util.GetUserHomeDir() - batchCmd := make([]*types.Cmd, 0) + homeDir := plugin_util.GetUserHomeDir() + batchCmd := make([]*plugin_exec.Cmd, 0) // start the Docker daemon server //nolint: nestif @@ -141,7 +142,7 @@ func (p *Plugin) Execute() error { buildkitConf := p.Settings.BuildkitConfig if buildkitConf != "" { - if p.Settings.Daemon.BuildkitConfigFile, err = file.WriteTmpFile("buildkit.toml", buildkitConf); err != nil { + if p.Settings.Daemon.BuildkitConfigFile, err = plugin_file.WriteTmpFile("buildkit.toml", buildkitConf); err != nil { return fmt.Errorf("error writing buildkit config: %w", err) } @@ -189,14 +190,14 @@ func (p *Plugin) Execute() error { } func (p *Plugin) FlagsFromContext() error { - cacheFrom, ok := p.Context.Generic("cache-from").(*types.StringSliceFlag) + cacheFrom, ok := p.Context.Generic("cache-from").(*plugin_types.StringSliceFlag) if !ok { return fmt.Errorf("%w: failed to read cache-from input", ErrTypeAssertionFailed) } p.Settings.Build.CacheFrom = cacheFrom.Get() - secrets, ok := p.Context.Generic("secrets").(*types.StringSliceFlag) + secrets, ok := p.Context.Generic("secrets").(*plugin_types.StringSliceFlag) if !ok { return fmt.Errorf("%w: failed to read secrets input", ErrTypeAssertionFailed) } diff --git a/plugin/plugin.go b/plugin/plugin.go index 9d6ed54..5b6eb55 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -4,8 +4,8 @@ import ( "fmt" "github.com/thegeeklab/wp-docker-buildx/docker" - wp "github.com/thegeeklab/wp-plugin-go/v2/plugin" - "github.com/thegeeklab/wp-plugin-go/v2/types" + plugin_base "github.com/thegeeklab/wp-plugin-go/v3/plugin" + plugin_types "github.com/thegeeklab/wp-plugin-go/v3/types" "github.com/urfave/cli/v2" ) @@ -13,7 +13,7 @@ import ( // Plugin implements provide the plugin. type Plugin struct { - *wp.Plugin + *plugin_base.Plugin Settings *Settings } @@ -26,15 +26,15 @@ type Settings struct { Build docker.Build } -func New(e wp.ExecuteFunc, build ...string) *Plugin { +func New(e plugin_base.ExecuteFunc, build ...string) *Plugin { p := &Plugin{ Settings: &Settings{}, } - options := wp.Options{ + options := plugin_base.Options{ Name: "wp-docker-buildx", Description: "Build multiarch OCI images with buildx", - Flags: Flags(p.Settings, wp.FlagsPluginCategory), + Flags: Flags(p.Settings, plugin_base.FlagsPluginCategory), Execute: p.run, HideWoodpeckerFlags: true, } @@ -51,7 +51,7 @@ func New(e wp.ExecuteFunc, build ...string) *Plugin { options.Execute = e } - p.Plugin = wp.New(options) + p.Plugin = plugin_base.New(options) return p } @@ -267,7 +267,7 @@ func Flags(settings *Settings, category string) []cli.Flag { Name: "cache-from", EnvVars: []string{"PLUGIN_CACHE_FROM"}, Usage: "images to consider as cache sources", - Value: &types.StringSliceFlag{}, + Value: &plugin_types.StringSliceFlag{}, Category: category, }, &cli.StringFlag{ @@ -387,7 +387,7 @@ func Flags(settings *Settings, category string) []cli.Flag { Name: "secrets", EnvVars: []string{"PLUGIN_SECRETS"}, Usage: "exposes secrets to the build", - Value: &types.StringSliceFlag{}, + Value: &plugin_types.StringSliceFlag{}, Category: category, }, }