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

View File

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

View File

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

View File

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

View File

@ -1,12 +1,13 @@
package git
import (
"github.com/thegeeklab/wp-plugin-go/v2/types"
"golang.org/x/sys/execabs"
"os"
plugin_exec "github.com/thegeeklab/wp-plugin-go/v3/exec"
)
// SubmoduleUpdate recursively initializes and updates submodules.
func (r *Repository) SubmoduleUpdate() *types.Cmd {
func (r *Repository) SubmoduleUpdate() *plugin_exec.Cmd {
args := []string{
"submodule",
"update",
@ -22,7 +23,9 @@ func (r *Repository) SubmoduleUpdate() *types.Cmd {
args = append(args, "--remote")
}
return &types.Cmd{
Cmd: execabs.Command(gitBin, args...),
}
cmd := plugin_exec.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/rs/zerolog v1.32.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
golang.org/x/sys v0.20.0
)
require (
@ -32,5 +31,6 @@ 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
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.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-git-clone/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

@ -11,9 +11,10 @@ import (
"time"
"github.com/rs/zerolog/log"
"github.com/thegeeklab/wp-plugin-go/v2/file"
"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_types "github.com/thegeeklab/wp-plugin-go/v3/types"
plugin_util "github.com/thegeeklab/wp-plugin-go/v3/util"
)
const (
@ -73,8 +74,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)
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)
}
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 {
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 {
return fmt.Errorf("failed to check working directory: %w", err)
}
@ -175,7 +176,7 @@ func (p *Plugin) Execute() 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 {
return fmt.Errorf("%w: failed to read submodule-override input", ErrTypeAssertionFailed)
}

View File

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

View File

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