Migrate to github.com/urfave/cli/v3 (#29)

Reviewed-on: https://codeberg.org/woodpecker-plugins/go-plugin/pulls/29
Reviewed-by: Patrick Schratz <pat-s@noreply.codeberg.org>
Co-authored-by: 6543 <6543@obermui.de>
Co-committed-by: 6543 <6543@obermui.de>
This commit is contained in:
6543 2024-07-22 23:32:14 +00:00 committed by Patrick Schratz
parent 2e397e1b02
commit 67b6cdf4a6
16 changed files with 268 additions and 207 deletions

View File

@ -25,7 +25,7 @@ import (
"codeberg.org/woodpecker-plugins/go-plugin"
"github.com/rs/zerolog/log"
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v3"
)
type Settings struct {
@ -44,7 +44,7 @@ func (p *Plugin) Flags() []cli.Flag {
&cli.StringFlag{
Name: "sample.flag",
Usage: "sample flag",
EnvVars: []string{"PLUGIN_SAMPLE_FLAG"},
Sources: cli.EnvVars("PLUGIN_SAMPLE_FLAG"),
Destination: &p.Settings.SampleFlag,
},
}

180
commit.go
View File

@ -15,7 +15,7 @@
package plugin
import (
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v3"
)
type (
@ -44,69 +44,106 @@ type (
func commitFlags() []cli.Flag {
return []cli.Flag{
&cli.StringFlag{
Name: "commit.sha",
Usage: "commit SHA",
EnvVars: []string{"CI_COMMIT_SHA", "DRONE_COMMIT", "DRONE_COMMIT_SHA"},
Name: "commit.sha",
Usage: "commit SHA",
Sources: cli.EnvVars(
"CI_COMMIT_SHA",
"DRONE_COMMIT",
"DRONE_COMMIT_SHA",
),
},
&cli.StringFlag{
Name: "commit.ref",
Usage: "commit ref",
EnvVars: []string{"CI_COMMIT_REF", "DRONE_COMMIT_REF"},
Name: "commit.ref",
Usage: "commit ref",
Sources: cli.EnvVars(
"CI_COMMIT_REF",
"DRONE_COMMIT_REF",
),
},
&cli.StringFlag{
Name: "commit.refspec",
Usage: "commit refspec",
EnvVars: []string{"CI_COMMIT_REFSPEC"},
Name: "commit.refspec",
Usage: "commit refspec",
Sources: cli.EnvVars(
"CI_COMMIT_REFSPEC",
),
},
&cli.StringFlag{
Name: "commit.pull-request",
Usage: "commit pull request",
EnvVars: []string{"CI_COMMIT_PULL_REQUEST", "DRONE_PULL_REQUEST"},
Name: "commit.pull-request",
Usage: "commit pull request",
Sources: cli.EnvVars(
"CI_COMMIT_PULL_REQUEST",
"DRONE_PULL_REQUEST",
),
},
&cli.StringFlag{
Name: "commit.source-branch",
Usage: "commit source branch",
EnvVars: []string{"CI_COMMIT_SOURCE_BRANCH", "DRONE_SOURCE_BRANCH"},
Name: "commit.source-branch",
Usage: "commit source branch",
Sources: cli.EnvVars(
"CI_COMMIT_SOURCE_BRANCH",
"DRONE_SOURCE_BRANCH",
),
},
&cli.StringFlag{
Name: "commit.target-branch",
Usage: "commit target branch",
EnvVars: []string{"CI_COMMIT_TARGET_BRANCH", "DRONE_TARGET_BRANCH"},
Name: "commit.target-branch",
Usage: "commit target branch",
Sources: cli.EnvVars(
"CI_COMMIT_TARGET_BRANCH",
"DRONE_TARGET_BRANCH",
),
},
&cli.StringFlag{
Name: "commit.branch",
Usage: "commit branch",
EnvVars: []string{"CI_COMMIT_BRANCH", "DRONE_BRANCH"},
Name: "commit.branch",
Usage: "commit branch",
Sources: cli.EnvVars(
"CI_COMMIT_BRANCH",
"DRONE_BRANCH",
),
},
&cli.StringFlag{
Name: "commit.tag",
Usage: "commit tag",
EnvVars: []string{"CI_COMMIT_TAG", "DRONE_TAG"},
Name: "commit.tag",
Usage: "commit tag",
Sources: cli.EnvVars(
"CI_COMMIT_TAG",
"DRONE_TAG",
),
},
&cli.StringFlag{
Name: "commit.message",
Usage: "commit message",
EnvVars: []string{"CI_COMMIT_MESSAGE", "DRONE_COMMIT_MESSAGE"},
Name: "commit.message",
Usage: "commit message",
Sources: cli.EnvVars(
"CI_COMMIT_MESSAGE",
"DRONE_COMMIT_MESSAGE",
),
},
&cli.StringFlag{
Name: "commit.author.name",
Usage: "commit author name",
EnvVars: []string{"CI_COMMIT_AUTHOR", "DRONE_COMMIT_AUTHOR", "DRONE_COMMIT_AUTHOR_NAME"},
Name: "commit.author.name",
Usage: "commit author name",
Sources: cli.EnvVars(
"CI_COMMIT_AUTHOR",
"DRONE_COMMIT_AUTHOR",
"DRONE_COMMIT_AUTHOR_NAME",
),
},
&cli.StringFlag{
Name: "commit.author.email",
Usage: "commit author email",
EnvVars: []string{"CI_COMMIT_AUTHOR_EMAIL", "DRONE_COMMIT_AUTHOR_EMAIL"},
Name: "commit.author.email",
Usage: "commit author email",
Sources: cli.EnvVars(
"CI_COMMIT_AUTHOR_EMAIL",
"DRONE_COMMIT_AUTHOR_EMAIL",
),
},
&cli.StringFlag{
Name: "commit.author.avatar",
Usage: "commit author avatar",
EnvVars: []string{"CI_COMMIT_AUTHOR_AVATAR", "DRONE_COMMIT_AUTHOR_AVATAR"},
Name: "commit.author.avatar",
Usage: "commit author avatar",
Sources: cli.EnvVars(
"CI_COMMIT_AUTHOR_AVATAR",
"DRONE_COMMIT_AUTHOR_AVATAR",
),
},
}
}
func commitFromContext(c *cli.Context) Commit {
func commitFromContext(c *cli.Command) Commit {
return Commit{
Sha: c.String("commit.sha"),
Ref: c.String("commit.ref"),
@ -128,49 +165,66 @@ func commitFromContext(c *cli.Context) Commit {
func previousCommitFlags() []cli.Flag {
return []cli.Flag{
&cli.StringFlag{
Name: "prev.commit.sha",
Usage: "previous commit SHA",
EnvVars: []string{"CI_PREV_COMMIT_SHA", "DRONE_COMMIT_BEFORE"},
Name: "prev.commit.sha",
Usage: "previous commit SHA",
Sources: cli.EnvVars(
"CI_PREV_COMMIT_SHA",
"DRONE_COMMIT_BEFORE",
),
},
&cli.StringFlag{
Name: "prev.commit.ref",
Usage: "previous commit ref",
EnvVars: []string{"CI_PREV_COMMIT_REF"},
Name: "prev.commit.ref",
Usage: "previous commit ref",
Sources: cli.EnvVars(
"CI_PREV_COMMIT_REF",
),
},
&cli.StringFlag{
Name: "prev.commit.refspec",
Usage: "previous commit refspec",
EnvVars: []string{"CI_PREV_COMMIT_REFSPEC"},
Name: "prev.commit.refspec",
Usage: "previous commit refspec",
Sources: cli.EnvVars(
"CI_PREV_COMMIT_REFSPEC",
),
},
&cli.StringFlag{
Name: "prev.commit.branch",
Usage: "previous commit branch",
EnvVars: []string{"CI_PREV_COMMIT_BRANCH"},
Name: "prev.commit.branch",
Usage: "previous commit branch",
Sources: cli.EnvVars(
"CI_PREV_COMMIT_BRANCH",
),
},
&cli.StringFlag{
Name: "prev.commit.message",
Usage: "previous commit message",
EnvVars: []string{"CI_PREV_COMMIT_MESSAGE"},
Name: "prev.commit.message",
Usage: "previous commit message",
Sources: cli.EnvVars(
"CI_PREV_COMMIT_MESSAGE",
),
},
&cli.StringFlag{
Name: "prev.commit.author.name",
Usage: "previous commit author name",
EnvVars: []string{"CI_PREV_COMMIT_AUTHOR"},
Name: "prev.commit.author.name",
Usage: "previous commit author name",
Sources: cli.EnvVars(
"CI_PREV_COMMIT_AUTHOR",
),
},
&cli.StringFlag{
Name: "prev.commit.author.email",
Usage: "previous commit author email",
EnvVars: []string{"CI_PREV_COMMIT_AUTHOR_EMAIL"},
Name: "prev.commit.author.email",
Usage: "previous commit author email",
Sources: cli.EnvVars(
"CI_PREV_COMMIT_AUTHOR_EMAIL",
),
},
&cli.StringFlag{
Name: "prev.commit.author.avatar",
Usage: "previous commit author avatar",
EnvVars: []string{"CI_PREV_COMMIT_AUTHOR_AVATAR"},
Name: "prev.commit.author.avatar",
Usage: "previous commit author avatar",
Sources: cli.EnvVars(
"CI_PREV_COMMIT_AUTHOR_AVATAR",
),
},
}
}
func previousCommitFromContext(c *cli.Context) Commit {
func previousCommitFromContext(c *cli.Command) Commit {
return Commit{
Sha: c.String("prev.commit.sha"),
Ref: c.String("prev.commit.ref"),

View File

@ -15,7 +15,7 @@
package plugin
import (
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v3"
)
// Flags has the cli.Flags for the Woodpecker plugin.

View File

@ -15,7 +15,7 @@
package plugin
import (
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v3"
)
const (
@ -40,26 +40,26 @@ func forgeFlags() []cli.Flag {
&cli.StringFlag{
Name: "forge.type",
Usage: "forge type (gitea, forgejo, github, gitlab, bitbucket)",
EnvVars: []string{
Sources: cli.EnvVars(
"CI_FORGE_TYPE",
},
),
},
&cli.StringFlag{
Name: "forge.url",
Aliases: []string{"forge.link"},
Usage: "forge url",
EnvVars: []string{
Sources: cli.EnvVars(
"CI_FORGE_URL",
"CI_FORGE_LINK",
},
),
},
}
}
func forgeFromContext(ctx *cli.Context) Forge {
func forgeFromContext(c *cli.Command) Forge {
return Forge{
Type: ctx.String("forge.type"),
Link: ctx.String("forge.url"),
URL: ctx.String("forge.url"),
Type: c.String("forge.type"),
Link: c.String("forge.url"),
URL: c.String("forge.url"),
}
}

5
go.mod
View File

@ -6,18 +6,15 @@ require (
github.com/joho/godotenv v1.5.1
github.com/rs/zerolog v1.33.0
github.com/stretchr/testify v1.9.0
github.com/urfave/cli/v2 v2.27.2
github.com/urfave/cli/v3 v3.0.0-alpha9.0.20240717192922-127cf54fac9f
golang.org/x/net v0.27.0
)
require (
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 // indirect
golang.org/x/sys v0.22.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

10
go.sum
View File

@ -1,6 +1,4 @@
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
@ -17,14 +15,10 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
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/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=
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913/go.mod h1:4aEEwZQutDLsQv2Deui4iYQ6DWTxR14g6m8Wv88+Xqk=
github.com/urfave/cli/v3 v3.0.0-alpha9.0.20240717192922-127cf54fac9f h1:yCJ90PBe7+45EQSF3qJXyAGW5rkE65lE8huv5pM0HY8=
github.com/urfave/cli/v3 v3.0.0-alpha9.0.20240717192922-127cf54fac9f/go.mod h1:Z1ItyMma7t6I7zHG9OpbExhHQOSkFf/96n+mAZ9MtVI=
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

42
http.go
View File

@ -23,37 +23,43 @@ import (
"time"
"github.com/rs/zerolog/log"
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v3"
"golang.org/x/net/proxy"
)
func httpClientFlags() []cli.Flag {
return []cli.Flag{
&cli.BoolFlag{
Name: "transport.skip-verify",
Usage: "skip ssl verify",
EnvVars: []string{"PLUGIN_SKIP_VERIFY"},
Name: "transport.skip-verify",
Usage: "skip ssl verify",
Sources: cli.EnvVars(
"PLUGIN_SKIP_VERIFY",
),
},
&cli.StringFlag{
Name: "transport.socks-proxy",
Usage: "socks proxy address",
EnvVars: []string{"SOCKS_PROXY"},
Hidden: true,
Name: "transport.socks-proxy",
Usage: "socks proxy address",
Sources: cli.EnvVars(
"SOCKS_PROXY",
),
Hidden: true,
},
&cli.BoolFlag{
Name: "transport.socks-proxy-off",
Usage: "socks proxy ignored",
EnvVars: []string{"SOCKS_PROXY_OFF"},
Hidden: true,
Name: "transport.socks-proxy-off",
Usage: "socks proxy ignored",
Sources: cli.EnvVars(
"SOCKS_PROXY_OFF",
),
Hidden: true,
},
}
}
func HTTPClientFromContext(ctx *cli.Context) *http.Client {
func HTTPClientFromContext(c *cli.Command) *http.Client {
var (
skip = ctx.Bool("transport.skip-verify")
socks = ctx.String("transport.socks-proxy")
socksoff = ctx.Bool("transport.socks-proxy-off")
skip = c.Bool("transport.skip-verify")
socks = c.String("transport.socks-proxy")
socksOff = c.Bool("transport.socks-proxy-off")
)
certs, err := x509.SystemCertPool()
@ -80,7 +86,7 @@ func HTTPClientFromContext(ctx *cli.Context) *http.Client {
DualStack: true,
}
if len(socks) != 0 && !socksoff {
if len(socks) != 0 && !socksOff {
proxyDialer, err := proxy.SOCKS5("tcp", socks, nil, dialer)
if err != nil {
log.Error().Err(err).Msg("failed to create socks proxy")
@ -88,7 +94,7 @@ func HTTPClientFromContext(ctx *cli.Context) *http.Client {
if contextDialer, ok := proxyDialer.(proxy.ContextDialer); ok {
transport.DialContext = contextDialer.DialContext
} else {
transport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
transport.DialContext = func(_ context.Context, network, addr string) (net.Conn, error) {
return proxyDialer.Dial(network, addr)
}
}

View File

@ -19,22 +19,24 @@ import (
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v3"
)
func loggingFlags() []cli.Flag {
return []cli.Flag{
&cli.StringFlag{
Name: "log-level",
Usage: "log level",
EnvVars: []string{"PLUGIN_LOG_LEVEL"},
Value: "info",
Name: "log-level",
Usage: "log level",
Sources: cli.EnvVars(
"PLUGIN_LOG_LEVEL",
),
Value: "info",
},
}
}
// SetupConsoleLogger sets up the console logger.
func SetupConsoleLogger(c *cli.Context) error {
func SetupConsoleLogger(c *cli.Command) error {
level := c.String("log-level")
lvl, err := zerolog.ParseLevel(level)
if err != nil {

View File

@ -15,7 +15,7 @@
package plugin
import (
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v3"
)
// Metadata defines runtime metadata.
@ -36,18 +36,18 @@ type Metadata struct {
}
// MetadataFromContext creates a Metadata from the cli.Context.
func MetadataFromContext(ctx *cli.Context) Metadata {
commit := commitFromContext(ctx)
previousCommit := previousCommitFromContext(ctx)
func MetadataFromContext(c *cli.Command) Metadata {
commit := commitFromContext(c)
previousCommit := previousCommitFromContext(c)
return Metadata{
Repository: repositoryFromContext(ctx),
Pipeline: pipelineFromContext(ctx),
Repository: repositoryFromContext(c),
Pipeline: pipelineFromContext(c),
Commit: commit,
PreviousCommit: previousCommit,
Step: stepFromContext(ctx),
System: systemFromContext(ctx),
Forge: forgeFromContext(ctx),
Step: stepFromContext(c),
System: systemFromContext(c),
Forge: forgeFromContext(c),
Curr: commit,
Prev: previousCommit,

View File

@ -79,7 +79,7 @@ func TestMetadata(t *testing.T) {
return nil
},
})
err := plugin.app.Run([]string{"test"})
err := plugin.app.Run(plugin.ctx, []string{"test"})
require.NoError(t, err)
// Repository

View File

@ -17,7 +17,7 @@ package plugin
import (
"time"
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v3"
)
const (
@ -47,94 +47,94 @@ type Pipeline struct {
func pipelineFlags() []cli.Flag {
return []cli.Flag{
&cli.Int64Flag{
&cli.IntFlag{
Name: "pipeline.number",
Usage: "pipeline number",
EnvVars: []string{
Sources: cli.EnvVars(
"CI_PIPELINE_NUMBER",
"DRONE_BUILD_NUMBER",
},
),
},
&cli.StringFlag{
Name: "pipeline.status",
Usage: "pipeline status",
EnvVars: []string{
Sources: cli.EnvVars(
"CI_PIPELINE_STATUS",
"DRONE_BUILD_STATUS",
},
),
},
&cli.StringFlag{
Name: "pipeline.event",
Usage: "pipeline event",
EnvVars: []string{
Sources: cli.EnvVars(
"CI_PIPELINE_EVENT",
"DRONE_BUILD_EVENT",
},
),
},
&cli.StringFlag{
Name: "pipeline.url",
Aliases: []string{"pipeline.link"},
Usage: "pipeline url",
EnvVars: []string{
Sources: cli.EnvVars(
"CI_PIPELINE_URL",
"CI_PIPELINE_LINK",
"DRONE_BUILD_LINK",
},
),
},
&cli.StringFlag{
Name: "pipeline.deploy-target",
Usage: "pipeline deployment target",
EnvVars: []string{
Sources: cli.EnvVars(
"CI_PIPELINE_DEPLOY_TARGET",
"DRONE_DEPLOY_TO",
},
),
},
&cli.Int64Flag{
&cli.IntFlag{
Name: "pipeline.created",
Usage: "pipeline creation time",
EnvVars: []string{
Sources: cli.EnvVars(
"CI_PIPELINE_CREATED",
"DRONE_BUILD_CREATED",
},
),
},
&cli.Int64Flag{
&cli.IntFlag{
Name: "pipeline.started",
Usage: "pipeline start time",
EnvVars: []string{
Sources: cli.EnvVars(
"CI_PIPELINE_STARTED",
"DRONE_BUILD_STARTED",
},
),
},
&cli.Int64Flag{
&cli.IntFlag{
Name: "pipeline.finished",
Usage: "pipeline finish time",
EnvVars: []string{
Sources: cli.EnvVars(
"CI_PIPELINE_FINISHED",
"DRONE_BUILD_FINISHED",
},
),
},
&cli.Int64Flag{
&cli.IntFlag{
Name: "pipeline.parent",
Usage: "pipeline parent",
EnvVars: []string{
Sources: cli.EnvVars(
"CI_PIPELINE_PARENT",
"DRONE_BUILD_PARENT",
},
),
},
}
}
func pipelineFromContext(c *cli.Context) Pipeline {
func pipelineFromContext(c *cli.Command) Pipeline {
return Pipeline{
Number: c.Int64("pipeline.number"),
Number: c.Int("pipeline.number"),
Status: c.String("pipeline.status"),
Event: c.String("pipeline.event"),
URL: c.String("pipeline.url"),
Link: c.String("pipeline.url"),
DeployTarget: c.String("pipeline.deploy-target"),
Created: time.Unix(c.Int64("pipeline.created"), 0),
Started: time.Unix(c.Int64("pipeline.started"), 0),
Finished: time.Unix(c.Int64("pipeline.finished"), 0),
Parent: c.Int64("pipeline.parent"),
Created: time.Unix(c.Int("pipeline.created"), 0),
Started: time.Unix(c.Int("pipeline.started"), 0),
Finished: time.Unix(c.Int("pipeline.finished"), 0),
Parent: c.Int("pipeline.parent"),
}
}

View File

@ -21,7 +21,7 @@ import (
"github.com/joho/godotenv"
"github.com/rs/zerolog/log"
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v3"
)
// Options defines the options for the plugin.
@ -36,13 +36,16 @@ type Options struct {
Flags []cli.Flag
// Execute function of the plugin.
Execute ExecuteFunc
// Context the plugin will use while executing.
Context context.Context
}
// Plugin defines the plugin instance.
type Plugin struct {
app *cli.App
app *cli.Command
execute ExecuteFunc
client *http.Client
ctx context.Context
// Metadata of the current pipeline.
Metadata Metadata
}
@ -56,7 +59,7 @@ func New(opt Options) *Plugin {
_ = godotenv.Overload("/run/woodpecker/env")
}
app := &cli.App{
app := &cli.Command{
Name: opt.Name,
Usage: "Run the Woodpecker CI plugin",
Description: opt.Description,
@ -67,25 +70,30 @@ func New(opt Options) *Plugin {
plugin := &Plugin{
app: app,
execute: opt.Execute,
ctx: opt.Context,
}
plugin.app.Action = plugin.action
if plugin.ctx == nil {
plugin.ctx = context.Background()
}
return plugin
}
func (p *Plugin) action(ctx *cli.Context) error {
if err := SetupConsoleLogger(ctx); err != nil {
func (p *Plugin) action(ctx context.Context, c *cli.Command) error {
if err := SetupConsoleLogger(c); err != nil {
return err
}
p.Metadata = MetadataFromContext(ctx)
p.client = HTTPClientFromContext(ctx)
p.Metadata = MetadataFromContext(c)
p.client = HTTPClientFromContext(c)
if p.execute == nil {
panic("plugin execute function is not set")
}
return p.execute(ctx.Context)
return p.execute(ctx)
}
// HTTPClient returns the http.Client instance.
@ -95,7 +103,7 @@ func (p *Plugin) HTTPClient() *http.Client {
// Run the plugin.
func (p *Plugin) Run() {
if err := p.app.Run(os.Args); err != nil {
if err := p.app.Run(p.ctx, os.Args); err != nil {
log.Error().Err(err).Msg("execution failed")
os.Exit(1)
}

View File

@ -30,7 +30,7 @@ func TestPlugin(t *testing.T) {
return nil
},
})
err := p.app.Run([]string{"test"})
err := p.app.Run(p.ctx, []string{"test"})
assert.NoError(t, err)
assert.True(t, executed)
}

32
repo.go
View File

@ -15,7 +15,7 @@
package plugin
import (
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v3"
)
// Repository defines runtime metadata for a repository.
@ -38,64 +38,64 @@ func repositoryFlags() []cli.Flag {
&cli.StringFlag{
Name: "repo.remote-id",
Usage: "repo remote id",
EnvVars: []string{
Sources: cli.EnvVars(
"CI_REPO_REMOTE_ID",
},
),
},
&cli.StringFlag{
Name: "repo.name",
Usage: "repo name",
EnvVars: []string{
Sources: cli.EnvVars(
"CI_REPO_NAME",
"DRONE_REPO_NAME",
},
),
},
&cli.StringFlag{
Name: "repo.owner",
Usage: "repo owner",
EnvVars: []string{
Sources: cli.EnvVars(
"CI_REPO_OWNER",
"DRONE_REPO_OWNER",
},
),
},
&cli.StringFlag{
Name: "repo.url",
Aliases: []string{"repo.link"},
Usage: "repo url",
EnvVars: []string{
Sources: cli.EnvVars(
"CI_REPO_URL",
"CI_REPO_LINK",
"DRONE_REPO_LINK",
},
),
},
&cli.StringFlag{
Name: "repo.clone-url",
Usage: "repo clone url",
EnvVars: []string{
Sources: cli.EnvVars(
"CI_REPO_CLONE_URL",
"DRONE_GIT_HTTP_URL",
},
),
},
&cli.BoolFlag{
Name: "repo.private",
Usage: "repo private",
EnvVars: []string{
Sources: cli.EnvVars(
"CI_REPO_PRIVATE",
"DRONE_REPO_PRIVATE",
},
),
},
&cli.StringFlag{
Name: "repo.default-branch",
Usage: "repo default branch",
EnvVars: []string{
Sources: cli.EnvVars(
"CI_REPO_DEFAULT_BRANCH",
"DRONE_REPO_BRANCH",
},
),
},
}
}
func repositoryFromContext(c *cli.Context) Repository {
func repositoryFromContext(c *cli.Command) Repository {
return Repository{
RemoteID: c.String("repo.remote-id"),
Name: c.String("repo.name"),

26
step.go
View File

@ -17,7 +17,7 @@ package plugin
import (
"time"
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v3"
)
// Step defines runtime metadata for a step.
@ -32,32 +32,32 @@ func stepFlags() []cli.Flag {
&cli.IntFlag{
Name: "step.number",
Usage: "step number",
EnvVars: []string{
Sources: cli.EnvVars(
"CI_STEP_NUMBER",
"DRONE_STEP_NUMBER",
},
),
},
&cli.Int64Flag{
&cli.IntFlag{
Name: "step.started",
Usage: "step start time",
EnvVars: []string{
Sources: cli.EnvVars(
"CI_STEP_STARTED",
},
),
},
&cli.Int64Flag{
&cli.IntFlag{
Name: "step.finished",
Usage: "step finish time",
EnvVars: []string{
Sources: cli.EnvVars(
"CI_STEP_FINISHED",
},
),
},
}
}
func stepFromContext(c *cli.Context) Step {
func stepFromContext(c *cli.Command) Step {
return Step{
Number: c.Int("step.number"),
Started: time.Unix(c.Int64("step.started"), 0),
Finished: time.Unix(c.Int64("step.finished"), 0),
Number: int(c.Int("step.number")),
Started: time.Unix(c.Int("step.started"), 0),
Finished: time.Unix(c.Int("step.finished"), 0),
}
}

View File

@ -17,7 +17,7 @@ package plugin
import (
"os"
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v3"
)
// System defines runtime metadata for a ci/cd system.
@ -37,50 +37,50 @@ func systemFlags() []cli.Flag {
&cli.StringFlag{
Name: "system.name",
Usage: "system name",
EnvVars: []string{
Sources: cli.EnvVars(
"CI_SYSTEM_NAME",
},
),
},
&cli.StringFlag{
Name: "system.host",
Usage: "system host",
EnvVars: []string{
Sources: cli.EnvVars(
"CI_SYSTEM_HOST",
"DRONE_SYSTEM_HOST",
"DRONE_SYSTEM_HOSTNAME",
},
),
},
&cli.StringFlag{
Name: "system.url",
Aliases: []string{"system.link"},
Usage: "system url",
EnvVars: []string{
Sources: cli.EnvVars(
"CI_SYSTEM_URL",
"CI_SYSTEM_LINK",
},
),
},
&cli.StringFlag{
Name: "system.arch",
Usage: "system arch",
EnvVars: []string{
Sources: cli.EnvVars(
"CI_SYSTEM_PLATFORM",
"DRONE_STAGE_ARCH",
},
),
},
&cli.StringFlag{
Name: "system.version",
Usage: "system version",
EnvVars: []string{
Sources: cli.EnvVars(
"CI_SYSTEM_VERSION",
"DRONE_SYSTEM_VERSION",
},
),
},
}
}
func systemFromContext(ctx *cli.Context) System {
url := ctx.String("system.url")
host := ctx.String("system.host")
func systemFromContext(c *cli.Command) System {
url := c.String("system.url")
host := c.String("system.host")
if url == "" && host != "" {
// Alternative url format used by Drone.
proto := os.Getenv("DRONE_SYSTEM_PROTO")
@ -89,11 +89,11 @@ func systemFromContext(ctx *cli.Context) System {
}
}
return System{
Name: ctx.String("system.name"),
Name: c.String("system.name"),
Host: host,
URL: url,
Link: url,
Platform: ctx.String("system.arch"),
Version: ctx.String("system.version"),
Platform: c.String("system.arch"),
Version: c.String("system.version"),
}
}