0
0
mirror of https://github.com/thegeeklab/wp-docker-buildx.git synced 2024-06-02 18:29:42 +02:00

chore: migrate to wp-plugin-go v3 (#181)

This commit is contained in:
Robert Kaussow 2024-05-17 21:55:16 +02:00 committed by GitHub
parent 8761da6871
commit 51889e1102
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 83 additions and 64 deletions

View File

@ -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
}

View File

@ -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.

4
go.mod
View File

@ -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
)

4
go.sum
View File

@ -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=

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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,
},
}