0
0
mirror of https://github.com/thegeeklab/wp-git-clone.git synced 2024-11-21 14:10:38 +00:00

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

This commit is contained in:
Robert Kaussow 2024-05-17 21:50:05 +02:00 committed by GitHub
parent ccfad42ff3
commit 6e5ad068d0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 111 additions and 93 deletions

View File

@ -2,13 +2,13 @@ package git
import ( import (
"fmt" "fmt"
"os"
"github.com/thegeeklab/wp-plugin-go/v2/types" plugin_exec "github.com/thegeeklab/wp-plugin-go/v3/exec"
"golang.org/x/sys/execabs"
) )
// FetchSource fetches the source from remote. // FetchSource fetches the source from remote.
func (r *Repository) FetchSource(ref string) *types.Cmd { func (r *Repository) FetchSource(ref string) *plugin_exec.Cmd {
args := []string{ args := []string{
"fetch", "fetch",
} }
@ -24,13 +24,15 @@ func (r *Repository) FetchSource(ref string) *types.Cmd {
args = append(args, "origin") args = append(args, "origin")
args = append(args, fmt.Sprintf("+%s:", ref)) args = append(args, fmt.Sprintf("+%s:", ref))
return &types.Cmd{ cmd := plugin_exec.Command(gitBin, args...)
Cmd: execabs.Command(gitBin, args...), cmd.Stdout = os.Stdout
} cmd.Stderr = os.Stderr
return cmd
} }
// FetchTags fetches the source from remote. // FetchTags fetches the source from remote.
func (r *Repository) FetchTags() *types.Cmd { func (r *Repository) FetchTags() *plugin_exec.Cmd {
args := []string{ args := []string{
"fetch", "fetch",
"--tags", "--tags",
@ -38,25 +40,29 @@ func (r *Repository) FetchTags() *types.Cmd {
"origin", "origin",
} }
return &types.Cmd{ cmd := plugin_exec.Command(gitBin, args...)
Cmd: execabs.Command(gitBin, args...), cmd.Stdout = os.Stdout
} cmd.Stderr = os.Stderr
return cmd
} }
// FetchLFS fetches lfs. // FetchLFS fetches lfs.
func (r *Repository) FetchLFS() *types.Cmd { func (r *Repository) FetchLFS() *plugin_exec.Cmd {
args := []string{ args := []string{
"lfs", "lfs",
"fetch", "fetch",
} }
return &types.Cmd{ cmd := plugin_exec.Command(gitBin, args...)
Cmd: execabs.Command(gitBin, args...), cmd.Stdout = os.Stdout
} cmd.Stderr = os.Stderr
return cmd
} }
// CheckoutHead handles head checkout. // CheckoutHead handles head checkout.
func (r *Repository) CheckoutHead() *types.Cmd { func (r *Repository) CheckoutHead() *plugin_exec.Cmd {
args := []string{ args := []string{
"checkout", "checkout",
"--force", "--force",
@ -64,13 +70,15 @@ func (r *Repository) CheckoutHead() *types.Cmd {
"FETCH_HEAD", "FETCH_HEAD",
} }
return &types.Cmd{ cmd := plugin_exec.Command(gitBin, args...)
Cmd: execabs.Command(gitBin, args...), cmd.Stdout = os.Stdout
} cmd.Stderr = os.Stderr
return cmd
} }
// CheckoutSha handles commit checkout. // CheckoutSha handles commit checkout.
func (r *Repository) CheckoutSha() *types.Cmd { func (r *Repository) CheckoutSha() *plugin_exec.Cmd {
args := []string{ args := []string{
"reset", "reset",
"--hard", "--hard",
@ -78,19 +86,23 @@ func (r *Repository) CheckoutSha() *types.Cmd {
r.CommitSha, r.CommitSha,
} }
return &types.Cmd{ cmd := plugin_exec.Command(gitBin, args...)
Cmd: execabs.Command(gitBin, args...), cmd.Stdout = os.Stdout
} cmd.Stderr = os.Stderr
return cmd
} }
// CheckoutLFS handles commit checkout. // CheckoutLFS handles commit checkout.
func (r *Repository) CheckoutLFS() *types.Cmd { func (r *Repository) CheckoutLFS() *plugin_exec.Cmd {
args := []string{ args := []string{
"lfs", "lfs",
"checkout", "checkout",
} }
return &types.Cmd{ cmd := plugin_exec.Command(gitBin, args...)
Cmd: execabs.Command(gitBin, args...), cmd.Stdout = os.Stdout
} cmd.Stderr = os.Stderr
return cmd
} }

View File

@ -4,12 +4,11 @@ import (
"fmt" "fmt"
"strconv" "strconv"
"github.com/thegeeklab/wp-plugin-go/v2/types" plugin_exec "github.com/thegeeklab/wp-plugin-go/v3/exec"
"golang.org/x/sys/execabs"
) )
// ConfigSSLVerify disables globally the git ssl verification. // ConfigSSLVerify disables globally the git ssl verification.
func (r *Repository) ConfigSSLVerify(skipVerify bool) *types.Cmd { func (r *Repository) ConfigSSLVerify(skipVerify bool) *plugin_exec.Cmd {
args := []string{ args := []string{
"config", "config",
"--global", "--global",
@ -17,13 +16,14 @@ func (r *Repository) ConfigSSLVerify(skipVerify bool) *types.Cmd {
strconv.FormatBool(!skipVerify), strconv.FormatBool(!skipVerify),
} }
return &types.Cmd{ cmd := plugin_exec.Command(gitBin, args...)
Cmd: execabs.Command(gitBin, args...), cmd.Trace = false
}
return cmd
} }
// ConfigSafeDirectory disables globally the git ssl verification. // ConfigSafeDirectory disables globally the git ssl verification.
func (r *Repository) ConfigSafeDirectory() *types.Cmd { func (r *Repository) ConfigSafeDirectory() *plugin_exec.Cmd {
args := []string{ args := []string{
"config", "config",
"--global", "--global",
@ -32,14 +32,15 @@ func (r *Repository) ConfigSafeDirectory() *types.Cmd {
r.SafeDirectory, r.SafeDirectory,
} }
return &types.Cmd{ cmd := plugin_exec.Command(gitBin, args...)
Cmd: execabs.Command(gitBin, args...), cmd.Trace = false
}
return cmd
} }
// ConfigRemapSubmodule returns a git command that, when executed configures git to // ConfigRemapSubmodule returns a git command that, when executed configures git to
// remap submodule urls. // remap submodule urls.
func (r *Repository) ConfigRemapSubmodule(name, url string) *types.Cmd { func (r *Repository) ConfigRemapSubmodule(name, url string) *plugin_exec.Cmd {
args := []string{ args := []string{
"config", "config",
"--global", "--global",
@ -47,13 +48,14 @@ func (r *Repository) ConfigRemapSubmodule(name, url string) *types.Cmd {
url, url,
} }
return &types.Cmd{ cmd := plugin_exec.Command(gitBin, args...)
Cmd: execabs.Command(gitBin, args...), cmd.Trace = false
}
return cmd
} }
// ConfigSSHCommand sets custom SSH key. // ConfigSSHCommand sets custom SSH key.
func (r *Repository) ConfigSSHCommand(sshKey string) *types.Cmd { func (r *Repository) ConfigSSHCommand(sshKey string) *plugin_exec.Cmd {
args := []string{ args := []string{
"config", "config",
"--global", "--global",
@ -61,10 +63,8 @@ func (r *Repository) ConfigSSHCommand(sshKey string) *types.Cmd {
"ssh -i " + sshKey, "ssh -i " + sshKey,
} }
cmd := &types.Cmd{ cmd := plugin_exec.Command(gitBin, args...)
Cmd: execabs.Command(gitBin, args...), cmd.Trace = false
}
cmd.SetTrace(false)
return cmd return cmd
} }

View File

@ -1,19 +1,22 @@
package git package git
import ( import (
"github.com/thegeeklab/wp-plugin-go/v2/types" "os"
"golang.org/x/sys/execabs"
plugin_exec "github.com/thegeeklab/wp-plugin-go/v3/exec"
) )
// RemoteRemove drops the defined remote from a git repo. // RemoteRemove drops the defined remote from a git repo.
func (r *Repository) Init() *types.Cmd { func (r *Repository) Init() *plugin_exec.Cmd {
args := []string{ args := []string{
"init", "init",
"-b", "-b",
r.Branch, r.Branch,
} }
return &types.Cmd{ cmd := plugin_exec.Command(gitBin, args...)
Cmd: execabs.Command(gitBin, args...), cmd.Stdout = os.Stdout
} cmd.Stderr = os.Stderr
return cmd
} }

View File

@ -1,12 +1,13 @@
package git package git
import ( import (
"github.com/thegeeklab/wp-plugin-go/v2/types" "os"
"golang.org/x/sys/execabs"
plugin_exec "github.com/thegeeklab/wp-plugin-go/v3/exec"
) )
// RemoteAdd adds an additional remote to a git repo. // RemoteAdd adds an additional remote to a git repo.
func (r *Repository) RemoteAdd() *types.Cmd { func (r *Repository) RemoteAdd() *plugin_exec.Cmd {
args := []string{ args := []string{
"remote", "remote",
"add", "add",
@ -14,7 +15,9 @@ func (r *Repository) RemoteAdd() *types.Cmd {
r.RemoteURL, r.RemoteURL,
} }
return &types.Cmd{ cmd := plugin_exec.Command(gitBin, args...)
Cmd: execabs.Command(gitBin, args...), cmd.Stdout = os.Stdout
} cmd.Stderr = os.Stderr
return cmd
} }

View File

@ -1,12 +1,13 @@
package git package git
import ( import (
"github.com/thegeeklab/wp-plugin-go/v2/types" "os"
"golang.org/x/sys/execabs"
plugin_exec "github.com/thegeeklab/wp-plugin-go/v3/exec"
) )
// SubmoduleUpdate recursively initializes and updates submodules. // SubmoduleUpdate recursively initializes and updates submodules.
func (r *Repository) SubmoduleUpdate() *types.Cmd { func (r *Repository) SubmoduleUpdate() *plugin_exec.Cmd {
args := []string{ args := []string{
"submodule", "submodule",
"update", "update",
@ -22,7 +23,9 @@ func (r *Repository) SubmoduleUpdate() *types.Cmd {
args = append(args, "--remote") args = append(args, "--remote")
} }
return &types.Cmd{ cmd := plugin_exec.Command(gitBin, args...)
Cmd: execabs.Command(gitBin, args...), cmd.Stdout = os.Stdout
} cmd.Stderr = os.Stderr
return cmd
} }

4
go.mod
View File

@ -6,9 +6,8 @@ require (
github.com/cenkalti/backoff/v4 v4.3.0 github.com/cenkalti/backoff/v4 v4.3.0
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 (
@ -32,5 +31,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

@ -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.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-git-clone/plugin" "github.com/thegeeklab/wp-git-clone/plugin"
"github.com/thegeeklab/wp-plugin-go/v2/docs" plugin_docs "github.com/thegeeklab/wp-plugin-go/v3/docs"
"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 := 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

@ -11,9 +11,10 @@ import (
"time" "time"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/thegeeklab/wp-plugin-go/v2/file" plugin_exec "github.com/thegeeklab/wp-plugin-go/v3/exec"
"github.com/thegeeklab/wp-plugin-go/v2/types" plugin_file "github.com/thegeeklab/wp-plugin-go/v3/file"
"github.com/thegeeklab/wp-plugin-go/v2/util" plugin_types "github.com/thegeeklab/wp-plugin-go/v3/types"
plugin_util "github.com/thegeeklab/wp-plugin-go/v3/util"
) )
const ( const (
@ -73,8 +74,8 @@ func (p *Plugin) Validate() error {
func (p *Plugin) Execute() error { func (p *Plugin) Execute() error {
var err error var err error
homeDir := util.GetUserHomeDir() homeDir := plugin_util.GetUserHomeDir()
batchCmd := make([]*types.Cmd, 0) batchCmd := make([]*plugin_exec.Cmd, 0)
fmt.Println(p.Settings.Repo.WorkDir) fmt.Println(p.Settings.Repo.WorkDir)
@ -83,12 +84,12 @@ func (p *Plugin) Execute() error {
return fmt.Errorf("failed to create working directory: %w", err) return fmt.Errorf("failed to create working directory: %w", err)
} }
p.Settings.Repo.IsEmpty, err = file.IsDirEmpty(p.Settings.Repo.WorkDir) p.Settings.Repo.IsEmpty, err = plugin_file.IsDirEmpty(p.Settings.Repo.WorkDir)
if err != nil { if err != nil {
return fmt.Errorf("failed to check working directory: %w", err) return fmt.Errorf("failed to check working directory: %w", err)
} }
isDir, err := file.IsDir(filepath.Join(p.Settings.Repo.WorkDir, ".git")) isDir, err := plugin_file.IsDir(filepath.Join(p.Settings.Repo.WorkDir, ".git"))
if err != nil { if err != nil {
return fmt.Errorf("failed to check working directory: %w", err) return fmt.Errorf("failed to check working directory: %w", err)
} }
@ -175,7 +176,7 @@ func (p *Plugin) Execute() error {
} }
func (p *Plugin) FlagsFromContext() error { func (p *Plugin) FlagsFromContext() error {
submodules, ok := p.Context.Generic("submodule-override").(*types.MapFlag) submodules, ok := p.Context.Generic("submodule-override").(*plugin_types.MapFlag)
if !ok { if !ok {
return fmt.Errorf("%w: failed to read submodule-override input", ErrTypeAssertionFailed) return fmt.Errorf("%w: failed to read submodule-override input", ErrTypeAssertionFailed)
} }

View File

@ -4,8 +4,8 @@ import (
"fmt" "fmt"
"github.com/thegeeklab/wp-git-clone/git" "github.com/thegeeklab/wp-git-clone/git"
wp "github.com/thegeeklab/wp-plugin-go/v2/plugin" plugin_base "github.com/thegeeklab/wp-plugin-go/v3/plugin"
"github.com/thegeeklab/wp-plugin-go/v2/types" plugin_types "github.com/thegeeklab/wp-plugin-go/v3/types"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
) )
@ -13,7 +13,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
} }
@ -36,15 +36,15 @@ type Settings struct {
Repo git.Repository Repo git.Repository
} }
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-git-clone", Name: "wp-git-clone",
Description: "Clone git repository", Description: "Clone git repository",
Flags: Flags(p.Settings, wp.FlagsPluginCategory), Flags: Flags(p.Settings, plugin_base.FlagsPluginCategory),
Execute: p.run, Execute: p.run,
HideWoodpeckerFlags: true, HideWoodpeckerFlags: true,
} }
@ -61,7 +61,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
} }
@ -164,7 +164,7 @@ func Flags(settings *Settings, category string) []cli.Flag {
Name: "submodule-override", Name: "submodule-override",
Usage: "JSON map of submodule overrides", Usage: "JSON map of submodule overrides",
EnvVars: []string{"PLUGIN_SUBMODULE_OVERRIDE"}, EnvVars: []string{"PLUGIN_SUBMODULE_OVERRIDE"},
Value: &types.MapFlag{}, Value: &plugin_types.MapFlag{},
Category: category, Category: category,
}, },
&cli.BoolFlag{ &cli.BoolFlag{

View File

@ -9,8 +9,7 @@ import (
"github.com/cenkalti/backoff/v4" "github.com/cenkalti/backoff/v4"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/thegeeklab/wp-plugin-go/v2/types" plugin_exec "github.com/thegeeklab/wp-plugin-go/v3/exec"
"golang.org/x/sys/execabs"
) )
const ( const (
@ -35,13 +34,10 @@ func newBackoff(maxRetries uint64) backoff.BackOff {
return backoff.WithMaxRetries(b, maxRetries) return backoff.WithMaxRetries(b, maxRetries)
} }
func retryCmd(cmd *types.Cmd) error { func retryCmd(cmd *plugin_exec.Cmd) error {
backoffOps := func() error { backoffOps := func() error {
// copy the original command // copy the original command
//nolint:gosec retry := plugin_exec.Command(cmd.Cmd.Path, cmd.Cmd.Args...)
retry := &types.Cmd{
Cmd: execabs.Command(cmd.Cmd.Path, cmd.Cmd.Args...),
}
retry.Env = cmd.Env retry.Env = cmd.Env
retry.Stdout = cmd.Stdout retry.Stdout = cmd.Stdout
retry.Stderr = cmd.Stderr retry.Stderr = cmd.Stderr