From 13152cca8d6ebc72e90ae44475828d389fc6a660 Mon Sep 17 00:00:00 2001 From: Christopher LaPointe Date: Sat, 20 Aug 2022 21:21:21 -0400 Subject: [PATCH 1/2] Remove hidden gendocs command during release builds --- .goreleaser.yml | 8 +++++++- cmd/gendocs.go | 37 +++++++++++++++++++++++++++++++++++++ cmd/gendocs_test.go | 17 +++++++++++++++++ main.go | 21 --------------------- 4 files changed, 61 insertions(+), 22 deletions(-) create mode 100644 cmd/gendocs.go create mode 100644 cmd/gendocs_test.go diff --git a/.goreleaser.yml b/.goreleaser.yml index cd0068b2..ecb6655f 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -9,8 +9,10 @@ builds: env: - CGO_ENABLED=1 flags: - - -tags=pcre2 - -trimpath + tags: + - pcre2 + - urfave_cli_no_docs ldflags: - -s -w - -X main.version={{.Version}} @@ -22,6 +24,10 @@ builds: - id: rare env: - CGO_ENABLED=0 + flags: + - -trimpath + tags: + - urfave_cli_no_docs ldflags: - -s -w - -X main.version={{.Version}} diff --git a/cmd/gendocs.go b/cmd/gendocs.go new file mode 100644 index 00000000..ec1ecb3f --- /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 00000000..d0625649 --- /dev/null +++ b/cmd/gendocs_test.go @@ -0,0 +1,17 @@ +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 9b165d36..bc06a849 100644 --- a/main.go +++ b/main.go @@ -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") { From 18025f8c3c9e4ad534379ffb576fe0618710e736 Mon Sep 17 00:00:00 2001 From: Christopher LaPointe Date: Sat, 20 Aug 2022 21:36:28 -0400 Subject: [PATCH 2/2] Remove profiling and gendocs in release version of app, save 500KB --- .goreleaser.yml | 2 ++ cmd/gendocs_test.go | 2 ++ main.go | 27 +++++++++------------------ profiling.go | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 18 deletions(-) diff --git a/.goreleaser.yml b/.goreleaser.yml index ecb6655f..956344f2 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -13,6 +13,7 @@ builds: tags: - pcre2 - urfave_cli_no_docs + - rare_no_pprof ldflags: - -s -w - -X main.version={{.Version}} @@ -28,6 +29,7 @@ builds: - -trimpath tags: - urfave_cli_no_docs + - rare_no_pprof ldflags: - -s -w - -X main.version={{.Version}} diff --git a/cmd/gendocs_test.go b/cmd/gendocs_test.go index d0625649..b49717de 100644 --- a/cmd/gendocs_test.go +++ b/cmd/gendocs_test.go @@ -1,3 +1,5 @@ +//go:build !urfave_cli_no_docs + package cmd import ( diff --git a/main.go b/main.go index bc06a849..eeb6417d 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 @@ -102,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 }) @@ -125,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 2302cbaa..c00c939b 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 + } + }) +}