From 35072e3425b6c758d836d37d6b3fb1c1978212e2 Mon Sep 17 00:00:00 2001 From: Robert Kaussow Date: Fri, 3 May 2024 22:51:11 +0200 Subject: [PATCH] feat: add option to hide woodpecker system flags (#68) --- plugin/logger.go | 6 +++--- plugin/network.go | 2 +- plugin/plugin.go | 43 +++++++++++++++++++++++++++++++++++++++---- template/template.go | 14 +++++++------- 4 files changed, 50 insertions(+), 15 deletions(-) diff --git a/plugin/logger.go b/plugin/logger.go index df7cd12..cc6ee35 100644 --- a/plugin/logger.go +++ b/plugin/logger.go @@ -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 diff --git a/plugin/network.go b/plugin/network.go index c3f3e8e..d5724a4 100644 --- a/plugin/network.go +++ b/plugin/network.go @@ -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{ diff --git a/plugin/plugin.go b/plugin/plugin.go index c9480f2..17d8693 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -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 diff --git a/template/template.go b/template/template.go index f2a1f16..8da3f4a 100644 --- a/template/template.go +++ b/template/template.go @@ -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 }