From 6b3b53b67976fed52d108f13801352b1c2faf9d3 Mon Sep 17 00:00:00 2001 From: Chris LaPointe Date: Sat, 20 Aug 2022 21:40:46 -0400 Subject: [PATCH] Urfave cli 2 shrink (#78) * Remove hidden gendocs command during release builds * Remove profiling and gendocs in release version of app, save 500KB --- .goreleaser.yml | 10 +++++++++- cmd/gendocs.go | 37 ++++++++++++++++++++++++++++++++++ cmd/gendocs_test.go | 19 ++++++++++++++++++ main.go | 48 +++++++++------------------------------------ profiling.go | 38 +++++++++++++++++++++++++++++++++++ 5 files changed, 112 insertions(+), 40 deletions(-) create mode 100644 cmd/gendocs.go create mode 100644 cmd/gendocs_test.go diff --git a/.goreleaser.yml b/.goreleaser.yml index cd0068b..956344f 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -9,8 +9,11 @@ builds: env: - CGO_ENABLED=1 flags: - - -tags=pcre2 - -trimpath + tags: + - pcre2 + - urfave_cli_no_docs + - rare_no_pprof ldflags: - -s -w - -X main.version={{.Version}} @@ -22,6 +25,11 @@ builds: - id: rare env: - CGO_ENABLED=0 + flags: + - -trimpath + tags: + - urfave_cli_no_docs + - rare_no_pprof ldflags: - -s -w - -X main.version={{.Version}} diff --git a/cmd/gendocs.go b/cmd/gendocs.go new file mode 100644 index 0000000..ec1ecb3 --- /dev/null +++ b/cmd/gendocs.go @@ -0,0 +1,37 @@ +//go:build !urfave_cli_no_docs + +package cmd + +import ( + "fmt" + + "github.com/urfave/cli/v2" +) + +func gendocCommand() *cli.Command { + return &cli.Command{ + Name: "_gendoc", + Hidden: true, + Usage: "Generates documentation", + Action: func(c *cli.Context) error { + var text string + if c.Bool("man") { + text, _ = c.App.ToMan() + } else { + text, _ = c.App.ToMarkdown() + } + fmt.Print(text) + return nil + }, + Flags: []cli.Flag{ + &cli.BoolFlag{ + Name: "man", + Usage: "manpage syntax", + }, + }, + } +} + +func init() { + commands = append(commands, gendocCommand()) +} diff --git a/cmd/gendocs_test.go b/cmd/gendocs_test.go new file mode 100644 index 0000000..b49717d --- /dev/null +++ b/cmd/gendocs_test.go @@ -0,0 +1,19 @@ +//go:build !urfave_cli_no_docs + +package cmd + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestGenDocs(t *testing.T) { + out, _, err := testCommandCapture(gendocCommand(), ``) + assert.NoError(t, err) + assert.NotEmpty(t, out) + + out, _, err = testCommandCapture(gendocCommand(), `--man`) + assert.NoError(t, err) + assert.NotEmpty(t, out) +} diff --git a/main.go b/main.go index 9b165d3..eeb6417 100644 --- a/main.go +++ b/main.go @@ -16,6 +16,10 @@ import ( "github.com/urfave/cli/v2" ) +type appModifier func(app *cli.App) + +var appModifiers []appModifier + func cliMain(args ...string) error { app := cli.NewApp() @@ -62,10 +66,6 @@ func cliMain(args ...string) error { Name: "notrim", Usage: "By default, rare will trim output text for in-place updates. Setting this flag will disable that", }, - &cli.StringFlag{ - Name: "profile", - Usage: "Write application profiling information as part of execution. Specify base-name", - }, } // When showing default help, exit with an error code @@ -86,27 +86,6 @@ func cliMain(args ...string) error { } app.Commands = cmd.GetSupportedCommands() - app.Commands = append(app.Commands, &cli.Command{ - Name: "_gendoc", - Hidden: true, - Usage: "Generates documentation", - Action: func(c *cli.Context) error { - var text string - if c.Bool("man") { - text, _ = c.App.ToMan() - } else { - text, _ = c.App.ToMarkdown() - } - fmt.Print(text) - return nil - }, - Flags: []cli.Flag{ - &cli.BoolFlag{ - Name: "man", - Usage: "manpage syntax", - }, - }, - }) app.Before = cli.BeforeFunc(func(c *cli.Context) error { if c.Bool("nocolor") { @@ -123,20 +102,6 @@ func cliMain(args ...string) error { if c.Bool("nounicode") { termunicode.UnicodeEnabled = false } - - // Profiling - if c.IsSet("profile") { - basename := c.String("profile") - startProfiler(basename) - } - - return nil - }) - - app.After = cli.AfterFunc(func(c *cli.Context) error { - if c.IsSet("profile") { - stopProfile() - } return nil }) @@ -146,6 +111,11 @@ func cliMain(args ...string) error { // This also allows for better unit testing... } + // Apply any plugin/modifiers + for _, modifier := range appModifiers { + modifier(app) + } + return app.Run(args) } diff --git a/profiling.go b/profiling.go index 2302cba..c00c939 100644 --- a/profiling.go +++ b/profiling.go @@ -1,3 +1,5 @@ +//go:build !rare_no_pprof + package main import ( @@ -5,6 +7,8 @@ import ( "os" "runtime/pprof" "time" + + "github.com/urfave/cli/v2" ) var profilerDone chan bool @@ -36,3 +40,37 @@ func stopProfile() { pprof.StopCPUProfile() profilerDone <- true } + +func init() { + appModifiers = append(appModifiers, func(app *cli.App) { + app.Flags = append(app.Flags, &cli.StringFlag{ + Name: "profile", + Usage: "Write application profiling information as part of execution. Specify base-name", + }) + + oldBefore := app.Before + app.Before = func(c *cli.Context) error { + if c.IsSet("profile") { + basename := c.String("profile") + startProfiler(basename) + } + + if oldBefore != nil { + return oldBefore(c) + } + return nil + } + + oldAfter := app.After + app.After = func(c *cli.Context) error { + if c.IsSet("profile") { + stopProfile() + } + + if oldAfter != nil { + return oldAfter(c) + } + return nil + } + }) +}