feat: add option to hide woodpecker system flags (#68)

This commit is contained in:
Robert Kaussow 2024-05-03 22:51:11 +02:00 committed by GitHub
parent 86776b673a
commit 35072e3425
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 50 additions and 15 deletions

View File

@ -26,8 +26,8 @@ func loggingFlags(category string) []cli.Flag {
return []cli.Flag{
&cli.StringFlag{
Name: "log-level",
Usage: "log level",
EnvVars: []string{"CI_LOG_LEVEL"},
Usage: "plugin log level",
EnvVars: []string{"PLUGIN_LOG_LEVEL"},
Value: "info",
Category: category,
},
@ -51,7 +51,7 @@ func SetupConsoleLogger(c *cli.Context) error {
if zerolog.GlobalLevel() <= zerolog.DebugLevel {
log.Logger = log.With().Caller().Logger()
log.Log().Msgf("LogLevel = %s", zerolog.GlobalLevel().String())
log.Info().Msgf("LogLevel = %s", zerolog.GlobalLevel().String())
}
return nil

View File

@ -57,7 +57,7 @@ func networkFlags(category string) []cli.Flag {
&cli.BoolFlag{
Name: "transport.skip-verify",
Usage: "skip ssl verify",
EnvVars: []string{"CI_SKIP_VERIFY"},
EnvVars: []string{"PLUGIN_SKIP_VERIFY"},
Category: category,
},
&cli.StringFlag{

View File

@ -27,6 +27,37 @@ import (
"github.com/urfave/cli/v2"
)
//nolint:lll
const appHelpTemplate = `NAME:
{{template "helpNameTemplate" .}}
USAGE:
{{if .UsageText}}{{wrap .UsageText 3}}{{else}}{{.HelpName}} {{if .VisibleFlags}}[global options]{{end}}{{if .Commands}} command [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}{{if .Args}}[arguments...]{{end}}{{end}}{{end}}{{if .Version}}{{if not .HideVersion}}
VERSION:
{{.Version}}{{end}}{{end}}{{if .Description}}
DESCRIPTION:
{{template "descriptionTemplate" .}}{{end}}
{{- if len .Authors}}
AUTHOR{{template "authorsTemplate" .}}{{end}}{{if .VisibleCommands}}
COMMANDS:{{template "visibleCommandCategoryTemplate" .}}{{end}}{{if .VisibleFlagCategories}}
GLOBAL OPTIONS:{{range .VisibleFlagCategories}}{{if and .Name (ne .Name "Plugin Flags")}}{{continue}}{{end}}
{{if .Name}}{{.Name}}
{{end}}{{$flglen := len .Flags}}{{range $i, $e := .Flags}}{{if eq (subtract $flglen $i) 1}}{{$e}}
{{else}}{{$e}}
{{end}}{{end}}{{end}}{{else if .VisibleFlags}}
GLOBAL OPTIONS:{{template "visibleFlagTemplate" .}}{{end}}{{if .Copyright}}
COPYRIGHT:
{{template "copyrightTemplate" .}}{{end}}
`
// Options defines the options for the plugin.
type Options struct {
// Name of the plugin.
@ -41,6 +72,8 @@ type Options struct {
Flags []cli.Flag
// Execute function of the plugin.
Execute ExecuteFunc
// Hide woodpecker system flags.
HideWoodpeckerFlags bool
}
// Plugin defines the plugin instance.
@ -68,6 +101,12 @@ func New(opt Options) *Plugin {
Usage: opt.Description,
Version: opt.Version,
Flags: append(opt.Flags, Flags()...),
Before: SetupConsoleLogger,
After: SetupConsoleLogger,
}
if opt.HideWoodpeckerFlags {
app.CustomAppHelpTemplate = appHelpTemplate
}
cli.VersionPrinter = func(c *cli.Context) {
@ -85,10 +124,6 @@ func New(opt Options) *Plugin {
}
func (p *Plugin) action(ctx *cli.Context) error {
if err := SetupConsoleLogger(ctx); err != nil {
return err
}
p.Metadata = MetadataFromContext(ctx)
p.Network = NetworkFromContext(ctx)
p.Context = ctx

View File

@ -22,12 +22,12 @@ import (
// format. Trailing or leading spaces or new-lines are not getting truncated. It
// is able to read templates from remote paths, local files or directly from the
// string.
func Render(ctx context.Context, client http.Client, templateString string, payload interface{}) (string, error) {
func Render(ctx context.Context, client http.Client, tmpl string, payload interface{}) (string, error) {
var outString bytes.Buffer
tpl := new(template.Template).Funcs(LoadFuncMap())
templateURL, err := url.Parse(templateString)
templateURL, err := url.Parse(tmpl)
if err == nil {
switch templateURL.Scheme {
case "http", "https":
@ -48,18 +48,18 @@ func Render(ctx context.Context, client http.Client, templateString string, payl
return "", fmt.Errorf("failed to read: %w", err)
}
templateString = string(out)
tmpl = string(out)
case "file":
out, err := os.ReadFile(templateURL.Path)
if err != nil {
return "", fmt.Errorf("failed to read: %w", err)
}
templateString = string(out)
tmpl = string(out)
}
}
tpl, err = tpl.Parse(templateString)
tpl, err = tpl.Parse(tmpl)
if err != nil {
return "", err
}
@ -72,8 +72,8 @@ func Render(ctx context.Context, client http.Client, templateString string, payl
// RenderTrim parses and executes a template, returning the results in string
// format. The result is trimmed to remove left and right padding and newlines
// that may be added unintentially in the template markup.
func RenderTrim(ctx context.Context, client http.Client, template string, playload interface{}) (string, error) {
out, err := Render(ctx, client, template, playload)
func RenderTrim(ctx context.Context, client http.Client, tmpl string, playload interface{}) (string, error) {
out, err := Render(ctx, client, tmpl, playload)
return strings.Trim(out, " \n"), err
}