Skip to content

Commit 77fb75a

Browse files
authored
Merge pull request #1375 from kolyshkin/no-docs
Add `urfave_cli_no_docs` build tag
2 parents 924052a + e20d3d4 commit 77fb75a

File tree

6 files changed

+160
-133
lines changed

6 files changed

+160
-133
lines changed

.github/workflows/cli.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,18 @@ jobs:
3737
- name: vet
3838
run: go run internal/build/build.go vet
3939

40+
- name: test with tags
41+
run: go run internal/build/build.go -tags urfave_cli_no_docs test
42+
4043
- name: test
4144
run: go run internal/build/build.go test
4245

4346
- name: check-binary-size
4447
run: go run internal/build/build.go check-binary-size
4548

49+
- name: check-binary-size with tags (informational only)
50+
run: go run internal/build/build.go -tags urfave_cli_no_docs check-binary-size || true
51+
4652
- name: Upload coverage to Codecov
4753
if: success() && matrix.go == '1.18.x' && matrix.os == 'ubuntu-latest'
4854
uses: codecov/codecov-action@v2

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ import (
5555
...
5656
```
5757

58+
### Build tags
59+
60+
You can use the following build tags:
61+
62+
#### `urfave_cli_no_docs`
63+
64+
When set, this removes `ToMarkdown` and `ToMan` methods, so your application
65+
won't be able to call those. This reduces the resulting binary size by about
66+
300-400 KB (measured using Go 1.18.1 on Linux/amd64), due to less dependencies.
67+
5868
### GOPATH
5969

6070
Make sure your `PATH` includes the `$GOPATH/bin` directory so your commands can

docs.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//go:build !urfave_cli_no_docs
2+
// +build !urfave_cli_no_docs
3+
14
package cli
25

36
import (

docs_test.go

Lines changed: 3 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -1,138 +1,13 @@
1+
//go:build !urfave_cli_no_docs
2+
// +build !urfave_cli_no_docs
3+
14
package cli
25

36
import (
4-
"bytes"
57
"errors"
6-
"io/ioutil"
78
"testing"
89
)
910

10-
func testApp() *App {
11-
app := newTestApp()
12-
app.Name = "greet"
13-
app.Flags = []Flag{
14-
&StringFlag{
15-
Name: "socket",
16-
Aliases: []string{"s"},
17-
Usage: "some 'usage' text",
18-
Value: "value",
19-
TakesFile: true,
20-
},
21-
&StringFlag{Name: "flag", Aliases: []string{"fl", "f"}},
22-
&BoolFlag{
23-
Name: "another-flag",
24-
Aliases: []string{"b"},
25-
Usage: "another usage text",
26-
},
27-
&BoolFlag{
28-
Name: "hidden-flag",
29-
Hidden: true,
30-
},
31-
}
32-
app.Commands = []*Command{{
33-
Aliases: []string{"c"},
34-
Flags: []Flag{
35-
&StringFlag{
36-
Name: "flag",
37-
Aliases: []string{"fl", "f"},
38-
TakesFile: true,
39-
},
40-
&BoolFlag{
41-
Name: "another-flag",
42-
Aliases: []string{"b"},
43-
Usage: "another usage text",
44-
},
45-
},
46-
Name: "config",
47-
Usage: "another usage test",
48-
Subcommands: []*Command{{
49-
Aliases: []string{"s", "ss"},
50-
Flags: []Flag{
51-
&StringFlag{Name: "sub-flag", Aliases: []string{"sub-fl", "s"}},
52-
&BoolFlag{
53-
Name: "sub-command-flag",
54-
Aliases: []string{"s"},
55-
Usage: "some usage text",
56-
},
57-
},
58-
Name: "sub-config",
59-
Usage: "another usage test",
60-
}},
61-
}, {
62-
Aliases: []string{"i", "in"},
63-
Name: "info",
64-
Usage: "retrieve generic information",
65-
}, {
66-
Name: "some-command",
67-
}, {
68-
Name: "hidden-command",
69-
Hidden: true,
70-
}, {
71-
Aliases: []string{"u"},
72-
Flags: []Flag{
73-
&StringFlag{
74-
Name: "flag",
75-
Aliases: []string{"fl", "f"},
76-
TakesFile: true,
77-
},
78-
&BoolFlag{
79-
Name: "another-flag",
80-
Aliases: []string{"b"},
81-
Usage: "another usage text",
82-
},
83-
},
84-
Name: "usage",
85-
Usage: "standard usage text",
86-
UsageText: `
87-
Usage for the usage text
88-
- formatted: Based on the specified ConfigMap and summon secrets.yml
89-
- list: Inspect the environment for a specific process running on a Pod
90-
- for_effect: Compare 'namespace' environment with 'local'
91-
92-
` + "```" + `
93-
func() { ... }
94-
` + "```" + `
95-
96-
Should be a part of the same code block
97-
`,
98-
Subcommands: []*Command{{
99-
Aliases: []string{"su"},
100-
Flags: []Flag{
101-
&BoolFlag{
102-
Name: "sub-command-flag",
103-
Aliases: []string{"s"},
104-
Usage: "some usage text",
105-
},
106-
&StringFlag{
107-
Name: "sub-command-hidden-flag",
108-
Usage: "some hidden usage text",
109-
Hidden: true,
110-
},
111-
},
112-
Name: "sub-usage",
113-
Usage: "standard usage text",
114-
UsageText: "Single line of UsageText",
115-
}},
116-
}}
117-
app.UsageText = "app [first_arg] [second_arg]"
118-
app.Description = `Description of the application.`
119-
app.Usage = "Some app"
120-
app.Authors = []*Author{
121-
{Name: "Harrison", Email: "[email protected]"},
122-
{Name: "Oliver Allen", Email: "[email protected]"},
123-
}
124-
return app
125-
}
126-
127-
func expectFileContent(t *testing.T, file, got string) {
128-
data, err := ioutil.ReadFile(file)
129-
// Ignore windows line endings
130-
// TODO: Replace with bytes.ReplaceAll when support for Go 1.11 is dropped
131-
data = bytes.Replace(data, []byte("\r\n"), []byte("\n"), -1)
132-
expect(t, err, nil)
133-
expect(t, got, string(data))
134-
}
135-
13611
func TestToMarkdownFull(t *testing.T) {
13712
// Given
13813
app := testApp()

fish_test.go

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package cli
22

33
import (
4+
"bytes"
5+
"io/ioutil"
46
"testing"
57
)
68

@@ -19,3 +21,124 @@ func TestFishCompletion(t *testing.T) {
1921
expect(t, err, nil)
2022
expectFileContent(t, "testdata/expected-fish-full.fish", res)
2123
}
24+
25+
func testApp() *App {
26+
app := newTestApp()
27+
app.Name = "greet"
28+
app.Flags = []Flag{
29+
&StringFlag{
30+
Name: "socket",
31+
Aliases: []string{"s"},
32+
Usage: "some 'usage' text",
33+
Value: "value",
34+
TakesFile: true,
35+
},
36+
&StringFlag{Name: "flag", Aliases: []string{"fl", "f"}},
37+
&BoolFlag{
38+
Name: "another-flag",
39+
Aliases: []string{"b"},
40+
Usage: "another usage text",
41+
},
42+
&BoolFlag{
43+
Name: "hidden-flag",
44+
Hidden: true,
45+
},
46+
}
47+
app.Commands = []*Command{{
48+
Aliases: []string{"c"},
49+
Flags: []Flag{
50+
&StringFlag{
51+
Name: "flag",
52+
Aliases: []string{"fl", "f"},
53+
TakesFile: true,
54+
},
55+
&BoolFlag{
56+
Name: "another-flag",
57+
Aliases: []string{"b"},
58+
Usage: "another usage text",
59+
},
60+
},
61+
Name: "config",
62+
Usage: "another usage test",
63+
Subcommands: []*Command{{
64+
Aliases: []string{"s", "ss"},
65+
Flags: []Flag{
66+
&StringFlag{Name: "sub-flag", Aliases: []string{"sub-fl", "s"}},
67+
&BoolFlag{
68+
Name: "sub-command-flag",
69+
Aliases: []string{"s"},
70+
Usage: "some usage text",
71+
},
72+
},
73+
Name: "sub-config",
74+
Usage: "another usage test",
75+
}},
76+
}, {
77+
Aliases: []string{"i", "in"},
78+
Name: "info",
79+
Usage: "retrieve generic information",
80+
}, {
81+
Name: "some-command",
82+
}, {
83+
Name: "hidden-command",
84+
Hidden: true,
85+
}, {
86+
Aliases: []string{"u"},
87+
Flags: []Flag{
88+
&StringFlag{
89+
Name: "flag",
90+
Aliases: []string{"fl", "f"},
91+
TakesFile: true,
92+
},
93+
&BoolFlag{
94+
Name: "another-flag",
95+
Aliases: []string{"b"},
96+
Usage: "another usage text",
97+
},
98+
},
99+
Name: "usage",
100+
Usage: "standard usage text",
101+
UsageText: `
102+
Usage for the usage text
103+
- formatted: Based on the specified ConfigMap and summon secrets.yml
104+
- list: Inspect the environment for a specific process running on a Pod
105+
- for_effect: Compare 'namespace' environment with 'local'
106+
107+
` + "```" + `
108+
func() { ... }
109+
` + "```" + `
110+
111+
Should be a part of the same code block
112+
`,
113+
Subcommands: []*Command{{
114+
Aliases: []string{"su"},
115+
Flags: []Flag{
116+
&BoolFlag{
117+
Name: "sub-command-flag",
118+
Aliases: []string{"s"},
119+
Usage: "some usage text",
120+
},
121+
},
122+
Name: "sub-usage",
123+
Usage: "standard usage text",
124+
UsageText: "Single line of UsageText",
125+
}},
126+
}}
127+
app.UsageText = "app [first_arg] [second_arg]"
128+
app.Description = `Description of the application.`
129+
app.Usage = "Some app"
130+
app.Authors = []*Author{
131+
{Name: "Harrison", Email: "[email protected]"},
132+
{Name: "Oliver Allen", Email: "[email protected]"},
133+
}
134+
return app
135+
}
136+
137+
func expectFileContent(t *testing.T, file, got string) {
138+
data, err := ioutil.ReadFile(file)
139+
// Ignore windows line endings
140+
// TODO: Replace with bytes.ReplaceAll when support for Go 1.11 is dropped
141+
data = bytes.Replace(data, []byte("\r\n"), []byte("\n"), -1)
142+
expect(t, err, nil)
143+
expect(t, got, string(data))
144+
}

internal/build/build.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ func main() {
4646
Action: checkBinarySizeActionFunc,
4747
},
4848
}
49+
app.Flags = []cli.Flag{
50+
&cli.StringFlag{
51+
Name: "tags",
52+
Usage: "set build tags",
53+
},
54+
}
4955

5056
err := app.Run(os.Args)
5157
if err != nil {
@@ -68,6 +74,8 @@ func VetActionFunc(_ *cli.Context) error {
6874
}
6975

7076
func TestActionFunc(c *cli.Context) error {
77+
tags := c.String("tags")
78+
7179
for _, pkg := range packages {
7280
var packageName string
7381

@@ -79,7 +87,7 @@ func TestActionFunc(c *cli.Context) error {
7987

8088
coverProfile := fmt.Sprintf("--coverprofile=%s.coverprofile", pkg)
8189

82-
err := runCmd("go", "test", "-v", coverProfile, packageName)
90+
err := runCmd("go", "test", "-tags", tags, "-v", coverProfile, packageName)
8391
if err != nil {
8492
return err
8593
}
@@ -201,14 +209,16 @@ func checkBinarySizeActionFunc(c *cli.Context) (err error) {
201209
mbStringFormatter = "%.1fMB"
202210
)
203211

212+
tags := c.String("tags")
213+
204214
// get cli example size
205-
cliSize, err := getSize(cliSourceFilePath, cliBuiltFilePath)
215+
cliSize, err := getSize(cliSourceFilePath, cliBuiltFilePath, tags)
206216
if err != nil {
207217
return err
208218
}
209219

210220
// get hello world size
211-
helloSize, err := getSize(helloSourceFilePath, helloBuiltFilePath)
221+
helloSize, err := getSize(helloSourceFilePath, helloBuiltFilePath, tags)
212222
if err != nil {
213223
return err
214224
}
@@ -270,9 +280,9 @@ func checkBinarySizeActionFunc(c *cli.Context) (err error) {
270280
return nil
271281
}
272282

273-
func getSize(sourcePath string, builtPath string) (size int64, err error) {
283+
func getSize(sourcePath string, builtPath string, tags string) (size int64, err error) {
274284
// build example binary
275-
err = runCmd("go", "build", "-o", builtPath, "-ldflags", "-s -w", sourcePath)
285+
err = runCmd("go", "build", "-tags", tags, "-o", builtPath, "-ldflags", "-s -w", sourcePath)
276286
if err != nil {
277287
fmt.Println("issue getting size for example binary")
278288
return 0, err

0 commit comments

Comments
 (0)