0
0
mirror of https://github.com/thegeeklab/wp-git-action.git synced 2024-11-21 13:50:39 +00:00

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

This commit is contained in:
Robert Kaussow 2024-05-17 21:49:59 +02:00 committed by GitHub
parent 9beccd28de
commit 99812a77c7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 89 additions and 108 deletions

View File

@ -2,38 +2,38 @@ 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() *types.Cmd { func (r *Repository) FetchSource() *plugin_exec.Cmd {
args := []string{ args := []string{
"fetch", "fetch",
"origin", "origin",
fmt.Sprintf("+%s:", r.Branch), fmt.Sprintf("+%s:", r.Branch),
} }
cmd := &types.Cmd{ cmd := plugin_exec.Command(gitBin, args...)
Cmd: execabs.Command(gitBin, args...), cmd.Stdout = os.Stdout
} cmd.Stderr = os.Stderr
cmd.Dir = r.WorkDir cmd.Dir = r.WorkDir
return cmd return cmd
} }
// CheckoutHead handles branch checkout. // CheckoutHead handles branch checkout.
func (r *Repository) CheckoutHead() *types.Cmd { func (r *Repository) CheckoutHead() *plugin_exec.Cmd {
args := []string{ args := []string{
"checkout", "checkout",
"-qf", "-qf",
r.Branch, r.Branch,
} }
cmd := &types.Cmd{ cmd := plugin_exec.Command(gitBin, args...)
Cmd: execabs.Command(gitBin, args...), cmd.Stdout = os.Stdout
} cmd.Stderr = os.Stderr
cmd.Dir = r.WorkDir cmd.Dir = r.WorkDir
return cmd return cmd

View File

@ -1,28 +1,23 @@
package git package git
import ( import (
"io" "os"
"github.com/thegeeklab/wp-plugin-go/v2/types" plugin_exec "github.com/thegeeklab/wp-plugin-go/v3/exec"
"golang.org/x/sys/execabs"
) )
// Add updates the index to match the working tree. // Add updates the index to match the working tree.
func (r *Repository) Add() *types.Cmd { func (r *Repository) Add() *plugin_exec.Cmd {
cmd := &types.Cmd{ cmd := plugin_exec.Command(gitBin, "add", "--all")
Cmd: execabs.Command( cmd.Stdout = os.Stdout
gitBin, cmd.Stderr = os.Stderr
"add",
"--all",
),
}
cmd.Dir = r.WorkDir cmd.Dir = r.WorkDir
return cmd return cmd
} }
// IsCleanTree returns non-zero if diff between index and local repository. // IsCleanTree returns non-zero if diff between index and local repository.
func (r *Repository) IsCleanTree() *types.Cmd { func (r *Repository) IsCleanTree() *plugin_exec.Cmd {
args := []string{ args := []string{
"diff-index", "diff-index",
"--quiet", "--quiet",
@ -30,19 +25,15 @@ func (r *Repository) IsCleanTree() *types.Cmd {
"--ignore-submodules", "--ignore-submodules",
} }
cmd := &types.Cmd{ cmd := plugin_exec.Command(gitBin, args...)
Cmd: execabs.Command(gitBin, args...),
}
cmd.Dir = r.WorkDir cmd.Dir = r.WorkDir
cmd.Stdout = io.Discard cmd.Trace = false
cmd.Stderr = io.Discard
cmd.SetTrace(false)
return cmd return cmd
} }
// Commit creates a new commit with the specified commit message. // Commit creates a new commit with the specified commit message.
func (r *Repository) Commit() *types.Cmd { func (r *Repository) Commit() *plugin_exec.Cmd {
args := []string{ args := []string{
"commit", "commit",
"-m", "-m",
@ -57,9 +48,9 @@ func (r *Repository) Commit() *types.Cmd {
args = append(args, "--no-verify") args = append(args, "--no-verify")
} }
cmd := &types.Cmd{ cmd := plugin_exec.Command(gitBin, args...)
Cmd: execabs.Command(gitBin, args...), cmd.Stdout = os.Stdout
} cmd.Stderr = os.Stderr
cmd.Dir = r.WorkDir cmd.Dir = r.WorkDir
return cmd return cmd

View File

@ -3,13 +3,12 @@ package git
import ( import (
"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"
) )
// ConfigAutocorrect sets the local git autocorrect configuration for the given repository. // ConfigAutocorrect sets the local git autocorrect configuration for the given repository.
// The autocorrect setting determines how git handles minor typos in commands. // The autocorrect setting determines how git handles minor typos in commands.
func (r *Repository) ConfigAutocorrect() *types.Cmd { func (r *Repository) ConfigAutocorrect() *plugin_exec.Cmd {
args := []string{ args := []string{
"config", "config",
"--local", "--local",
@ -17,17 +16,15 @@ func (r *Repository) ConfigAutocorrect() *types.Cmd {
r.Autocorrect, r.Autocorrect,
} }
cmd := &types.Cmd{ cmd := plugin_exec.Command(gitBin, args...)
Cmd: execabs.Command(gitBin, args...),
}
cmd.Dir = r.WorkDir cmd.Dir = r.WorkDir
cmd.SetTrace(false) cmd.Trace = false
return cmd return cmd
} }
// ConfigUserEmail sets the global git author email. // ConfigUserEmail sets the global git author email.
func (r *Repository) ConfigUserEmail() *types.Cmd { func (r *Repository) ConfigUserEmail() *plugin_exec.Cmd {
args := []string{ args := []string{
"config", "config",
"--local", "--local",
@ -35,17 +32,15 @@ func (r *Repository) ConfigUserEmail() *types.Cmd {
r.Author.Email, r.Author.Email,
} }
cmd := &types.Cmd{ cmd := plugin_exec.Command(gitBin, args...)
Cmd: execabs.Command(gitBin, args...),
}
cmd.Dir = r.WorkDir cmd.Dir = r.WorkDir
cmd.SetTrace(false) cmd.Trace = false
return cmd return cmd
} }
// ConfigUserName configures the user.name git config setting for the given repository. // ConfigUserName configures the user.name git config setting for the given repository.
func (r *Repository) ConfigUserName() *types.Cmd { func (r *Repository) ConfigUserName() *plugin_exec.Cmd {
args := []string{ args := []string{
"config", "config",
"--local", "--local",
@ -53,17 +48,15 @@ func (r *Repository) ConfigUserName() *types.Cmd {
r.Author.Name, r.Author.Name,
} }
cmd := &types.Cmd{ cmd := plugin_exec.Command(gitBin, args...)
Cmd: execabs.Command(gitBin, args...),
}
cmd.Dir = r.WorkDir cmd.Dir = r.WorkDir
cmd.SetTrace(false) cmd.Trace = false
return cmd return cmd
} }
// ConfigSSLVerify configures the http.sslVerify git config setting for the given repository. // ConfigSSLVerify configures the http.sslVerify git config setting for the given repository.
func (r *Repository) ConfigSSLVerify(skipVerify bool) *types.Cmd { func (r *Repository) ConfigSSLVerify(skipVerify bool) *plugin_exec.Cmd {
args := []string{ args := []string{
"config", "config",
"--local", "--local",
@ -71,17 +64,15 @@ func (r *Repository) ConfigSSLVerify(skipVerify bool) *types.Cmd {
strconv.FormatBool(!skipVerify), strconv.FormatBool(!skipVerify),
} }
cmd := &types.Cmd{ cmd := plugin_exec.Command(gitBin, args...)
Cmd: execabs.Command(gitBin, args...),
}
cmd.Dir = r.WorkDir cmd.Dir = r.WorkDir
cmd.SetTrace(false) cmd.Trace = false
return cmd 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",
"--local", "--local",
@ -89,11 +80,9 @@ 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.Dir = r.WorkDir cmd.Dir = r.WorkDir
cmd.SetTrace(false) cmd.Trace = false
return cmd return cmd
} }

View File

@ -1,21 +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"
) )
// Init creates a new Git repository in the specified directory. // Init creates a new Git repository in the specified directory.
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,
} }
cmd := &types.Cmd{ cmd := plugin_exec.Command(gitBin, args...)
Cmd: execabs.Command(gitBin, args...), cmd.Stdout = os.Stdout
} cmd.Stderr = os.Stderr
cmd.Dir = r.WorkDir cmd.Dir = r.WorkDir
return cmd return cmd

View File

@ -2,29 +2,29 @@ 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"
) )
// RemoteRemove drops the defined remote from a git repo. // RemoteRemove drops the defined remote from a git repo.
func (r *Repository) RemoteRemove() *types.Cmd { func (r *Repository) RemoteRemove() *plugin_exec.Cmd {
args := []string{ args := []string{
"remote", "remote",
"rm", "rm",
r.RemoteName, r.RemoteName,
} }
cmd := &types.Cmd{ cmd := plugin_exec.Command(gitBin, args...)
Cmd: execabs.Command(gitBin, args...), cmd.Stdout = os.Stdout
} cmd.Stderr = os.Stderr
cmd.Dir = r.WorkDir cmd.Dir = r.WorkDir
return cmd return cmd
} }
// 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",
@ -32,16 +32,16 @@ func (r *Repository) RemoteAdd() *types.Cmd {
r.RemoteURL, r.RemoteURL,
} }
cmd := &types.Cmd{ cmd := plugin_exec.Command(gitBin, args...)
Cmd: execabs.Command(gitBin, args...), cmd.Stdout = os.Stdout
} cmd.Stderr = os.Stderr
cmd.Dir = r.WorkDir cmd.Dir = r.WorkDir
return cmd return cmd
} }
// RemotePush pushs the changes from the local head to a remote branch. // RemotePush pushs the changes from the local head to a remote branch.
func (r *Repository) RemotePush() *types.Cmd { func (r *Repository) RemotePush() *plugin_exec.Cmd {
args := []string{ args := []string{
"push", "push",
r.RemoteName, r.RemoteName,
@ -56,9 +56,9 @@ func (r *Repository) RemotePush() *types.Cmd {
args = append(args, "--follow-tags") args = append(args, "--follow-tags")
} }
cmd := &types.Cmd{ cmd := plugin_exec.Command(gitBin, args...)
Cmd: execabs.Command(gitBin, args...), cmd.Stdout = os.Stdout
} cmd.Stderr = os.Stderr
cmd.Dir = r.WorkDir cmd.Dir = r.WorkDir
return cmd return cmd

4
go.mod
View File

@ -5,9 +5,8 @@ go 1.22
require ( require (
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

@ -46,8 +46,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-action/plugin" "github.com/thegeeklab/wp-git-action/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

@ -9,9 +9,9 @@ import (
"github.com/rs/zerolog" "github.com/rs/zerolog"
"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_util "github.com/thegeeklab/wp-plugin-go/v3/util"
) )
var ( var (
@ -106,8 +106,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)
gitEnv := []string{ gitEnv := []string{
"GIT_AUTHOR_NAME", "GIT_AUTHOR_NAME",
"GIT_AUTHOR_EMAIL", "GIT_AUTHOR_EMAIL",
@ -143,12 +143,12 @@ func (p *Plugin) Execute() error {
} }
defer os.RemoveAll(p.Settings.Repo.WorkDir) defer os.RemoveAll(p.Settings.Repo.WorkDir)
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)
} }
@ -215,7 +215,7 @@ func (p *Plugin) Execute() error {
// handleClone clones the remote repository into the configured working directory. // handleClone clones the remote repository into the configured working directory.
// If the working directory is not empty, it returns an error. // If the working directory is not empty, it returns an error.
func (p *Plugin) handleClone() error { func (p *Plugin) handleClone() error {
var batchCmd []*types.Cmd var batchCmd []*plugin_exec.Cmd
if !p.Settings.Repo.IsEmpty { if !p.Settings.Repo.IsEmpty {
return fmt.Errorf("%w: %s exists and not empty", ErrGitCloneDestintionNotValid, p.Settings.Repo.WorkDir) return fmt.Errorf("%w: %s exists and not empty", ErrGitCloneDestintionNotValid, p.Settings.Repo.WorkDir)

View File

@ -4,7 +4,7 @@ import (
"fmt" "fmt"
"github.com/thegeeklab/wp-git-action/git" "github.com/thegeeklab/wp-git-action/git"
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
} }
@ -40,15 +40,15 @@ type Pages struct {
type GitAction string type GitAction string
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-action", Name: "wp-git-action",
Description: "Perform git actions", Description: "Perform git actions",
Flags: Flags(p.Settings, wp.FlagsPluginCategory), Flags: Flags(p.Settings, plugin_base.FlagsPluginCategory),
Execute: p.run, Execute: p.run,
HideWoodpeckerFlags: true, HideWoodpeckerFlags: true,
} }
@ -65,7 +65,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

@ -1,12 +1,12 @@
package plugin package plugin
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"
) )
func SyncDirectories(exclude []string, del bool, src, dest string, debug bool) *types.Cmd { func SyncDirectories(exclude []string, del bool, src, dest string, debug bool) *plugin_exec.Cmd {
args := []string{ args := []string{
"-r", "-r",
"--exclude", "--exclude",
@ -41,9 +41,9 @@ func SyncDirectories(exclude []string, del bool, src, dest string, debug bool) *
dest, dest,
) )
cmd := &types.Cmd{ cmd := plugin_exec.Command("rsync", args...)
Cmd: execabs.Command("rsync", args...), cmd.Stdout = os.Stdout
} cmd.Stderr = os.Stderr
cmd.Dir = src cmd.Dir = src
return cmd return cmd

View File

@ -5,7 +5,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"github.com/thegeeklab/wp-plugin-go/v2/types" plugin_exec "github.com/thegeeklab/wp-plugin-go/v3/exec"
) )
const ( const (
@ -30,7 +30,7 @@ func WriteNetrc(path, machine, login, password string) error {
} }
// ExecBatch executes a batch of commands. If any command in the batch fails, the function will return the error. // ExecBatch executes a batch of commands. If any command in the batch fails, the function will return the error.
func ExecBatch(batchCmd []*types.Cmd) error { func ExecBatch(batchCmd []*plugin_exec.Cmd) error {
for _, cmd := range batchCmd { for _, cmd := range batchCmd {
if cmd == nil { if cmd == nil {
continue continue