feat: expose cli context (#10)
This commit is contained in:
parent
c2338b40b8
commit
be61f319f7
|
@ -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
|
||||
|
|
|
@ -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.
|
||||
|
|
Loading…
Reference in New Issue