From be61f319f79120ba7401c3a926fd001b417d9984 Mon Sep 17 00:00:00 2001 From: Robert Kaussow Date: Sun, 13 Aug 2023 22:03:55 +0200 Subject: [PATCH] feat: expose cli context (#10) --- README.md | 4 ++-- plugin/plugin.go | 13 +++++++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 87e50fb..1c71f54 100644 --- a/README.md +++ b/README.md @@ -13,13 +13,13 @@ Helper library to reduce the boilerplate code for writing Woodpecker CI plugins. ### Download the package ```Shell -go get -d github.com/thegeeklab/wp-plugin-go/woodpecker +go get -d github.com/thegeeklab/wp-plugin-go ``` ### Import the package ```Go -import "github.com/thegeeklab/wp-plugin-go/woodpecker" +import "github.com/thegeeklab/wp-plugin-go" ``` ## Contributors diff --git a/plugin/plugin.go b/plugin/plugin.go index 578605b..1dfdb8a 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -16,8 +16,10 @@ package plugin import ( "context" + "fmt" "net/http" "os" + "strings" "github.com/joho/godotenv" "github.com/rs/zerolog/log" @@ -32,6 +34,8 @@ type Options struct { Description string // Version of the plugin. Version string + // Version metadata of the plugin. + VersionMetadata string // Flags of the plugin. Flags []cli.Flag // Execute function of the plugin. @@ -48,7 +52,7 @@ type Plugin struct { } // ExecuteFunc defines the function that is executed by the plugin. -type ExecuteFunc func(ctx context.Context) error +type ExecuteFunc func(ctx context.Context, cCtx *cli.Context) error // New plugin instance. func New(opt Options) *Plugin { @@ -63,6 +67,11 @@ func New(opt Options) *Plugin { Flags: append(opt.Flags, Flags()...), } + cli.VersionPrinter = func(c *cli.Context) { + version := fmt.Sprintf("%s version=%s %s\n", c.App.Name, c.App.Version, opt.VersionMetadata) + fmt.Print(strings.TrimSpace(version)) + } + plugin := &Plugin{ app: app, execute: opt.Execute, @@ -84,7 +93,7 @@ func (p *Plugin) action(ctx *cli.Context) error { panic("plugin execute function is not set") } - return p.execute(ctx.Context) + return p.execute(ctx.Context, ctx) } // HTTPClient returns the http.Client instance.