Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Urfave cli 2 shrink #78

Merged
merged 2 commits into from
Aug 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
}
})
}