0
0
mirror of https://github.com/thegeeklab/wp-opentofu.git synced 2024-09-16 16:22:46 +02:00

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

This commit is contained in:
Robert Kaussow 2024-05-17 21:55:22 +02:00 committed by GitHub
parent 5f8c89c973
commit 53be7b2f70
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 82 additions and 51 deletions

4
go.mod
View File

@ -6,9 +6,8 @@ require (
github.com/Masterminds/semver/v3 v3.2.1 github.com/Masterminds/semver/v3 v3.2.1
github.com/rs/zerolog v1.32.0 github.com/rs/zerolog v1.32.0
github.com/stretchr/testify v1.9.0 github.com/stretchr/testify v1.9.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 github.com/urfave/cli/v2 v2.27.2
golang.org/x/sys v0.20.0
) )
require ( require (
@ -31,5 +30,6 @@ require (
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 // indirect github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 // indirect
golang.org/x/crypto v0.23.0 // indirect golang.org/x/crypto v0.23.0 // indirect
golang.org/x/net v0.25.0 // indirect golang.org/x/net v0.25.0 // indirect
golang.org/x/sys v0.20.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect
) )

4
go.sum
View File

@ -47,8 +47,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.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 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= 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/v3 v3.0.2 h1:Mv5i8S1WY+BUNjTjX6lOnB3p8S9mvM+XwfY4R98gx0g=
github.com/thegeeklab/wp-plugin-go/v2 v2.3.1/go.mod h1:0t8M8txtEFiaB6RqLX8vLrxkqAo5FT5Hx7dztN592D4= 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 h1:6e0H+AkS+zDckwPCUrZkKX38mRaau4nL2uipkJpbkcI=
github.com/urfave/cli/v2 v2.27.2/go.mod h1:g0+79LmHHATl7DAcHO99smiR/T7uGLw84w8Y42x+4eM= 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= github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 h1:+qGGcbkzsfDQNPPe9UDgpxAWQrhbbBXOYJFQDq/dtJw=

View File

@ -11,8 +11,8 @@ import (
"time" "time"
"github.com/thegeeklab/wp-opentofu/plugin" "github.com/thegeeklab/wp-opentofu/plugin"
"github.com/thegeeklab/wp-plugin-go/v2/docs" plugin_docs "github.com/thegeeklab/wp-plugin-go/v3/docs"
wp_template "github.com/thegeeklab/wp-plugin-go/v2/template" plugin_template "github.com/thegeeklab/wp-plugin-go/v3/template"
) )
func main() { func main() {
@ -23,7 +23,7 @@ func main() {
p := plugin.New(nil) p := plugin.New(nil)
out, err := wp_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 { if err != nil {
panic(err) panic(err)
} }

View File

@ -8,7 +8,7 @@ import (
"os" "os"
"github.com/thegeeklab/wp-opentofu/tofu" "github.com/thegeeklab/wp-opentofu/tofu"
"github.com/thegeeklab/wp-plugin-go/v2/types" plugin_exec "github.com/thegeeklab/wp-plugin-go/v3/exec"
) )
var ( var (
@ -80,7 +80,7 @@ func (p *Plugin) Validate() error {
// Execute provides the implementation of the plugin. // Execute provides the implementation of the plugin.
func (p *Plugin) Execute() error { func (p *Plugin) Execute() error {
batchCmd := make([]*types.Cmd, 0) batchCmd := make([]*plugin_exec.Cmd, 0)
batchCmd = append(batchCmd, p.Settings.Tofu.Version()) batchCmd = append(batchCmd, p.Settings.Tofu.Version())
if p.Settings.TofuVersion != "" { if p.Settings.TofuVersion != "" {

View File

@ -4,7 +4,7 @@ import (
"fmt" "fmt"
"github.com/thegeeklab/wp-opentofu/tofu" "github.com/thegeeklab/wp-opentofu/tofu"
wp "github.com/thegeeklab/wp-plugin-go/v2/plugin" plugin_base "github.com/thegeeklab/wp-plugin-go/v3/plugin"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
) )
@ -12,7 +12,7 @@ import (
// Plugin implements provide the plugin. // Plugin implements provide the plugin.
type Plugin struct { type Plugin struct {
*wp.Plugin *plugin_base.Plugin
Settings *Settings Settings *Settings
} }
@ -25,15 +25,15 @@ type Settings struct {
Tofu tofu.Tofu Tofu tofu.Tofu
} }
func New(e wp.ExecuteFunc, build ...string) *Plugin { func New(e plugin_base.ExecuteFunc, build ...string) *Plugin {
p := &Plugin{ p := &Plugin{
Settings: &Settings{}, Settings: &Settings{},
} }
options := wp.Options{ options := plugin_base.Options{
Name: "wp-opentofu", Name: "wp-opentofu",
Description: "Manage infrastructure with OpenTofu", Description: "Manage infrastructure with OpenTofu",
Flags: Flags(p.Settings, wp.FlagsPluginCategory), Flags: Flags(p.Settings, plugin_base.FlagsPluginCategory),
Execute: p.run, Execute: p.run,
HideWoodpeckerFlags: true, HideWoodpeckerFlags: true,
} }
@ -50,7 +50,7 @@ func New(e wp.ExecuteFunc, build ...string) *Plugin {
options.Execute = e options.Execute = e
} }
p.Plugin = wp.New(options) p.Plugin = plugin_base.New(options)
return p return p
} }

View File

@ -2,10 +2,10 @@ package tofu
import ( import (
"fmt" "fmt"
"os"
"github.com/thegeeklab/wp-plugin-go/v2/types" plugin_exec "github.com/thegeeklab/wp-plugin-go/v3/exec"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
"golang.org/x/sys/execabs"
) )
const TofuBin = "/usr/local/bin/tofu" const TofuBin = "/usr/local/bin/tofu"
@ -36,14 +36,18 @@ type FmtOptions struct {
Check *bool `json:"check"` Check *bool `json:"check"`
} }
func (t *Tofu) Version() *types.Cmd { func (t *Tofu) Version() *plugin_exec.Cmd {
return &types.Cmd{ cmd := plugin_exec.Command(TofuBin, "version")
Cmd: execabs.Command(TofuBin, "version"),
Private: t.NoLog, if !t.NoLog {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
} }
return cmd
} }
func (t *Tofu) Init() *types.Cmd { func (t *Tofu) Init() *plugin_exec.Cmd {
args := []string{ args := []string{
"init", "init",
} }
@ -55,24 +59,30 @@ func (t *Tofu) Init() *types.Cmd {
// Fail tofu execution on prompt // Fail tofu execution on prompt
args = append(args, "-input=false") args = append(args, "-input=false")
return &types.Cmd{ cmd := plugin_exec.Command(TofuBin, args...)
Cmd: execabs.Command(TofuBin, args...), cmd.Stdout = os.Stdout
} cmd.Stderr = os.Stderr
return cmd
} }
func (t *Tofu) GetModules() *types.Cmd { func (t *Tofu) GetModules() *plugin_exec.Cmd {
return &types.Cmd{ cmd := plugin_exec.Command(TofuBin, "get")
Cmd: execabs.Command(TofuBin, "get"), cmd.Stdout = os.Stdout
} cmd.Stderr = os.Stderr
return cmd
} }
func (t *Tofu) Validate() *types.Cmd { func (t *Tofu) Validate() *plugin_exec.Cmd {
return &types.Cmd{ cmd := plugin_exec.Command(TofuBin, "validate")
Cmd: execabs.Command(TofuBin, "validate"), cmd.Stdout = os.Stdout
} cmd.Stderr = os.Stderr
return cmd
} }
func (t *Tofu) Fmt() *types.Cmd { func (t *Tofu) Fmt() *plugin_exec.Cmd {
args := []string{ args := []string{
"fmt", "fmt",
} }
@ -93,12 +103,14 @@ func (t *Tofu) Fmt() *types.Cmd {
args = append(args, fmt.Sprintf("-check=%t", *t.FmtOptions.Check)) args = append(args, fmt.Sprintf("-check=%t", *t.FmtOptions.Check))
} }
return &types.Cmd{ cmd := plugin_exec.Command(TofuBin, args...)
Cmd: execabs.Command(TofuBin, args...), cmd.Stdout = os.Stdout
} cmd.Stderr = os.Stderr
return cmd
} }
func (t *Tofu) Plan(destroy bool) *types.Cmd { func (t *Tofu) Plan(destroy bool) *plugin_exec.Cmd {
args := []string{ args := []string{
"plan", "plan",
} }
@ -129,13 +141,17 @@ func (t *Tofu) Plan(destroy bool) *types.Cmd {
args = append(args, "-refresh=false") args = append(args, "-refresh=false")
} }
return &types.Cmd{ cmd := plugin_exec.Command(TofuBin, args...)
Cmd: execabs.Command(TofuBin, args...),
Private: t.NoLog, if !t.NoLog {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
} }
return cmd
} }
func (t *Tofu) Apply() *types.Cmd { func (t *Tofu) Apply() *plugin_exec.Cmd {
args := []string{ args := []string{
"apply", "apply",
} }
@ -164,13 +180,17 @@ func (t *Tofu) Apply() *types.Cmd {
args = append(args, t.OutFile) args = append(args, t.OutFile)
} }
return &types.Cmd{ cmd := plugin_exec.Command(TofuBin, args...)
Cmd: execabs.Command(TofuBin, args...),
Private: t.NoLog, if !t.NoLog {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
} }
return cmd
} }
func (t *Tofu) Destroy() *types.Cmd { func (t *Tofu) Destroy() *plugin_exec.Cmd {
args := []string{ args := []string{
"destroy", "destroy",
} }
@ -193,8 +213,12 @@ func (t *Tofu) Destroy() *types.Cmd {
args = append(args, "-auto-approve") args = append(args, "-auto-approve")
return &types.Cmd{ cmd := plugin_exec.Command(TofuBin, args...)
Cmd: execabs.Command(TofuBin, args...),
Private: t.NoLog, if !t.NoLog {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
} }
return cmd
} }

View File

@ -449,7 +449,10 @@ func TestTofu_Apply(t *testing.T) {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
cmd := tt.tofu.Apply() cmd := tt.tofu.Apply()
assert.Equal(t, tt.want, cmd.Cmd.Args) assert.Equal(t, tt.want, cmd.Cmd.Args)
assert.Equal(t, tt.tofu.NoLog, cmd.Private)
if tt.tofu.NoLog {
assert.Equal(t, cmd.Stdout, nil)
}
}) })
} }
} }
@ -538,8 +541,12 @@ func TestTofu_Destroy(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
cmd := tt.tofu.Destroy() cmd := tt.tofu.Destroy()
assert.Equal(t, tt.want, cmd.Cmd.Args) assert.Equal(t, tt.want, cmd.Cmd.Args)
assert.Equal(t, tt.tofu.NoLog, cmd.Private)
if tt.tofu.NoLog {
assert.Equal(t, cmd.Stdout, nil)
}
}) })
} }
} }