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

refactor: add more linters and fix findings (#27)

This commit is contained in:
Robert Kaussow 2023-02-08 10:16:10 +01:00 committed by GitHub
parent b00aab95c2
commit 22ebe21da3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 286 additions and 123 deletions

View File

@ -8,7 +8,7 @@ platform:
steps: steps:
- name: deps - name: deps
image: golang:1.19 image: golang:1.20
commands: commands:
- make deps - make deps
volumes: volumes:
@ -16,7 +16,7 @@ steps:
path: /go path: /go
- name: lint - name: lint
image: golang:1.19 image: golang:1.20
commands: commands:
- make lint - make lint
volumes: volumes:
@ -24,7 +24,7 @@ steps:
path: /go path: /go
- name: test - name: test
image: golang:1.19 image: golang:1.20
commands: commands:
- make test - make test
volumes: volumes:
@ -51,7 +51,7 @@ platform:
steps: steps:
- name: build - name: build
image: techknowlogick/xgo:go-1.19.x image: techknowlogick/xgo:go-1.20.x
commands: commands:
- ln -s /drone/src /source - ln -s /drone/src /source
- make release - make release
@ -292,6 +292,6 @@ depends_on:
--- ---
kind: signature kind: signature
hmac: 8b920283ba7cc82bad64ea0eb55b8cf3de5aae95ca0f7018bac9f0deb8b22244 hmac: 89039f09e352a636edf52307e5e48f8bd54f4d9f0b2cf55dabddfdca88702bda
... ...

View File

@ -1,25 +1,92 @@
linters: linters:
enable:
- gosimple
- deadcode
- typecheck
- govet
- errcheck
- staticcheck
- unused
- structcheck
- varcheck
- dupl
- gofmt
- misspell
- gocritic
- bidichk
- ineffassign
- revive
- gofumpt
- depguard
enable-all: false enable-all: false
disable-all: true disable-all: true
enable:
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- typecheck
- unused
- asasalint
- asciicheck
- bidichk
- bodyclose
- containedctx
- contextcheck
- decorder
- depguard
- dogsled
- dupl
- dupword
- durationcheck
- errchkjson
- errname
- errorlint
- execinquery
- exhaustive
- exportloopref
- forcetypeassert
- ginkgolinter
- gocheckcompilerdirectives
- gochecknoglobals
- gochecknoinits
- gocognit
- goconst
- gocritic
- gocyclo
- godot
- godox
- goerr113
- gofmt
- gofumpt
- goheader
- goimports
- gomnd
- gomoddirectives
- gomodguard
- goprintffuncname
- gosec
- grouper
- importas
- interfacebloat
- ireturn
- lll
- loggercheck
- maintidx
- makezero
- misspell
- musttag
- nakedret
- nestif
- nilerr
- nilnil
- nlreturn
- noctx
- nolintlint
- nonamedreturns
- nosprintfhostport
- prealloc
- predeclared
- promlinter
- reassign
- revive
# - rowserrcheck
# - sqlclosecheck
# - structcheck
- stylecheck
- tagliatelle
- tenv
- testableexamples
- thelper
- tparallel
- unconvert
- unparam
- usestdlibvars
# - wastedassign
- whitespace
- wsl
fast: false fast: false
run: run:
@ -28,4 +95,4 @@ run:
linters-settings: linters-settings:
gofumpt: gofumpt:
extra-rules: true extra-rules: true
lang-version: "1.18" lang-version: "1.20"

View File

@ -19,7 +19,7 @@ GOLANGCI_LINT_PACKAGE ?= github.com/golangci/golangci-lint/cmd/golangci-lint@$(G
XGO_PACKAGE ?= src.techknowlogick.com/xgo@latest XGO_PACKAGE ?= src.techknowlogick.com/xgo@latest
GENERATE ?= GENERATE ?=
XGO_VERSION := go-1.19.x XGO_VERSION := go-1.20.x
XGO_TARGETS ?= linux/amd64,linux/arm-6,linux/arm-7,linux/arm64 XGO_TARGETS ?= linux/amd64,linux/arm-6,linux/arm-7,linux/arm64
TARGETOS ?= linux TARGETOS ?= linux

View File

@ -10,7 +10,7 @@ func settingsFlags(settings *plugin.Settings, category string) []cli.Flag {
return []cli.Flag{ return []cli.Flag{
&cli.StringSliceFlag{ &cli.StringSliceFlag{
Name: "action", Name: "action",
Usage: "git action to to execute", Usage: "git action to execute",
EnvVars: []string{"PLUGIN_ACTION"}, EnvVars: []string{"PLUGIN_ACTION"},
Destination: &settings.Action, Destination: &settings.Action,
Required: true, Required: true,

View File

@ -11,6 +11,7 @@ import (
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
) )
//nolint:gochecknoglobals
var ( var (
BuildVersion = "devel" BuildVersion = "devel"
BuildDate = "00000000" BuildDate = "00000000"

View File

@ -3,16 +3,21 @@ package git
import ( import (
"fmt" "fmt"
"os" "os"
"os/exec"
"golang.org/x/sys/execabs"
) )
// FetchSource fetches the source from remote. // FetchSource fetches the source from remote.
func FetchSource(repo Repository) *exec.Cmd { func FetchSource(repo Repository) *execabs.Cmd {
cmd := exec.Command( args := []string{
"git",
"fetch", "fetch",
"origin", "origin",
fmt.Sprintf("+%s:", repo.Branch), fmt.Sprintf("+%s:", repo.Branch),
}
cmd := execabs.Command(
gitBin,
args...,
) )
cmd.Dir = repo.WorkDir cmd.Dir = repo.WorkDir
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
@ -21,12 +26,16 @@ func FetchSource(repo Repository) *exec.Cmd {
} }
// CheckoutHead handles branch checkout. // CheckoutHead handles branch checkout.
func CheckoutHead(repo Repository) *exec.Cmd { func CheckoutHead(repo Repository) *execabs.Cmd {
cmd := exec.Command( args := []string{
"git",
"checkout", "checkout",
"-qf", "-qf",
repo.Branch, repo.Branch,
}
cmd := execabs.Command(
gitBin,
args...,
) )
cmd.Dir = repo.WorkDir cmd.Dir = repo.WorkDir
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr

View File

@ -2,13 +2,14 @@ package git
import ( import (
"os" "os"
"os/exec"
"golang.org/x/sys/execabs"
) )
// ForceAdd forces the addition of all dirty files. // ForceAdd forces the addition of all dirty files.
func ForceAdd(repo Repository) *exec.Cmd { func ForceAdd(repo Repository) *execabs.Cmd {
cmd := exec.Command( cmd := execabs.Command(
"git", gitBin,
"add", "add",
"--all", "--all",
"--force", "--force",
@ -20,9 +21,9 @@ func ForceAdd(repo Repository) *exec.Cmd {
} }
// Add updates the index to match the working tree. // Add updates the index to match the working tree.
func Add(repo Repository) *exec.Cmd { func Add(repo Repository) *execabs.Cmd {
cmd := exec.Command( cmd := execabs.Command(
"git", gitBin,
"add", "add",
) )
cmd.Dir = repo.WorkDir cmd.Dir = repo.WorkDir
@ -37,10 +38,10 @@ func Add(repo Repository) *exec.Cmd {
return cmd return cmd
} }
// TestCleanTree returns non-zero if diff between index and local repository // TestCleanTree returns non-zero if diff between index and local repository.
func TestCleanTree(repo Repository) *exec.Cmd { func TestCleanTree(repo Repository) *execabs.Cmd {
cmd := exec.Command( cmd := execabs.Command(
"git", gitBin,
"diff-index", "diff-index",
"--quiet", "--quiet",
"HEAD", "HEAD",
@ -52,14 +53,18 @@ func TestCleanTree(repo Repository) *exec.Cmd {
return cmd return cmd
} }
// EmptyCommit simply create an empty commit // EmptyCommit simply create an empty commit.
func EmptyCommit(repo Repository) *exec.Cmd { func EmptyCommit(repo Repository) *execabs.Cmd {
cmd := exec.Command( args := []string{
"git",
"commit", "commit",
"--allow-empty", "--allow-empty",
"-m", "-m",
repo.CommitMsg, repo.CommitMsg,
}
cmd := execabs.Command(
gitBin,
args...,
) )
cmd.Dir = repo.WorkDir cmd.Dir = repo.WorkDir
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
@ -72,12 +77,16 @@ func EmptyCommit(repo Repository) *exec.Cmd {
} }
// ForceCommit commits every change while skipping CI. // ForceCommit commits every change while skipping CI.
func ForceCommit(repo Repository) *exec.Cmd { func ForceCommit(repo Repository) *execabs.Cmd {
cmd := exec.Command( args := []string{
"git",
"commit", "commit",
"-m", "-m",
repo.CommitMsg, repo.CommitMsg,
}
cmd := execabs.Command(
gitBin,
args...,
) )
cmd.Dir = repo.WorkDir cmd.Dir = repo.WorkDir
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr

View File

@ -2,18 +2,23 @@ package git
import ( import (
"os" "os"
"os/exec"
"strconv" "strconv"
"golang.org/x/sys/execabs"
) )
// repoUserEmail sets the global git author email. // repoUserEmail sets the global git author email.
func ConfigAutocorrect(repo Repository) *exec.Cmd { func ConfigAutocorrect(repo Repository) *execabs.Cmd {
cmd := exec.Command( args := []string{
"git",
"config", "config",
"--local", "--local",
"help.autocorrect", "help.autocorrect",
repo.Autocorrect, repo.Autocorrect,
}
cmd := execabs.Command(
gitBin,
args...,
) )
cmd.Dir = repo.WorkDir cmd.Dir = repo.WorkDir
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
@ -22,13 +27,17 @@ func ConfigAutocorrect(repo Repository) *exec.Cmd {
} }
// repoUserEmail sets the global git author email. // repoUserEmail sets the global git author email.
func ConfigUserEmail(repo Repository) *exec.Cmd { func ConfigUserEmail(repo Repository) *execabs.Cmd {
cmd := exec.Command( args := []string{
"git",
"config", "config",
"--local", "--local",
"user.email", "user.email",
repo.Author.Email, repo.Author.Email,
}
cmd := execabs.Command(
gitBin,
args...,
) )
cmd.Dir = repo.WorkDir cmd.Dir = repo.WorkDir
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
@ -37,13 +46,17 @@ func ConfigUserEmail(repo Repository) *exec.Cmd {
} }
// repoUserName sets the global git author name. // repoUserName sets the global git author name.
func ConfigUserName(repo Repository) *exec.Cmd { func ConfigUserName(repo Repository) *execabs.Cmd {
cmd := exec.Command( args := []string{
"git",
"config", "config",
"--local", "--local",
"user.name", "user.name",
repo.Author.Name, repo.Author.Name,
}
cmd := execabs.Command(
gitBin,
args...,
) )
cmd.Dir = repo.WorkDir cmd.Dir = repo.WorkDir
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
@ -52,13 +65,17 @@ func ConfigUserName(repo Repository) *exec.Cmd {
} }
// repoSSLVerify disables globally the git ssl verification. // repoSSLVerify disables globally the git ssl verification.
func ConfigSSLVerify(repo Repository) *exec.Cmd { func ConfigSSLVerify(repo Repository) *execabs.Cmd {
cmd := exec.Command( args := []string{
"git",
"config", "config",
"--local", "--local",
"http.sslVerify", "http.sslVerify",
strconv.FormatBool(repo.SSLVerify), strconv.FormatBool(repo.SSLVerify),
}
cmd := execabs.Command(
gitBin,
args...,
) )
cmd.Dir = repo.WorkDir cmd.Dir = repo.WorkDir
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr

19
git/init.go Normal file
View File

@ -0,0 +1,19 @@
package git
import (
"os"
"golang.org/x/sys/execabs"
)
// RemoteRemove drops the defined remote from a git repo.
func Init(repo Repository) *execabs.Cmd {
cmd := execabs.Command(
gitBin,
"init",
)
cmd.Dir = repo.WorkDir
cmd.Stderr = os.Stderr
return cmd
}

View File

@ -3,16 +3,21 @@ package git
import ( import (
"fmt" "fmt"
"os" "os"
"os/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 RemoteRemove(repo Repository) *exec.Cmd { func RemoteRemove(repo Repository) *execabs.Cmd {
cmd := exec.Command( args := []string{
"git",
"remote", "remote",
"rm", "rm",
repo.RemoteName, repo.RemoteName,
}
cmd := execabs.Command(
gitBin,
args...,
) )
cmd.Dir = repo.WorkDir cmd.Dir = repo.WorkDir
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
@ -21,13 +26,17 @@ func RemoteRemove(repo Repository) *exec.Cmd {
} }
// RemoteAdd adds an additional remote to a git repo. // RemoteAdd adds an additional remote to a git repo.
func RemoteAdd(repo Repository) *exec.Cmd { func RemoteAdd(repo Repository) *execabs.Cmd {
cmd := exec.Command( args := []string{
"git",
"remote", "remote",
"add", "add",
repo.RemoteName, repo.RemoteName,
repo.RemoteURL, repo.RemoteURL,
}
cmd := execabs.Command(
gitBin,
args...,
) )
cmd.Dir = repo.WorkDir cmd.Dir = repo.WorkDir
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
@ -36,12 +45,16 @@ func RemoteAdd(repo Repository) *exec.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 RemotePush(repo Repository) *exec.Cmd { func RemotePush(repo Repository) *execabs.Cmd {
cmd := exec.Command( args := []string{
"git",
"push", "push",
repo.RemoteName, repo.RemoteName,
fmt.Sprintf("HEAD:%s", repo.Branch), fmt.Sprintf("HEAD:%s", repo.Branch),
}
cmd := execabs.Command(
gitBin,
args...,
) )
cmd.Dir = repo.WorkDir cmd.Dir = repo.WorkDir
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr

View File

@ -2,14 +2,15 @@ package git
import ( import (
"bytes" "bytes"
"fmt"
"os" "os"
"os/exec"
"github.com/sirupsen/logrus"
"golang.org/x/sys/execabs"
) )
func Status(repo Repository) *exec.Cmd { func Status(repo Repository) *execabs.Cmd {
cmd := exec.Command( cmd := execabs.Command(
"git", gitBin,
"status", "status",
"--porcelain", "--porcelain",
) )
@ -34,7 +35,8 @@ func IsDirty(repo Repository) bool {
} }
if res.Len() > 0 { if res.Len() > 0 {
fmt.Print(res.String()) logrus.Debug(res.String())
return true return true
} }

View File

@ -25,3 +25,5 @@ type Repository struct {
Author Author Author Author
} }
const gitBin = "/usr/bin/git"

View File

@ -3,23 +3,30 @@ package git
import ( import (
"fmt" "fmt"
"os" "os"
"os/exec"
"os/user" "os/user"
"path/filepath" "path/filepath"
"strings" "strings"
"golang.org/x/sys/execabs"
) )
const netrcFile = ` const (
netrcFile = `
machine %s machine %s
login %s login %s
password %s password %s
` `
configFile = `
const configFile = `
Host * Host *
StrictHostKeyChecking no StrictHostKeyChecking no
UserKnownHostsFile=/dev/null UserKnownHostsFile=/dev/null
` `
)
const (
strictFilePerm = 0o600
strictDirPerm = 0o600
)
// WriteKey writes the SSH private key. // WriteKey writes the SSH private key.
func WriteSSHKey(privateKey string) error { func WriteSSHKey(privateKey string) error {
@ -31,7 +38,7 @@ func WriteSSHKey(privateKey string) error {
sshpath := filepath.Join(home, ".ssh") sshpath := filepath.Join(home, ".ssh")
if err := os.MkdirAll(sshpath, 0o700); err != nil { if err := os.MkdirAll(sshpath, strictDirPerm); err != nil {
return err return err
} }
@ -40,7 +47,7 @@ func WriteSSHKey(privateKey string) error {
if err := os.WriteFile( if err := os.WriteFile(
confpath, confpath,
[]byte(configFile), []byte(configFile),
0o700, strictFilePerm,
); err != nil { ); err != nil {
return err return err
} }
@ -50,7 +57,7 @@ func WriteSSHKey(privateKey string) error {
if err := os.WriteFile( if err := os.WriteFile(
privpath, privpath,
[]byte(privateKey), []byte(privateKey),
0o600, strictFilePerm,
); err != nil { ); err != nil {
return err return err
} }
@ -81,15 +88,15 @@ func WriteNetrc(machine, login, password string) error {
return os.WriteFile( return os.WriteFile(
netpath, netpath,
[]byte(netrcContent), []byte(netrcContent),
0o600, strictFilePerm,
) )
} }
func trace(cmd *exec.Cmd) { func trace(cmd *execabs.Cmd) {
fmt.Fprintf(os.Stdout, "+ %s\n", strings.Join(cmd.Args, " ")) fmt.Fprintf(os.Stdout, "+ %s\n", strings.Join(cmd.Args, " "))
} }
func runCommand(cmd *exec.Cmd) error { func runCommand(cmd *execabs.Cmd) error {
if cmd.Stdout == nil { if cmd.Stdout == nil {
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
} }
@ -99,5 +106,6 @@ func runCommand(cmd *exec.Cmd) error {
} }
trace(cmd) trace(cmd)
return cmd.Run() return cmd.Run()
} }

4
go.mod
View File

@ -1,17 +1,17 @@
module github.com/thegeeklab/drone-git-action module github.com/thegeeklab/drone-git-action
go 1.19 go 1.20
require ( require (
github.com/joho/godotenv v1.4.0 github.com/joho/godotenv v1.4.0
github.com/sirupsen/logrus v1.9.0 github.com/sirupsen/logrus v1.9.0
github.com/thegeeklab/drone-plugin-lib/v2 v2.2.1 github.com/thegeeklab/drone-plugin-lib/v2 v2.2.1
github.com/urfave/cli/v2 v2.24.3 github.com/urfave/cli/v2 v2.24.3
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8
) )
require ( require (
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
) )

View File

@ -1,9 +1,9 @@
package plugin package plugin
import ( import (
"errors"
"fmt" "fmt"
"os" "os"
"os/exec"
"path/filepath" "path/filepath"
"github.com/thegeeklab/drone-git-action/git" "github.com/thegeeklab/drone-git-action/git"
@ -32,6 +32,16 @@ type Settings struct {
Repo git.Repository Repo git.Repository
} }
var (
ErrAuthSourceNotSet = errors.New("either SSH key or netrc password is required")
ErrPagesDirectoryNotExist = errors.New("pages directory must exist")
ErrPagesDirectoryNotValid = errors.New("pages directory not valid")
ErrPagesSourceNotSet = errors.New("pages source directory must be set")
ErrPagesActionNotExclusive = errors.New("pages action is mutual exclusive")
ErrActionUnknown = errors.New("action not found")
ErrGitCloneDestintionNotValid = errors.New("destination not valid")
)
// Validate handles the settings validation of the plugin. // Validate handles the settings validation of the plugin.
func (p *Plugin) Validate() error { func (p *Plugin) Validate() error {
var err error var err error
@ -43,6 +53,7 @@ func (p *Plugin) Validate() error {
if p.settings.Repo.WorkDir == "" { if p.settings.Repo.WorkDir == "" {
p.settings.Repo.WorkDir, err = os.Getwd() p.settings.Repo.WorkDir, err = os.Getwd()
} }
if err != nil { if err != nil {
return err return err
} }
@ -55,33 +66,33 @@ func (p *Plugin) Validate() error {
continue continue
case "push": case "push":
if p.settings.SSHKey == "" && p.settings.Netrc.Password == "" { if p.settings.SSHKey == "" && p.settings.Netrc.Password == "" {
return fmt.Errorf("either SSH key or netrc password is required") return ErrAuthSourceNotSet
} }
case "pages": case "pages":
p.settings.Pages.Directory = filepath.Join(p.settings.Repo.WorkDir, p.settings.Pages.Directory) p.settings.Pages.Directory = filepath.Join(p.settings.Repo.WorkDir, p.settings.Pages.Directory)
p.settings.Repo.WorkDir = filepath.Join(p.settings.Repo.WorkDir, ".tmp") p.settings.Repo.WorkDir = filepath.Join(p.settings.Repo.WorkDir, ".tmp")
if _, err := os.Stat(p.settings.Pages.Directory); os.IsNotExist(err) { if _, err := os.Stat(p.settings.Pages.Directory); os.IsNotExist(err) {
return fmt.Errorf("pages directory '%s' must exist", p.settings.Pages.Directory) return fmt.Errorf("%w: '%s' not found", ErrPagesDirectoryNotExist, p.settings.Pages.Directory)
} }
if info, _ := os.Stat(p.settings.Pages.Directory); !info.IsDir() { if info, _ := os.Stat(p.settings.Pages.Directory); !info.IsDir() {
return fmt.Errorf("pages directory '%s' is not a directory", p.settings.Pages.Directory) return fmt.Errorf("%w: '%s' not a directory", ErrPagesDirectoryNotValid, p.settings.Pages.Directory)
} }
if p.settings.SSHKey == "" && p.settings.Netrc.Password == "" { if p.settings.SSHKey == "" && p.settings.Netrc.Password == "" {
return fmt.Errorf("either SSH key or netrc password is required") return ErrAuthSourceNotSet
} }
if p.settings.Pages.Directory == "" { if p.settings.Pages.Directory == "" {
return fmt.Errorf("pages source directory needs to be set") return ErrPagesSourceNotSet
} }
if len(p.settings.Action.Value()) > 1 { if len(p.settings.Action.Value()) > 1 {
return fmt.Errorf("pages action can not be combined with other actions") return ErrPagesActionNotExclusive
} }
default: default:
return fmt.Errorf("unknown action %s", action) return fmt.Errorf("%w: %s", ErrActionUnknown, action)
} }
} }
@ -98,28 +109,33 @@ func (p *Plugin) Execute() error {
"GIT_COMMITTER_EMAIL", "GIT_COMMITTER_EMAIL",
"GIT_COMMITTER_DATE", "GIT_COMMITTER_DATE",
} }
for _, env := range gitEnv { for _, env := range gitEnv {
if err := os.Unsetenv(env); err != nil { if err := os.Unsetenv(env); err != nil {
return err return err
} }
} }
if err := os.Setenv("GIT_TERMINAL_PROMPT", "0"); err != nil { if err := os.Setenv("GIT_TERMINAL_PROMPT", "0"); err != nil {
return err return err
} }
if err := p.initRepo(); err != nil { if err := p.handleInit(); err != nil {
return err return err
} }
if err := git.ConfigAutocorrect(p.settings.Repo).Run(); err != nil { if err := git.ConfigAutocorrect(p.settings.Repo).Run(); err != nil {
return err return err
} }
if err := git.ConfigUserName(p.settings.Repo).Run(); err != nil { if err := git.ConfigUserName(p.settings.Repo).Run(); err != nil {
return err return err
} }
if err := git.ConfigUserEmail(p.settings.Repo).Run(); err != nil { if err := git.ConfigUserEmail(p.settings.Repo).Run(); err != nil {
return err return err
} }
if err := git.ConfigSSLVerify(p.settings.Repo).Run(); err != nil { if err := git.ConfigSSLVerify(p.settings.Repo).Run(); err != nil {
return err return err
} }
@ -158,31 +174,31 @@ func (p *Plugin) Execute() error {
return nil return nil
} }
// InitRepo initializes the repository. // handleInit initializes the repository.
func (p *Plugin) initRepo() error { func (p *Plugin) handleInit() error {
path := filepath.Join(p.settings.Repo.WorkDir, ".git") path := filepath.Join(p.settings.Repo.WorkDir, ".git")
if err := os.MkdirAll(p.settings.Repo.WorkDir, os.ModePerm); err != nil { if err := os.MkdirAll(p.settings.Repo.WorkDir, os.ModePerm); err != nil {
return err return err
} }
if _, err := os.Stat(path); !os.IsNotExist(err) { if _, err := os.Stat(path); !os.IsNotExist(err) {
p.settings.Repo.InitExists = true p.settings.Repo.InitExists = true
return nil return nil
} }
cmd := exec.Command( if err := execute(git.Init(p.settings.Repo)); err != nil {
"git", return err
"init", }
)
cmd.Dir = p.settings.Repo.WorkDir
return execute(cmd) return nil
} }
// HandleClone clones remote. // HandleClone clones remote.
func (p *Plugin) handleClone() error { func (p *Plugin) handleClone() error {
if p.settings.Repo.InitExists { if p.settings.Repo.InitExists {
return fmt.Errorf("destination '%s' already exists and is not an empty directory", p.settings.Repo.WorkDir) return fmt.Errorf("%w: %s exists and not empty", ErrGitCloneDestintionNotValid, p.settings.Repo.WorkDir)
} }
if p.settings.Repo.RemoteURL != "" { if p.settings.Repo.RemoteURL != "" {
@ -212,11 +228,11 @@ func (p *Plugin) handleCommit() error {
if err := execute(git.ForceCommit(p.settings.Repo)); err != nil { if err := execute(git.ForceCommit(p.settings.Repo)); err != nil {
return err return err
} }
} else { }
if p.settings.Repo.EmptyCommit {
if err := execute(git.EmptyCommit(p.settings.Repo)); err != nil { if p.settings.Repo.EmptyCommit {
return err if err := execute(git.EmptyCommit(p.settings.Repo)); err != nil {
} return err
} }
} }

View File

@ -12,7 +12,7 @@ type Plugin struct {
} }
// New initializes a plugin from the given Settings, Pipeline, and Network. // New initializes a plugin from the given Settings, Pipeline, and Network.
func New(settings Settings, pipeline drone.Pipeline, network drone.Network) drone.Plugin { func New(settings Settings, pipeline drone.Pipeline, network drone.Network) *Plugin {
return &Plugin{ return &Plugin{
settings: settings, settings: settings,
pipeline: pipeline, pipeline: pipeline,

View File

@ -1,17 +1,17 @@
package plugin package plugin
import ( import (
"fmt"
"os" "os"
"os/exec"
"strings" "strings"
"github.com/sirupsen/logrus"
"github.com/thegeeklab/drone-git-action/git" "github.com/thegeeklab/drone-git-action/git"
"golang.org/x/sys/execabs"
) )
// helper function to simply wrap os execte command. // helper function to simply wrap os execte command.
func execute(cmd *exec.Cmd) error { func execute(cmd *execabs.Cmd) error {
fmt.Println("+", strings.Join(cmd.Args, " ")) logrus.Debug("+", strings.Join(cmd.Args, " "))
cmd.Env = os.Environ() cmd.Env = os.Environ()
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
@ -20,7 +20,7 @@ func execute(cmd *exec.Cmd) error {
return cmd.Run() return cmd.Run()
} }
func rsyncDirectories(pages Pages, repo git.Repository) *exec.Cmd { func rsyncDirectories(pages Pages, repo git.Repository) *execabs.Cmd {
args := []string{ args := []string{
"-r", "-r",
"--exclude", "--exclude",
@ -48,7 +48,7 @@ func rsyncDirectories(pages Pages, repo git.Repository) *exec.Cmd {
repo.WorkDir, repo.WorkDir,
) )
cmd := exec.Command( cmd := execabs.Command(
"rsync", "rsync",
args..., args...,
) )