feat: expose cli context (#10)

This commit is contained in:
Robert Kaussow 2023-08-13 22:03:55 +02:00 committed by GitHub
parent c2338b40b8
commit be61f319f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 4 deletions

View File

@ -13,13 +13,13 @@ Helper library to reduce the boilerplate code for writing Woodpecker CI plugins.
### Download the package ### Download the package
```Shell ```Shell
go get -d github.com/thegeeklab/wp-plugin-go/woodpecker go get -d github.com/thegeeklab/wp-plugin-go
``` ```
### Import the package ### Import the package
```Go ```Go
import "github.com/thegeeklab/wp-plugin-go/woodpecker" import "github.com/thegeeklab/wp-plugin-go"
``` ```
## Contributors ## Contributors

View File

@ -16,8 +16,10 @@ package plugin
import ( import (
"context" "context"
"fmt"
"net/http" "net/http"
"os" "os"
"strings"
"github.com/joho/godotenv" "github.com/joho/godotenv"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
@ -32,6 +34,8 @@ type Options struct {
Description string Description string
// Version of the plugin. // Version of the plugin.
Version string Version string
// Version metadata of the plugin.
VersionMetadata string
// Flags of the plugin. // Flags of the plugin.
Flags []cli.Flag Flags []cli.Flag
// Execute function of the plugin. // Execute function of the plugin.
@ -48,7 +52,7 @@ type Plugin struct {
} }
// ExecuteFunc defines the function that is executed by the plugin. // 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. // New plugin instance.
func New(opt Options) *Plugin { func New(opt Options) *Plugin {
@ -63,6 +67,11 @@ func New(opt Options) *Plugin {
Flags: append(opt.Flags, Flags()...), 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{ plugin := &Plugin{
app: app, app: app,
execute: opt.Execute, execute: opt.Execute,
@ -84,7 +93,7 @@ func (p *Plugin) action(ctx *cli.Context) error {
panic("plugin execute function is not set") panic("plugin execute function is not set")
} }
return p.execute(ctx.Context) return p.execute(ctx.Context, ctx)
} }
// HTTPClient returns the http.Client instance. // HTTPClient returns the http.Client instance.