0
0
mirror of https://github.com/thegeeklab/wp-git-clone.git synced 2024-11-24 23:10:40 +00:00

chore: cleanup docs and use cmd trace from plugin lib (#3)

This commit is contained in:
Robert Kaussow 2024-01-02 22:58:05 +01:00 committed by GitHub
parent 40636a12c5
commit 6b506e6a31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 58 additions and 48 deletions

View File

@ -13,6 +13,7 @@ import (
"github.com/thegeeklab/wp-git-clone/plugin" "github.com/thegeeklab/wp-git-clone/plugin"
"github.com/thegeeklab/wp-plugin-go/docs" "github.com/thegeeklab/wp-plugin-go/docs"
wp "github.com/thegeeklab/wp-plugin-go/plugin" wp "github.com/thegeeklab/wp-plugin-go/plugin"
wp_template "github.com/thegeeklab/wp-plugin-go/template"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
) )
@ -43,7 +44,7 @@ func main() {
func toYAML(app *cli.App) (string, error) { func toYAML(app *cli.App) (string, error) {
var w bytes.Buffer var w bytes.Buffer
yamlTmpl, err := template.New("docs").ParseFS(yamlTemplate, "templates/docs-data.yaml.tmpl") yamlTmpl, err := template.New("docs").Funcs(wp_template.LoadFuncMap()).ParseFS(yamlTemplate, "templates/docs-data.yaml.tmpl")
if err != nil { if err != nil {
fmt.Println(yamlTmpl) fmt.Println(yamlTmpl)
return "", err return "", err

View File

@ -1,9 +1,3 @@
// Copyright (c) 2020, the Drone Plugins project authors.
// Copyright (c) 2021, Robert Kaussow <mail@thegeeklab.de>
// Use of this source code is governed by an Apache 2.0 license that can be
// found in the LICENSE file.
package main package main
import ( import (
@ -19,16 +13,18 @@ func settingsFlags(settings *plugin.Settings, category string) []cli.Flag {
return []cli.Flag{ return []cli.Flag{
&cli.StringFlag{ &cli.StringFlag{
Name: "remote", Name: "remote",
Usage: "git remote url", Usage: "git remote HTTP clone url",
EnvVars: []string{"PLUGIN_REMOTE", "CI_REPO_CLONE_URL"}, EnvVars: []string{"PLUGIN_REMOTE", "CI_REPO_CLONE_URL"},
Destination: &settings.Repo.RemoteURL, Destination: &settings.Repo.RemoteURL,
DefaultText: "$CI_REPO_CLONE_URL",
Category: category, Category: category,
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "remote-ssh", Name: "remote-ssh",
Usage: "git clone ssh url", Usage: "git remote SSH clone url",
EnvVars: []string{"PLUGIN_REMOTE_SSH", "CI_REPO_CLONE_SSH_URL"}, EnvVars: []string{"PLUGIN_REMOTE_SSH", "CI_REPO_CLONE_SSH_URL"},
Destination: &settings.Repo.RemoteSSH, Destination: &settings.Repo.RemoteSSH,
DefaultText: "$CI_REPO_CLONE_SSH_URL",
Category: category, Category: category,
}, },
&cli.StringFlag{ &cli.StringFlag{
@ -36,6 +32,7 @@ func settingsFlags(settings *plugin.Settings, category string) []cli.Flag {
Usage: "path to clone git repository", Usage: "path to clone git repository",
EnvVars: []string{"PLUGIN_WORKDIR", "CI_WORKSPACE"}, EnvVars: []string{"PLUGIN_WORKDIR", "CI_WORKSPACE"},
Destination: &settings.WorkDir, Destination: &settings.WorkDir,
DefaultText: "$CI_WORKSPACE",
Category: category, Category: category,
}, },
&cli.StringFlag{ &cli.StringFlag{
@ -43,13 +40,14 @@ func settingsFlags(settings *plugin.Settings, category string) []cli.Flag {
Usage: "git commit sha", Usage: "git commit sha",
EnvVars: []string{"PLUGIN_COMMIT_SHA", "CI_COMMIT_SHA"}, EnvVars: []string{"PLUGIN_COMMIT_SHA", "CI_COMMIT_SHA"},
Destination: &settings.Repo.CommitSha, Destination: &settings.Repo.CommitSha,
DefaultText: "$CI_COMMIT_SHA",
Category: category, Category: category,
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "ref", Name: "ref",
Value: "refs/heads/main",
Usage: "git commit ref", Usage: "git commit ref",
EnvVars: []string{"PLUGIN_COMMIT_REF", "CI_COMMIT_REF"}, EnvVars: []string{"PLUGIN_COMMIT_REF", "CI_COMMIT_REF"},
Value: "refs/heads/main",
Destination: &settings.Repo.CommitRef, Destination: &settings.Repo.CommitRef,
Category: category, Category: category,
}, },
@ -99,7 +97,7 @@ func settingsFlags(settings *plugin.Settings, category string) []cli.Flag {
}, },
&cli.BoolFlag{ &cli.BoolFlag{
Name: "insecure-skip-ssl-verify", Name: "insecure-skip-ssl-verify",
Usage: "skip ssl verification of the remote machine", Usage: "skip SSL verification of the remote machine",
EnvVars: []string{"PLUGIN_INSECURE_SKIP_SSL_VERIFY"}, EnvVars: []string{"PLUGIN_INSECURE_SKIP_SSL_VERIFY"},
Destination: &settings.Repo.InsecureSkipSSLVerify, Destination: &settings.Repo.InsecureSkipSSLVerify,
Category: category, Category: category,
@ -113,14 +111,14 @@ func settingsFlags(settings *plugin.Settings, category string) []cli.Flag {
}, },
&cli.GenericFlag{ &cli.GenericFlag{
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: &types.MapFlag{},
Category: category, Category: category,
}, },
&cli.BoolFlag{ &cli.BoolFlag{
Name: "submodule-partial", Name: "submodule-partial",
Usage: "update submodules via partial clone", Usage: "update submodules via partial clone (`depth=1`)",
EnvVars: []string{"PLUGIN_SUBMODULES_PARTIAL", "PLUGIN_SUBMODULE_PARTIAL"}, EnvVars: []string{"PLUGIN_SUBMODULES_PARTIAL", "PLUGIN_SUBMODULE_PARTIAL"},
Value: true, Value: true,
Destination: &settings.Repo.SubmodulePartial, Destination: &settings.Repo.SubmodulePartial,
@ -138,8 +136,8 @@ func settingsFlags(settings *plugin.Settings, category string) []cli.Flag {
Name: "branch", Name: "branch",
Usage: "change branch name", Usage: "change branch name",
EnvVars: []string{"PLUGIN_BRANCH", "CI_COMMIT_BRANCH", "CI_REPO_DEFAULT_BRANCH"}, EnvVars: []string{"PLUGIN_BRANCH", "CI_COMMIT_BRANCH", "CI_REPO_DEFAULT_BRANCH"},
Destination: &settings.Repo.Branch,
Value: "main", Value: "main",
Destination: &settings.Repo.Branch,
Category: category, Category: category,
}, },
&cli.BoolFlag{ &cli.BoolFlag{
@ -154,18 +152,19 @@ func settingsFlags(settings *plugin.Settings, category string) []cli.Flag {
Usage: "define/replace safe directories", Usage: "define/replace safe directories",
EnvVars: []string{"PLUGIN_SAFE_DIRECTORY", "CI_WORKSPACE"}, EnvVars: []string{"PLUGIN_SAFE_DIRECTORY", "CI_WORKSPACE"},
Destination: &settings.Repo.SafeDirectory, Destination: &settings.Repo.SafeDirectory,
DefaultText: "$CI_WORKSPACE",
Category: category, Category: category,
}, },
&cli.BoolFlag{ &cli.BoolFlag{
Name: "use-ssh", Name: "use-ssh",
Usage: "using ssh for git clone", Usage: "using SSH for git clone",
EnvVars: []string{"PLUGIN_USE_SSH"}, EnvVars: []string{"PLUGIN_USE_SSH"},
Destination: &settings.UseSSH, Destination: &settings.UseSSH,
Category: category, Category: category,
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "ssh-key", Name: "ssh-key",
Usage: "ssh key for ssh clone", Usage: "private key for SSH clone",
EnvVars: []string{"PLUGIN_SSH_KEY"}, EnvVars: []string{"PLUGIN_SSH_KEY"},
Destination: &settings.SSHKey, Destination: &settings.SSHKey,
Category: category, Category: category,

View File

@ -1,9 +1,3 @@
// Copyright (c) 2020, the Drone Plugins project authors.
// Copyright (c) 2021, Robert Kaussow <mail@thegeeklab.de>
// Use of this source code is governed by an Apache 2.0 license that can be
// found in the LICENSE file.
package main package main
import ( import (

View File

@ -5,10 +5,16 @@ properties:
- name: {{ $v.Name }} - name: {{ $v.Name }}
{{- with $v.Description }} {{- with $v.Description }}
description: | description: |
{{ . }} {{ . | ToSentence }}
{{- end }}
{{- with $v.Type }}
type: {{ . }}
{{- end }} {{- end }}
{{- with $v.Default }} {{- with $v.Default }}
defaultvalue: {{ . }} defaultvalue: {{ . }}
{{- end }} {{- end }}
{{- with $v.Required }}
required: {{ . }}
{{- end }}
{{ end -}} {{ end -}}
{{ end -}} {{ end -}}

View File

@ -3,84 +3,107 @@ properties:
- name: branch - name: branch
description: | description: |
Change branch name. Change branch name.
type: string
defaultvalue: "main" defaultvalue: "main"
- name: commit_ref - name: commit_ref
description: | description: |
Git commit ref. Git commit ref.
type: string
defaultvalue: "refs/heads/main" defaultvalue: "refs/heads/main"
- name: commit_sha - name: commit_sha
description: | description: |
Git commit sha. Git commit sha.
type: string
defaultvalue: $CI_COMMIT_SHA
- name: depth - name: depth
description: | description: |
Clone depth. Clone depth.
type: integer
defaultvalue: 0 defaultvalue: 0
- name: insecure_skip_ssl_verify - name: insecure_skip_ssl_verify
description: | description: |
Skip SSL verification of the remote machine. Activating this option is insecure Skip SSL verification of the remote machine.
and should be avoided in most cases.
defaultvalue: true Activating this option is insecure and should be avoided in most cases.
type: bool type: bool
defaultvalue: false
- name: lfs - name: lfs
description: | description: |
Whether to retrieve LFS content if available. Whether to retrieve LFS content if available.
type: bool
defaultvalue: true defaultvalue: true
- name: partial - name: partial
description: | description: |
Enable/disable partial clone. Enable/disable partial clone.
type: bool
defaultvalue: false defaultvalue: false
- name: recursive - name: recursive
description: | description: |
Clone submodules. Clone submodules.
type: bool
defaultvalue: true defaultvalue: true
- name: remote - name: remote
description: | description: |
Git remote HTTP clone url. Git remote HTTP clone url.
type: string
defaultvalue: $CI_REPO_CLONE_URL
- name: remote_ssh - name: remote_ssh
description: | description: |
Git remote SSH clone url. Git remote SSH clone url.
type: string
defaultvalue: $CI_REPO_CLONE_SSH_URL
- name: safe_directory - name: safe_directory
description: | description: |
Define/replace safe directories. Define/replace safe directories.
type: string
defaultvalue: $CI_WORKSPACE
- name: ssh_key - name: ssh_key
description: | description: |
SSH key for ssh clone. Private key for SSH clone.
type: string
- name: submodule_override - name: submodule_override
description: | description: |
JSON map of submodule overrides. JSON map of submodule overrides.
type: generic
- name: submodules_partial - name: submodules_partial
description: | description: |
Update submodules via partial clone (`depth=1`). Update submodules via partial clone (`depth=1`).
type: bool
defaultvalue: true defaultvalue: true
- name: submodules_update_remote - name: submodules_update_remote
description: | description: |
Update remote submodules. Update remote submodules.
type: bool
defaultvalue: false defaultvalue: false
- name: tags - name: tags
description: | description: |
Fetch git tags during clone. Fetch git tags during clone.
type: bool
defaultvalue: true defaultvalue: true
- name: use_ssh - name: use_ssh
description: | description: |
Using ssh for git clone. Using SSH for git clone.
type: bool
defaultvalue: false defaultvalue: false
- name: workdir - name: workdir
description: | description: |
Path to clone git repository. Path to clone git repository.
type: string
defaultvalue: $CI_WORKSPACE

2
go.mod
View File

@ -5,7 +5,7 @@ go 1.21
require ( require (
github.com/cenkalti/backoff/v4 v4.2.1 github.com/cenkalti/backoff/v4 v4.2.1
github.com/rs/zerolog v1.31.0 github.com/rs/zerolog v1.31.0
github.com/thegeeklab/wp-plugin-go v1.4.0 github.com/thegeeklab/wp-plugin-go v1.5.0
github.com/urfave/cli/v2 v2.27.1 github.com/urfave/cli/v2 v2.27.1
golang.org/x/sys v0.15.0 golang.org/x/sys v0.15.0
) )

4
go.sum
View File

@ -50,8 +50,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.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/thegeeklab/wp-plugin-go v1.4.0 h1:gYctYST3wfNrx/G+JgevWqhCdSPDukL63e49+ZMVBzI= github.com/thegeeklab/wp-plugin-go v1.5.0 h1:fA/kQwQrntjxdxePKKGecMRnPbj1xwpy6D2JrrPD2qA=
github.com/thegeeklab/wp-plugin-go v1.4.0/go.mod h1:ULBD5SNC7bkIB/UbLMQpcPWj7iZObCTWg8bqXf1zqsA= github.com/thegeeklab/wp-plugin-go v1.5.0/go.mod h1:ULBD5SNC7bkIB/UbLMQpcPWj7iZObCTWg8bqXf1zqsA=
github.com/urfave/cli/v2 v2.27.1 h1:8xSQ6szndafKVRmfyeUMxkNUJQMjL1F2zmsZ+qHpfho= github.com/urfave/cli/v2 v2.27.1 h1:8xSQ6szndafKVRmfyeUMxkNUJQMjL1F2zmsZ+qHpfho=
github.com/urfave/cli/v2 v2.27.1/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ= github.com/urfave/cli/v2 v2.27.1/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU= github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=

View File

@ -1,8 +1,3 @@
// Copyright (c) 2023, Robert Kaussow <mail@thegeeklab.de>
// Use of this source code is governed by an Apache 2.0 license that can be
// found in the LICENSE file.
package plugin package plugin
import ( import (
@ -18,6 +13,7 @@ import (
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/thegeeklab/wp-git-clone/git" "github.com/thegeeklab/wp-git-clone/git"
"github.com/thegeeklab/wp-plugin-go/trace"
"github.com/thegeeklab/wp-plugin-go/types" "github.com/thegeeklab/wp-plugin-go/types"
"golang.org/x/sys/execabs" "golang.org/x/sys/execabs"
) )
@ -179,7 +175,7 @@ func (p *Plugin) execCmd(cmd *execabs.Cmd, buf *bytes.Buffer) error {
cmd.Stderr = io.MultiWriter(os.Stderr, buf) cmd.Stderr = io.MultiWriter(os.Stderr, buf)
cmd.Dir = p.Settings.WorkDir cmd.Dir = p.Settings.WorkDir
trace(cmd) trace.Cmd(cmd)
return cmd.Run() return cmd.Run()
} }

View File

@ -1,8 +1,3 @@
// Copyright (c) 2023, Robert Kaussow <mail@thegeeklab.de>
// Use of this source code is governed by an Apache 2.0 license that can be
// found in the LICENSE file.
package plugin package plugin
import ( import (

View File

@ -1,13 +1,13 @@
package plugin package plugin
import ( import (
"fmt"
"os" "os"
"strings" "strings"
"time" "time"
"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/trace"
"golang.org/x/sys/execabs" "golang.org/x/sys/execabs"
) )
@ -25,10 +25,6 @@ func newBackoff(maxRetries uint64) backoff.BackOff {
return backoff.WithMaxRetries(b, maxRetries) return backoff.WithMaxRetries(b, maxRetries)
} }
func trace(cmd *execabs.Cmd) {
fmt.Fprintf(os.Stdout, "+ %s\n", strings.Join(cmd.Args, " "))
}
func retryCmd(cmd *execabs.Cmd) error { func retryCmd(cmd *execabs.Cmd) error {
backoffOps := func() error { backoffOps := func() error {
// copy the original command // copy the original command
@ -39,7 +35,7 @@ func retryCmd(cmd *execabs.Cmd) error {
retry.Stdout = os.Stdout retry.Stdout = os.Stdout
retry.Stderr = os.Stderr retry.Stderr = os.Stderr
trace(cmd) trace.Cmd(cmd)
return cmd.Run() return cmd.Run()
} }