From 4a785e9f9ca3f1019b2af799bb1208c359e153b0 Mon Sep 17 00:00:00 2001 From: Lynn Date: Fri, 28 Feb 2020 23:06:39 -0800 Subject: [PATCH 01/27] WIP binary size work --- .github/workflows/cli.yml | 12 ++++++------ go.mod | 1 + go.sum | 17 +++++++++++++++++ build.go => internal/build/build.go | 0 4 files changed, 24 insertions(+), 6 deletions(-) rename build.go => internal/build/build.go (100%) diff --git a/.github/workflows/cli.yml b/.github/workflows/cli.yml index 88c4020..c2924e1 100644 --- a/.github/workflows/cli.yml +++ b/.github/workflows/cli.yml @@ -44,8 +44,8 @@ jobs: - name: Run Tests run: | - go run build.go vet - go run build.go test + go run internal/build/build.go vet + go run internal/build/build.go test - name: Upload coverage to Codecov if: success() && matrix.go == 1.13 && matrix.os == 'ubuntu-latest' @@ -91,11 +91,11 @@ jobs: - name: Run Tests (v1) if: contains(github.base_ref, 'v1') run: | - go run build.go gfmrun docs/v1/manual.md - go run build.go toc docs/v1/manual.md + go run internal/build/build.go gfmrun docs/v1/manual.md + go run internal/build/build.go toc docs/v1/manual.md - name: Run Tests (v2) if: contains(github.base_ref, 'master') run: | - go run build.go gfmrun docs/v2/manual.md - go run build.go toc docs/v2/manual.md + go run internal/build/build.go gfmrun docs/v2/manual.md + go run internal/build/build.go toc docs/v2/manual.md diff --git a/go.mod b/go.mod index c38d41c..2e02cc3 100644 --- a/go.mod +++ b/go.mod @@ -5,5 +5,6 @@ go 1.11 require ( github.com/BurntSushi/toml v0.3.1 github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d + github.com/jondot/goweight v1.0.5 // indirect gopkg.in/yaml.v2 v2.2.2 ) diff --git a/go.sum b/go.sum index ef121ff..f9319fd 100644 --- a/go.sum +++ b/go.sum @@ -1,13 +1,30 @@ github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc h1:cAKDfWh5VpdgMhJosfJnn5/FoN2SRZ4p7fJNX58YPaU= +github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= +github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf h1:qet1QNfXsQxTZqLG4oE62mJzwPIB8+Tee4RNCL9ulrY= +github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY= github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dustin/go-humanize v0.0.0-20180713052910-9f541cc9db5d h1:lDrio3iIdNb0Gw9CgH7cQF+iuB5mOOjdJ9ERNJCBgb4= +github.com/dustin/go-humanize v0.0.0-20180713052910-9f541cc9db5d/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= +github.com/jondot/goweight v1.0.5 h1:aRpnyj1G8BLLNhem8xezuuV0GlFz4G11e3/UtBU/FlQ= +github.com/jondot/goweight v1.0.5/go.mod h1:3PRcpOwkyspe1t4+KCNgauas+aNDTSSCwZ6AQ4kDD/A= +github.com/mattn/go-zglob v0.0.0-20180803001819-2ea3427bfa53 h1:tGfIHhDghvEnneeRhODvGYOt305TPwingKt6p90F4MU= +github.com/mattn/go-zglob v0.0.0-20180803001819-2ea3427bfa53/go.mod h1:9fxibJccNxU2cnpIKLRRFA7zX7qhkJIQWBb449FYHOo= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= +github.com/spf13/pflag v1.0.2/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/thoas/go-funk v0.0.0-20180716193722-1060394a7713 h1:knaxjm6QMbUMNvuaSnJZmw0gRX4V/79JVUQiziJGM84= +github.com/thoas/go-funk v0.0.0-20180716193722-1060394a7713/go.mod h1:mlR+dHGb+4YgXkf13rkQTuzrneeHANxOm6+ZnEV9HsA= +gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc= +gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= diff --git a/build.go b/internal/build/build.go similarity index 100% rename from build.go rename to internal/build/build.go From 1493c542fb047486eec82e7d40dbf81228d45351 Mon Sep 17 00:00:00 2001 From: Lynn Date: Fri, 28 Feb 2020 23:07:26 -0800 Subject: [PATCH 02/27] reset to master --- go.mod | 1 - go.sum | 17 ----------------- 2 files changed, 18 deletions(-) diff --git a/go.mod b/go.mod index 2e02cc3..c38d41c 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,5 @@ go 1.11 require ( github.com/BurntSushi/toml v0.3.1 github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d - github.com/jondot/goweight v1.0.5 // indirect gopkg.in/yaml.v2 v2.2.2 ) diff --git a/go.sum b/go.sum index f9319fd..ef121ff 100644 --- a/go.sum +++ b/go.sum @@ -1,30 +1,13 @@ github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= -github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc h1:cAKDfWh5VpdgMhJosfJnn5/FoN2SRZ4p7fJNX58YPaU= -github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= -github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf h1:qet1QNfXsQxTZqLG4oE62mJzwPIB8+Tee4RNCL9ulrY= -github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY= github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/dustin/go-humanize v0.0.0-20180713052910-9f541cc9db5d h1:lDrio3iIdNb0Gw9CgH7cQF+iuB5mOOjdJ9ERNJCBgb4= -github.com/dustin/go-humanize v0.0.0-20180713052910-9f541cc9db5d/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= -github.com/jondot/goweight v1.0.5 h1:aRpnyj1G8BLLNhem8xezuuV0GlFz4G11e3/UtBU/FlQ= -github.com/jondot/goweight v1.0.5/go.mod h1:3PRcpOwkyspe1t4+KCNgauas+aNDTSSCwZ6AQ4kDD/A= -github.com/mattn/go-zglob v0.0.0-20180803001819-2ea3427bfa53 h1:tGfIHhDghvEnneeRhODvGYOt305TPwingKt6p90F4MU= -github.com/mattn/go-zglob v0.0.0-20180803001819-2ea3427bfa53/go.mod h1:9fxibJccNxU2cnpIKLRRFA7zX7qhkJIQWBb449FYHOo= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= -github.com/spf13/pflag v1.0.2/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= -github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= -github.com/thoas/go-funk v0.0.0-20180716193722-1060394a7713 h1:knaxjm6QMbUMNvuaSnJZmw0gRX4V/79JVUQiziJGM84= -github.com/thoas/go-funk v0.0.0-20180716193722-1060394a7713/go.mod h1:mlR+dHGb+4YgXkf13rkQTuzrneeHANxOm6+ZnEV9HsA= -gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc= -gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= From af5ec51a36f59e1688eb89e32c728b4bbc2e5ae6 Mon Sep 17 00:00:00 2001 From: Lynn Date: Fri, 28 Feb 2020 23:12:46 -0800 Subject: [PATCH 03/27] add example file --- .gitignore | 2 ++ internal/example/example.go | 13 +++++++++++++ 2 files changed, 15 insertions(+) create mode 100644 internal/example/example.go diff --git a/.gitignore b/.gitignore index b013e4a..a660143 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ node_modules/ vendor .idea +internal/example/built-example +coverage.txt diff --git a/internal/example/example.go b/internal/example/example.go new file mode 100644 index 0000000..fd401e2 --- /dev/null +++ b/internal/example/example.go @@ -0,0 +1,13 @@ +// minimal example CLI used for binary size checking + +package main + +import ( + "os" + + "github.com/urfave/cli/v2" +) + +func main() { + (&cli.App{}).Run(os.Args) +} From 863b4a58fe067961387a4ac6238e80bcfb4caadd Mon Sep 17 00:00:00 2001 From: Lynn Date: Fri, 28 Feb 2020 23:19:43 -0800 Subject: [PATCH 04/27] binary size checking hooks --- .github/workflows/cli.yml | 1 + internal/build/build.go | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/.github/workflows/cli.yml b/.github/workflows/cli.yml index c2924e1..c9fa616 100644 --- a/.github/workflows/cli.yml +++ b/.github/workflows/cli.yml @@ -46,6 +46,7 @@ jobs: run: | go run internal/build/build.go vet go run internal/build/build.go test + go run internal/build/build.go check-binary-size - name: Upload coverage to Codecov if: success() && matrix.go == 1.13 && matrix.os == 'ubuntu-latest' diff --git a/internal/build/build.go b/internal/build/build.go index 5a6415a..df3bd88 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -1,5 +1,3 @@ -//+build ignore - package main import ( @@ -40,6 +38,10 @@ func main() { Name: "toc", Action: TocActionFunc, }, + { + Name: "check-binary-size", + Action: checkBinarySizeActionFunc, + }, } err := app.Run(os.Args) @@ -178,3 +180,12 @@ func TocActionFunc(c *cli.Context) error { return nil } + +func checkBinarySizeActionFunc(c *cli.Context) (err error) { + err = runCmd("git", "diff", "--exit-code") + if err != nil { + return err + } + + return nil +} From da34a22eef9bdd273b705723fb6c0ebdb4116811 Mon Sep 17 00:00:00 2001 From: Lynn Date: Fri, 28 Feb 2020 23:20:46 -0800 Subject: [PATCH 05/27] docs --- internal/build/build.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/internal/build/build.go b/internal/build/build.go index df3bd88..de42e81 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -1,3 +1,5 @@ +// local build script file, similar to a makefile or collection of bash scripts in other projects + package main import ( From 3b30b5c4220282c7145344f3d94cd99fdf442e2a Mon Sep 17 00:00:00 2001 From: Lynn Date: Fri, 28 Feb 2020 23:29:27 -0800 Subject: [PATCH 06/27] show file size --- internal/build/build.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/internal/build/build.go b/internal/build/build.go index de42e81..0988e5d 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -184,10 +184,24 @@ func TocActionFunc(c *cli.Context) error { } func checkBinarySizeActionFunc(c *cli.Context) (err error) { - err = runCmd("git", "diff", "--exit-code") + const ( + sourceFilePath = "./internal/example/example.go" + builtFilePath = "./internal/example/built-example" + ) + + err = runCmd("go", "build", "-o", builtFilePath, sourceFilePath) if err != nil { return err } + fileInfo, err := os.Stat(builtFilePath) + if err != nil { + return err + } + fileSize := fileInfo.Size() + + // show the file size + fmt.Println(fileSize) + return nil } From d6dfe3eb9b79dfbb7573ee06fa51ad509c34c30a Mon Sep 17 00:00:00 2001 From: Lynn Date: Fri, 28 Feb 2020 23:30:17 -0800 Subject: [PATCH 07/27] smaller example --- internal/example/example.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/internal/example/example.go b/internal/example/example.go index fd401e2..6fb84e2 100644 --- a/internal/example/example.go +++ b/internal/example/example.go @@ -3,11 +3,9 @@ package main import ( - "os" - "github.com/urfave/cli/v2" ) func main() { - (&cli.App{}).Run(os.Args) + (&cli.App{}).Run([]string{}) } From 45f2a862ff861abed1d2986ddf5f49280547660f Mon Sep 17 00:00:00 2001 From: Lynn Date: Fri, 28 Feb 2020 23:36:34 -0800 Subject: [PATCH 08/27] ci readability --- .github/workflows/cli.yml | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/.github/workflows/cli.yml b/.github/workflows/cli.yml index c9fa616..a779bb2 100644 --- a/.github/workflows/cli.yml +++ b/.github/workflows/cli.yml @@ -42,11 +42,14 @@ jobs: if: matrix.go == 1.13 && matrix.os == 'ubuntu-latest' run: test -z $(gofmt -l .) - - name: Run Tests - run: | - go run internal/build/build.go vet - go run internal/build/build.go test - go run internal/build/build.go check-binary-size + - name: vet + run: go run internal/build/build.go vet + + - name: test + run: go run internal/build/build.go test + + - name: check-binary-size + run: go run internal/build/build.go check-binary-size - name: Upload coverage to Codecov if: success() && matrix.go == 1.13 && matrix.os == 'ubuntu-latest' From 75089f4a6c27bd3444cafd9c77b1ac42830d6dca Mon Sep 17 00:00:00 2001 From: Lynn Date: Fri, 28 Feb 2020 23:51:42 -0800 Subject: [PATCH 09/27] show size in mb --- internal/build/build.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/internal/build/build.go b/internal/build/build.go index 0988e5d..5b66e09 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -8,6 +8,7 @@ import ( "fmt" "io/ioutil" "log" + "math" "os" "os/exec" "strings" @@ -199,9 +200,11 @@ func checkBinarySizeActionFunc(c *cli.Context) (err error) { return err } fileSize := fileInfo.Size() + fileSizeMB := float64(fileSize) / float64(1000000) + roundedFileSizeMB := math.Round(fileSizeMB*10) / 10 // show the file size - fmt.Println(fileSize) + fmt.Println(fmt.Sprintf("current binary size is: %.1fMB", roundedFileSizeMB)) return nil } From e7e165ef1b712c81996df27787a99cda61d9ae61 Mon Sep 17 00:00:00 2001 From: Lynn Date: Fri, 28 Feb 2020 23:56:57 -0800 Subject: [PATCH 10/27] cleanup rounding --- internal/build/build.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/internal/build/build.go b/internal/build/build.go index 5b66e09..356b576 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -190,21 +190,28 @@ func checkBinarySizeActionFunc(c *cli.Context) (err error) { builtFilePath = "./internal/example/built-example" ) + // build example binary err = runCmd("go", "build", "-o", builtFilePath, sourceFilePath) if err != nil { return err } + // get file into fileInfo, err := os.Stat(builtFilePath) if err != nil { return err } + + // get human readable size, in MB with one decimal place. + // example output is: 35.2MB. + // that output is much easier to reason about than the `35223432` + // that you would see output without the rounding fileSize := fileInfo.Size() - fileSizeMB := float64(fileSize) / float64(1000000) - roundedFileSizeMB := math.Round(fileSizeMB*10) / 10 + roundedFileSize := math.Round(float64(fileSize)/float64(1000000)*10) / 10 + roundedFileSizeString := fmt.Sprintf("%.1fMB", roundedFileSize) // show the file size - fmt.Println(fmt.Sprintf("current binary size is: %.1fMB", roundedFileSizeMB)) + fmt.Println(fmt.Sprintf("current binary size is: %s", roundedFileSizeString)) return nil } From 7bdc44fa188229b622109c5f55cf9629d988de4d Mon Sep 17 00:00:00 2001 From: Lynn Date: Fri, 28 Feb 2020 23:58:29 -0800 Subject: [PATCH 11/27] docs --- internal/build/build.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/internal/build/build.go b/internal/build/build.go index 356b576..44b4660 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -184,6 +184,9 @@ func TocActionFunc(c *cli.Context) error { return nil } +// checkBinarySizeActionFunc checks the size of an example binary to ensure that we are keeping size down +// this was originally inspired by https://github.com/urfave/cli/issues/1055, and followed up on as a part +// of https://github.com/urfave/cli/issues/1057 func checkBinarySizeActionFunc(c *cli.Context) (err error) { const ( sourceFilePath = "./internal/example/example.go" From 2571506b8d576f8314716804f9ce1f170baef032 Mon Sep 17 00:00:00 2001 From: Lynn Date: Fri, 28 Feb 2020 23:59:11 -0800 Subject: [PATCH 12/27] docs --- internal/build/build.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/build/build.go b/internal/build/build.go index 44b4660..81a97de 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -206,7 +206,7 @@ func checkBinarySizeActionFunc(c *cli.Context) (err error) { } // get human readable size, in MB with one decimal place. - // example output is: 35.2MB. + // example output is: 35.2MB. (note: this simply an example) // that output is much easier to reason about than the `35223432` // that you would see output without the rounding fileSize := fileInfo.Size() From 3a7e251d4887b07e572f549f655617d5c2c2d054 Mon Sep 17 00:00:00 2001 From: Lynn Date: Sat, 29 Feb 2020 00:25:00 -0800 Subject: [PATCH 13/27] add min maxing --- internal/build/build.go | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/internal/build/build.go b/internal/build/build.go index 81a97de..6013549 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -189,8 +189,14 @@ func TocActionFunc(c *cli.Context) error { // of https://github.com/urfave/cli/issues/1057 func checkBinarySizeActionFunc(c *cli.Context) (err error) { const ( - sourceFilePath = "./internal/example/example.go" - builtFilePath = "./internal/example/built-example" + sourceFilePath = "./internal/example/example.go" + builtFilePath = "./internal/example/built-example" + desiredMinBinarySize = 4.7 + desiredMaxBinarySize = 5.0 + badNewsEmoji = "🚨" + goodNewsEmoji = "✨" + checksPassedEmoji = "✅" + mbStringFormatter = "%.1fMB" ) // build example binary @@ -211,10 +217,28 @@ func checkBinarySizeActionFunc(c *cli.Context) (err error) { // that you would see output without the rounding fileSize := fileInfo.Size() roundedFileSize := math.Round(float64(fileSize)/float64(1000000)*10) / 10 - roundedFileSizeString := fmt.Sprintf("%.1fMB", roundedFileSize) + roundedFileSizeString := fmt.Sprintf(mbStringFormatter, roundedFileSize) - // show the file size - fmt.Println(fmt.Sprintf("current binary size is: %s", roundedFileSizeString)) + // check against bounds + isLessThanDesiredMin := roundedFileSize < desiredMinBinarySize + isMoreThanDesiredMax := roundedFileSize > desiredMaxBinarySize + desiredMinSizeString := fmt.Sprintf(mbStringFormatter, desiredMinBinarySize) + desiredMaxSizeString := fmt.Sprintf(mbStringFormatter, desiredMaxBinarySize) + + // show guidance + fmt.Println(fmt.Sprintf("\n%s is the current binary size", roundedFileSizeString)) + if isLessThanDesiredMin { + fmt.Println(fmt.Sprintf(" %s current binary size is %s", goodNewsEmoji, desiredMinSizeString)) + os.Exit(1) + } else { + fmt.Println(fmt.Sprintf(" %s %s is the target minium size", checksPassedEmoji, desiredMinSizeString)) + } + if isMoreThanDesiredMax { + fmt.Println(fmt.Sprintf(" %s current binary size is %s", badNewsEmoji, desiredMaxSizeString)) + os.Exit(1) + } else { + fmt.Println(fmt.Sprintf(" %s %s is the target maximum size", checksPassedEmoji, desiredMaxSizeString)) + } return nil } From 365dd2b4cba871557967f72054fc3c711d4a9864 Mon Sep 17 00:00:00 2001 From: Lynn Date: Sat, 29 Feb 2020 00:25:44 -0800 Subject: [PATCH 14/27] test ci --- internal/build/build.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/build/build.go b/internal/build/build.go index 6013549..fe0211a 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -191,7 +191,7 @@ func checkBinarySizeActionFunc(c *cli.Context) (err error) { const ( sourceFilePath = "./internal/example/example.go" builtFilePath = "./internal/example/built-example" - desiredMinBinarySize = 4.7 + desiredMinBinarySize = 4.8 desiredMaxBinarySize = 5.0 badNewsEmoji = "🚨" goodNewsEmoji = "✨" From 055c9c076d5ef676b9ba266a28c8fd0f1dd1bec5 Mon Sep 17 00:00:00 2001 From: Lynn Date: Sat, 29 Feb 2020 00:33:51 -0800 Subject: [PATCH 15/27] big guidance --- internal/build/build.go | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/internal/build/build.go b/internal/build/build.go index fe0211a..b7a05ef 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -227,17 +227,28 @@ func checkBinarySizeActionFunc(c *cli.Context) (err error) { // show guidance fmt.Println(fmt.Sprintf("\n%s is the current binary size", roundedFileSizeString)) + // show guidance for min size if isLessThanDesiredMin { - fmt.Println(fmt.Sprintf(" %s current binary size is %s", goodNewsEmoji, desiredMinSizeString)) + fmt.Println(fmt.Sprintf(" %s %s is the target min size", goodNewsEmoji, desiredMinSizeString)) + fmt.Println("") // visual spacing + fmt.Println(" The binary is smaller than the target min size, which is great news!") + fmt.Println(" That means that whatever you've done is shrinking the binary size.") + fmt.Println(" You'll want to go into ./internal/build/build.go and decrease") + fmt.Println(" the desiredMinBinarySize, and also probably decrease the ") + fmt.Println(" desiredMaxBinarySize by the same amount. That will ensure that") + fmt.Println(" future PRs will enforce the newly shrunk binary sizes.") + fmt.Println("") // visual spacing os.Exit(1) } else { - fmt.Println(fmt.Sprintf(" %s %s is the target minium size", checksPassedEmoji, desiredMinSizeString)) + fmt.Println(fmt.Sprintf(" %s %s is the target min size", checksPassedEmoji, desiredMinSizeString)) } + // show guidance for max size if isMoreThanDesiredMax { - fmt.Println(fmt.Sprintf(" %s current binary size is %s", badNewsEmoji, desiredMaxSizeString)) + fmt.Println(fmt.Sprintf(" %s %s is the target max size", badNewsEmoji, desiredMaxSizeString)) + fmt.Println("") // visual spacing os.Exit(1) } else { - fmt.Println(fmt.Sprintf(" %s %s is the target maximum size", checksPassedEmoji, desiredMaxSizeString)) + fmt.Println(fmt.Sprintf(" %s %s is the target max size", checksPassedEmoji, desiredMaxSizeString)) } return nil From 0ec96bc0647b021e6f5a0adc3a539e5b3d60a5b3 Mon Sep 17 00:00:00 2001 From: Lynn Date: Sat, 29 Feb 2020 00:35:46 -0800 Subject: [PATCH 16/27] decrease min --- internal/build/build.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/build/build.go b/internal/build/build.go index b7a05ef..eabf754 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -191,7 +191,7 @@ func checkBinarySizeActionFunc(c *cli.Context) (err error) { const ( sourceFilePath = "./internal/example/example.go" builtFilePath = "./internal/example/built-example" - desiredMinBinarySize = 4.8 + desiredMinBinarySize = 4.7 desiredMaxBinarySize = 5.0 badNewsEmoji = "🚨" goodNewsEmoji = "✨" From b1977c242ddf344733979822d7b54af3e617e404 Mon Sep 17 00:00:00 2001 From: Lynn Date: Sat, 29 Feb 2020 00:39:47 -0800 Subject: [PATCH 17/27] clarify MB --- internal/build/build.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/internal/build/build.go b/internal/build/build.go index eabf754..feb4c24 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -216,7 +216,8 @@ func checkBinarySizeActionFunc(c *cli.Context) (err error) { // that output is much easier to reason about than the `35223432` // that you would see output without the rounding fileSize := fileInfo.Size() - roundedFileSize := math.Round(float64(fileSize)/float64(1000000)*10) / 10 + fileSizeInMB := float64(fileSize) / float64(1000000) + roundedFileSize := math.Round(fileSizeInMB*10) / 10 roundedFileSizeString := fmt.Sprintf(mbStringFormatter, roundedFileSize) // check against bounds From 6594da85f696a44e14f84f19579a98e26d33314e Mon Sep 17 00:00:00 2001 From: Lynn Date: Sat, 29 Feb 2020 00:47:10 -0800 Subject: [PATCH 18/27] max guidance --- internal/build/build.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/internal/build/build.go b/internal/build/build.go index feb4c24..a2c2180 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -191,7 +191,7 @@ func checkBinarySizeActionFunc(c *cli.Context) (err error) { const ( sourceFilePath = "./internal/example/example.go" builtFilePath = "./internal/example/built-example" - desiredMinBinarySize = 4.7 + desiredMinBinarySize = 4.5 desiredMaxBinarySize = 5.0 badNewsEmoji = "🚨" goodNewsEmoji = "✨" @@ -233,7 +233,7 @@ func checkBinarySizeActionFunc(c *cli.Context) (err error) { fmt.Println(fmt.Sprintf(" %s %s is the target min size", goodNewsEmoji, desiredMinSizeString)) fmt.Println("") // visual spacing fmt.Println(" The binary is smaller than the target min size, which is great news!") - fmt.Println(" That means that whatever you've done is shrinking the binary size.") + fmt.Println(" That means that your changes are shrinking the binary size.") fmt.Println(" You'll want to go into ./internal/build/build.go and decrease") fmt.Println(" the desiredMinBinarySize, and also probably decrease the ") fmt.Println(" desiredMaxBinarySize by the same amount. That will ensure that") @@ -247,6 +247,16 @@ func checkBinarySizeActionFunc(c *cli.Context) (err error) { if isMoreThanDesiredMax { fmt.Println(fmt.Sprintf(" %s %s is the target max size", badNewsEmoji, desiredMaxSizeString)) fmt.Println("") // visual spacing + fmt.Println(" The binary is larger than the target max size.") + fmt.Println(" That means that your changes are increasing the binary size.") + fmt.Println(" The first thing you'll want to do is ask your yourself") + fmt.Println(" Is this change worth increasing the binary size?") + fmt.Println(" Larger binary sizes for this package can dissuade its use.") + fmt.Println(" If this change is worth the increase, then we can up the") + fmt.Println(" desired max binary size. To do that you'll want to go into") + fmt.Println(" ./internal/build/build.go and increase the desiredMaxBinarySize,") + fmt.Println(" and increase the desiredMinBinarySize by the same amount.") + fmt.Println("") // visual spacing os.Exit(1) } else { fmt.Println(fmt.Sprintf(" %s %s is the target max size", checksPassedEmoji, desiredMaxSizeString)) From d2060201794a583d7538b826d52aa697449d6898 Mon Sep 17 00:00:00 2001 From: "lynn [they]" Date: Sat, 29 Feb 2020 13:21:31 -0800 Subject: [PATCH 19/27] Update internal/build/build.go Co-Authored-By: Sascha Grunert --- internal/build/build.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/build/build.go b/internal/build/build.go index a2c2180..6246ee4 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -205,7 +205,7 @@ func checkBinarySizeActionFunc(c *cli.Context) (err error) { return err } - // get file into + // get file info fileInfo, err := os.Stat(builtFilePath) if err != nil { return err From 6ffee22f36f1edde55fca99ded1b53e27376e906 Mon Sep 17 00:00:00 2001 From: "lynn [they]" Date: Sat, 29 Feb 2020 13:26:25 -0800 Subject: [PATCH 20/27] add linker flags --- internal/build/build.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/build/build.go b/internal/build/build.go index 6246ee4..b75945a 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -200,7 +200,7 @@ func checkBinarySizeActionFunc(c *cli.Context) (err error) { ) // build example binary - err = runCmd("go", "build", "-o", builtFilePath, sourceFilePath) + err = runCmd("go", "build", "-ldflags=\"-s -w\"", "-o", builtFilePath, sourceFilePath) if err != nil { return err } From a25f82805f67e08b4c3394a1cbdd6448bf7461c9 Mon Sep 17 00:00:00 2001 From: Lynn Date: Sat, 29 Feb 2020 13:35:43 -0800 Subject: [PATCH 21/27] fix linker flags --- internal/build/build.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/build/build.go b/internal/build/build.go index b75945a..9ee4184 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -191,8 +191,8 @@ func checkBinarySizeActionFunc(c *cli.Context) (err error) { const ( sourceFilePath = "./internal/example/example.go" builtFilePath = "./internal/example/built-example" - desiredMinBinarySize = 4.5 - desiredMaxBinarySize = 5.0 + desiredMinBinarySize = 3.5 + desiredMaxBinarySize = 4.0 badNewsEmoji = "🚨" goodNewsEmoji = "✨" checksPassedEmoji = "✅" @@ -200,7 +200,7 @@ func checkBinarySizeActionFunc(c *cli.Context) (err error) { ) // build example binary - err = runCmd("go", "build", "-ldflags=\"-s -w\"", "-o", builtFilePath, sourceFilePath) + err = runCmd("go", "build", "-o", builtFilePath, "-ldflags", "-s -w", sourceFilePath) if err != nil { return err } From 741984cad42d6196c00d6326f33b446881a70bfe Mon Sep 17 00:00:00 2001 From: Lynn Date: Sat, 29 Feb 2020 13:37:06 -0800 Subject: [PATCH 22/27] move bounds --- internal/build/build.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/build/build.go b/internal/build/build.go index 9ee4184..0660e8b 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -191,8 +191,8 @@ func checkBinarySizeActionFunc(c *cli.Context) (err error) { const ( sourceFilePath = "./internal/example/example.go" builtFilePath = "./internal/example/built-example" - desiredMinBinarySize = 3.5 - desiredMaxBinarySize = 4.0 + desiredMinBinarySize = 3.2 + desiredMaxBinarySize = 3.8 badNewsEmoji = "🚨" goodNewsEmoji = "✨" checksPassedEmoji = "✅" From a0932dafcbb00cce920acd01bcb7ecad6a47d5d1 Mon Sep 17 00:00:00 2001 From: Lynn Date: Sat, 29 Feb 2020 13:41:43 -0800 Subject: [PATCH 23/27] move bounds --- internal/build/build.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/build/build.go b/internal/build/build.go index 0660e8b..7ccb18a 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -191,7 +191,7 @@ func checkBinarySizeActionFunc(c *cli.Context) (err error) { const ( sourceFilePath = "./internal/example/example.go" builtFilePath = "./internal/example/built-example" - desiredMinBinarySize = 3.2 + desiredMinBinarySize = 3.3 desiredMaxBinarySize = 3.8 badNewsEmoji = "🚨" goodNewsEmoji = "✨" From 3558c7f30c04e360c4ec46b9de7c571bd76f0048 Mon Sep 17 00:00:00 2001 From: Lynn Date: Sat, 29 Feb 2020 14:00:26 -0800 Subject: [PATCH 24/27] add diffing --- .gitignore | 2 +- internal/build/build.go | 46 +++++++++++++++---- .../example.go => example-cli/example-cli.go} | 0 .../example-hello-world.go | 7 +++ 4 files changed, 44 insertions(+), 11 deletions(-) rename internal/{example/example.go => example-cli/example-cli.go} (100%) create mode 100644 internal/example-hello-world/example-hello-world.go diff --git a/.gitignore b/.gitignore index a660143..2d5e149 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,5 @@ node_modules/ vendor .idea -internal/example/built-example +internal/*/built-example coverage.txt diff --git a/internal/build/build.go b/internal/build/build.go index 7ccb18a..a4e970e 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -189,34 +189,39 @@ func TocActionFunc(c *cli.Context) error { // of https://github.com/urfave/cli/issues/1057 func checkBinarySizeActionFunc(c *cli.Context) (err error) { const ( - sourceFilePath = "./internal/example/example.go" - builtFilePath = "./internal/example/built-example" - desiredMinBinarySize = 3.3 - desiredMaxBinarySize = 3.8 + cliSourceFilePath = "./internal/example-cli/example-cli.go" + cliBuiltFilePath = "./internal/example-cli/built-example" + helloSourceFilePath = "./internal/example-hello-world/example-hello-world.go" + helloBuiltFilePath = "./internal/example-hello-world/built-example" + desiredMinBinarySize = 2.1 + desiredMaxBinarySize = 2.6 badNewsEmoji = "🚨" goodNewsEmoji = "✨" checksPassedEmoji = "✅" mbStringFormatter = "%.1fMB" ) - // build example binary - err = runCmd("go", "build", "-o", builtFilePath, "-ldflags", "-s -w", sourceFilePath) + // get cli example size + cliSize, err := getSize(cliSourceFilePath, cliBuiltFilePath) if err != nil { return err } - // get file info - fileInfo, err := os.Stat(builtFilePath) + // get hello world size + helloSize, err := getSize(helloSourceFilePath, helloBuiltFilePath) if err != nil { return err } + // The CLI size diff is the number we are interested in. + // This tells us how much our CLI package contributes to the binary size. + cliSizeDiff := cliSize - helloSize + // get human readable size, in MB with one decimal place. // example output is: 35.2MB. (note: this simply an example) // that output is much easier to reason about than the `35223432` // that you would see output without the rounding - fileSize := fileInfo.Size() - fileSizeInMB := float64(fileSize) / float64(1000000) + fileSizeInMB := float64(cliSizeDiff) / float64(1000000) roundedFileSize := math.Round(fileSizeInMB*10) / 10 roundedFileSizeString := fmt.Sprintf(mbStringFormatter, roundedFileSize) @@ -264,3 +269,24 @@ func checkBinarySizeActionFunc(c *cli.Context) (err error) { return nil } + +func getSize(sourcePath string, builtPath string) (size int64, err error) { + // build example binary + err = runCmd("go", "build", "-o", builtPath, "-ldflags", "-s -w", sourcePath) + if err != nil { + fmt.Println("issue getting size for example binary") + return 0, err + } + + // get file info + fileInfo, err := os.Stat(builtPath) + if err != nil { + fmt.Println("issue getting size for example binary") + return 0, err + } + + // size! + size = fileInfo.Size() + + return size, nil +} diff --git a/internal/example/example.go b/internal/example-cli/example-cli.go similarity index 100% rename from internal/example/example.go rename to internal/example-cli/example-cli.go diff --git a/internal/example-hello-world/example-hello-world.go b/internal/example-hello-world/example-hello-world.go new file mode 100644 index 0000000..c048119 --- /dev/null +++ b/internal/example-hello-world/example-hello-world.go @@ -0,0 +1,7 @@ +package main + +import "fmt" + +func main() { + fmt.Println("hello world") +} From 1d4b01a6fcbc179a88b21feb8f8fefb13f8db7e6 Mon Sep 17 00:00:00 2001 From: Lynn Date: Sat, 29 Feb 2020 14:02:03 -0800 Subject: [PATCH 25/27] move bounds --- internal/build/build.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/build/build.go b/internal/build/build.go index a4e970e..6edc496 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -193,8 +193,8 @@ func checkBinarySizeActionFunc(c *cli.Context) (err error) { cliBuiltFilePath = "./internal/example-cli/built-example" helloSourceFilePath = "./internal/example-hello-world/example-hello-world.go" helloBuiltFilePath = "./internal/example-hello-world/built-example" - desiredMinBinarySize = 2.1 - desiredMaxBinarySize = 2.6 + desiredMinBinarySize = 2.0 + desiredMaxBinarySize = 2.3 badNewsEmoji = "🚨" goodNewsEmoji = "✨" checksPassedEmoji = "✅" From bb4ec3bda27d3c801382d45d5fc048561307071a Mon Sep 17 00:00:00 2001 From: Lynn Date: Sat, 29 Feb 2020 14:03:02 -0800 Subject: [PATCH 26/27] docs --- internal/example-hello-world/example-hello-world.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/internal/example-hello-world/example-hello-world.go b/internal/example-hello-world/example-hello-world.go index c048119..746e385 100644 --- a/internal/example-hello-world/example-hello-world.go +++ b/internal/example-hello-world/example-hello-world.go @@ -1,3 +1,5 @@ +// example hello world used for binary size checking + package main import "fmt" From a8a1ef04eea0e80585590153eb3f04dcb444285f Mon Sep 17 00:00:00 2001 From: Lynn Date: Sat, 29 Feb 2020 14:04:21 -0800 Subject: [PATCH 27/27] move bounds --- internal/build/build.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/build/build.go b/internal/build/build.go index 6edc496..197cfa5 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -194,7 +194,7 @@ func checkBinarySizeActionFunc(c *cli.Context) (err error) { helloSourceFilePath = "./internal/example-hello-world/example-hello-world.go" helloBuiltFilePath = "./internal/example-hello-world/built-example" desiredMinBinarySize = 2.0 - desiredMaxBinarySize = 2.3 + desiredMaxBinarySize = 2.1 badNewsEmoji = "🚨" goodNewsEmoji = "✨" checksPassedEmoji = "✅"