Skip to content

Commit

Permalink
Urfave cli 2 shrink (#78)
Browse files Browse the repository at this point in the history
* Remove hidden gendocs command during release builds

* Remove profiling and gendocs in release version of app, save 500KB
  • Loading branch information
zix99 committed Aug 21, 2022
1 parent fccfed4 commit 6b3b53b
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 40 deletions.
10 changes: 9 additions & 1 deletion .goreleaser.yml
Expand Up @@ -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}}
Expand All @@ -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}}
Expand Down
37 changes: 37 additions & 0 deletions 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())
}
19 changes: 19 additions & 0 deletions 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)
}
48 changes: 9 additions & 39 deletions main.go
Expand Up @@ -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()

Expand Down Expand Up @@ -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
Expand All @@ -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") {
Expand All @@ -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
})

Expand All @@ -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)
}

Expand Down
38 changes: 38 additions & 0 deletions profiling.go
@@ -1,10 +1,14 @@
//go:build !rare_no_pprof

package main

import (
"fmt"
"os"
"runtime/pprof"
"time"

"github.com/urfave/cli/v2"
)

var profilerDone chan bool
Expand Down Expand Up @@ -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
}
})
}

0 comments on commit 6b3b53b

Please sign in to comment.