2023-12-22 23:59:23 +00:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
2024-05-06 18:30:18 +00:00
|
|
|
"fmt"
|
|
|
|
|
2023-12-22 23:59:23 +00:00
|
|
|
"github.com/thegeeklab/wp-git-clone/git"
|
2024-05-17 19:50:05 +00:00
|
|
|
plugin_base "github.com/thegeeklab/wp-plugin-go/v3/plugin"
|
|
|
|
plugin_types "github.com/thegeeklab/wp-plugin-go/v3/types"
|
2024-05-06 18:30:18 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2023-12-22 23:59:23 +00:00
|
|
|
)
|
|
|
|
|
2024-05-06 18:30:18 +00:00
|
|
|
//go:generate go run ../internal/doc/main.go -output=../docs/data/data-raw.yaml
|
|
|
|
|
2023-12-22 23:59:23 +00:00
|
|
|
// Plugin implements provide the plugin.
|
|
|
|
type Plugin struct {
|
2024-05-17 19:50:05 +00:00
|
|
|
*plugin_base.Plugin
|
2023-12-22 23:59:23 +00:00
|
|
|
Settings *Settings
|
|
|
|
}
|
|
|
|
|
|
|
|
type Netrc struct {
|
|
|
|
Machine string
|
|
|
|
Login string
|
|
|
|
Password string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Settings for the plugin.
|
|
|
|
type Settings struct {
|
|
|
|
Recursive bool
|
|
|
|
Tags bool
|
|
|
|
Lfs bool
|
|
|
|
Partial bool
|
2023-12-23 15:12:56 +00:00
|
|
|
Home string
|
2024-05-06 18:30:18 +00:00
|
|
|
SSHKey string
|
2023-12-22 23:59:23 +00:00
|
|
|
|
2023-12-23 20:46:36 +00:00
|
|
|
Netrc Netrc
|
|
|
|
Repo git.Repository
|
2023-12-22 23:59:23 +00:00
|
|
|
}
|
|
|
|
|
2024-05-17 19:50:05 +00:00
|
|
|
func New(e plugin_base.ExecuteFunc, build ...string) *Plugin {
|
2024-05-06 18:30:18 +00:00
|
|
|
p := &Plugin{
|
|
|
|
Settings: &Settings{},
|
|
|
|
}
|
|
|
|
|
2024-05-17 19:50:05 +00:00
|
|
|
options := plugin_base.Options{
|
2024-05-06 18:30:18 +00:00
|
|
|
Name: "wp-git-clone",
|
|
|
|
Description: "Clone git repository",
|
2024-05-17 19:50:05 +00:00
|
|
|
Flags: Flags(p.Settings, plugin_base.FlagsPluginCategory),
|
2024-05-06 18:30:18 +00:00
|
|
|
Execute: p.run,
|
|
|
|
HideWoodpeckerFlags: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(build) > 0 {
|
|
|
|
options.Version = build[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(build) > 1 {
|
|
|
|
options.VersionMetadata = fmt.Sprintf("date=%s", build[1])
|
|
|
|
}
|
2023-12-22 23:59:23 +00:00
|
|
|
|
2024-05-06 18:30:18 +00:00
|
|
|
if e != nil {
|
|
|
|
options.Execute = e
|
2024-04-22 18:46:46 +00:00
|
|
|
}
|
2023-12-22 23:59:23 +00:00
|
|
|
|
2024-05-17 19:50:05 +00:00
|
|
|
p.Plugin = plugin_base.New(options)
|
2023-12-22 23:59:23 +00:00
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
2024-05-06 18:30:18 +00:00
|
|
|
|
|
|
|
// Flags returns a slice of CLI flags for the plugin.
|
|
|
|
func Flags(settings *Settings, category string) []cli.Flag {
|
|
|
|
return []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "remote",
|
|
|
|
Usage: "git remote HTTP clone url",
|
|
|
|
EnvVars: []string{"PLUGIN_REMOTE", "CI_REPO_CLONE_URL"},
|
|
|
|
Destination: &settings.Repo.RemoteURL,
|
|
|
|
DefaultText: "$CI_REPO_CLONE_URL",
|
|
|
|
Category: category,
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "remote-ssh",
|
|
|
|
Usage: "git remote SSH clone url",
|
|
|
|
EnvVars: []string{"PLUGIN_REMOTE_SSH", "CI_REPO_CLONE_SSH_URL"},
|
|
|
|
Destination: &settings.Repo.RemoteSSH,
|
|
|
|
DefaultText: "$CI_REPO_CLONE_SSH_URL",
|
|
|
|
Category: category,
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "workdir",
|
|
|
|
Usage: "path to clone git repository",
|
|
|
|
EnvVars: []string{"PLUGIN_WORKDIR", "CI_WORKSPACE"},
|
|
|
|
Destination: &settings.Repo.WorkDir,
|
|
|
|
DefaultText: "$CI_WORKSPACE",
|
|
|
|
Category: category,
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "sha",
|
|
|
|
Usage: "git commit sha",
|
|
|
|
EnvVars: []string{"PLUGIN_COMMIT_SHA", "CI_COMMIT_SHA"},
|
|
|
|
Destination: &settings.Repo.CommitSha,
|
|
|
|
DefaultText: "$CI_COMMIT_SHA",
|
|
|
|
Category: category,
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "ref",
|
|
|
|
Usage: "git commit ref",
|
|
|
|
EnvVars: []string{"PLUGIN_COMMIT_REF", "CI_COMMIT_REF"},
|
|
|
|
Value: "refs/heads/main",
|
|
|
|
Destination: &settings.Repo.CommitRef,
|
|
|
|
Category: category,
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "netrc.machine",
|
|
|
|
Usage: "netrc machine",
|
|
|
|
EnvVars: []string{"CI_NETRC_MACHINE"},
|
|
|
|
Destination: &settings.Netrc.Machine,
|
|
|
|
Category: category,
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "netrc.username",
|
|
|
|
Usage: "netrc username",
|
|
|
|
EnvVars: []string{"CI_NETRC_USERNAME"},
|
|
|
|
Destination: &settings.Netrc.Password,
|
|
|
|
Category: category,
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "netrc.password",
|
|
|
|
Usage: "netrc password",
|
|
|
|
EnvVars: []string{"CI_NETRC_PASSWORD"},
|
|
|
|
Destination: &settings.Netrc.Password,
|
|
|
|
Category: category,
|
|
|
|
},
|
|
|
|
&cli.IntFlag{
|
|
|
|
Name: "depth",
|
|
|
|
Usage: "clone depth",
|
|
|
|
EnvVars: []string{"PLUGIN_DEPTH"},
|
|
|
|
Destination: &settings.Repo.Depth,
|
|
|
|
Category: category,
|
|
|
|
},
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "recursive",
|
|
|
|
Usage: "clone submodules",
|
|
|
|
EnvVars: []string{"PLUGIN_RECURSIVE"},
|
|
|
|
Value: true,
|
|
|
|
Destination: &settings.Recursive,
|
|
|
|
Category: category,
|
|
|
|
},
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "tags",
|
|
|
|
Usage: "fetch git tags during clone",
|
|
|
|
EnvVars: []string{"PLUGIN_TAGS"},
|
|
|
|
Value: true,
|
|
|
|
Destination: &settings.Tags,
|
|
|
|
Category: category,
|
|
|
|
},
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "submodule-update-remote",
|
|
|
|
Usage: "update remote submodules",
|
|
|
|
EnvVars: []string{"PLUGIN_SUBMODULES_UPDATE_REMOTE", "PLUGIN_SUBMODULE_UPDATE_REMOTE"},
|
|
|
|
Destination: &settings.Repo.SubmoduleRemote,
|
|
|
|
Category: category,
|
|
|
|
},
|
|
|
|
&cli.GenericFlag{
|
|
|
|
Name: "submodule-override",
|
|
|
|
Usage: "JSON map of submodule overrides",
|
|
|
|
EnvVars: []string{"PLUGIN_SUBMODULE_OVERRIDE"},
|
2024-05-17 19:50:05 +00:00
|
|
|
Value: &plugin_types.MapFlag{},
|
2024-05-06 18:30:18 +00:00
|
|
|
Category: category,
|
|
|
|
},
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "submodule-partial",
|
|
|
|
Usage: "update submodules via partial clone (`depth=1`)",
|
|
|
|
EnvVars: []string{"PLUGIN_SUBMODULES_PARTIAL", "PLUGIN_SUBMODULE_PARTIAL"},
|
|
|
|
Value: true,
|
|
|
|
Destination: &settings.Repo.SubmodulePartial,
|
|
|
|
Category: category,
|
|
|
|
},
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "lfs",
|
|
|
|
Usage: "whether to retrieve LFS content if available",
|
|
|
|
EnvVars: []string{"PLUGIN_LFS"},
|
|
|
|
Value: true,
|
|
|
|
Destination: &settings.Lfs,
|
|
|
|
Category: category,
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "branch",
|
|
|
|
Usage: "change branch name",
|
|
|
|
EnvVars: []string{"PLUGIN_BRANCH", "CI_COMMIT_BRANCH", "CI_REPO_DEFAULT_BRANCH"},
|
|
|
|
Destination: &settings.Repo.Branch,
|
|
|
|
Category: category,
|
|
|
|
},
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "partial",
|
|
|
|
Usage: "enable/disable partial clone",
|
|
|
|
EnvVars: []string{"PLUGIN_PARTIAL"},
|
|
|
|
Destination: &settings.Partial,
|
|
|
|
Category: category,
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "safe-directory",
|
|
|
|
Usage: "define/replace safe directories",
|
|
|
|
EnvVars: []string{"PLUGIN_SAFE_DIRECTORY", "CI_WORKSPACE"},
|
|
|
|
Destination: &settings.Repo.SafeDirectory,
|
|
|
|
DefaultText: "$CI_WORKSPACE",
|
|
|
|
Category: category,
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "ssh-key",
|
|
|
|
Usage: "private key for SSH clone",
|
|
|
|
EnvVars: []string{"PLUGIN_SSH_KEY"},
|
|
|
|
Destination: &settings.SSHKey,
|
|
|
|
Category: category,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|