From a33bebe55fea79a860496e1c412973ce70bd46c6 Mon Sep 17 00:00:00 2001 From: Alexander Bruyako Date: Mon, 2 Nov 2020 19:11:40 +0300 Subject: [PATCH 1/8] replace kingpin with std/flag that change reduces binary size 5848 B -> 3292 B --- main.go | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/main.go b/main.go index d117fac..a86a618 100644 --- a/main.go +++ b/main.go @@ -2,11 +2,10 @@ package main import ( "encoding/json" + "flag" "fmt" "github.com/jondot/goweight/pkg" - - kingpin "gopkg.in/alecthomas/kingpin.v2" ) var ( @@ -16,14 +15,22 @@ var ( ) var ( - jsonOutput = kingpin.Flag("json", "Output json").Short('j').Bool() - buildTags = kingpin.Flag("tags", "Build tags").String() - packages = kingpin.Arg("packages", "Packages to build").String() + jsonOutput = flag.Bool("json", false, "Output json") + jsonOutputShort = flag.Bool("j", false, "Output json") + + buildTags = flag.String("tags", "", "Build tags") + packages = flag.String("packages", "", "Packages to build") + + showVersion = flag.Bool("version", false, "Shows version") ) func main() { - kingpin.Version(fmt.Sprintf("%s (%s)", version, commit)) - kingpin.Parse() + flag.Parse() + if *showVersion { + fmt.Printf("%s (%s)\n", version, commit) + return + } + weight := pkg.NewGoWeight() if *buildTags != "" { weight.BuildCmd = append(weight.BuildCmd, "-tags", *buildTags) @@ -35,7 +42,7 @@ func main() { work := weight.BuildCurrent() modules := weight.Process(work) - if *jsonOutput { + if *jsonOutput || *jsonOutputShort { m, _ := json.Marshal(modules) fmt.Print(string(m)) } else { From 679dc1639e03a6f975bf18f2c792c12cde42c33a Mon Sep 17 00:00:00 2001 From: Alexander Bruyako Date: Mon, 2 Nov 2020 19:13:48 +0300 Subject: [PATCH 2/8] go mod tidy --- go.mod | 4 ---- go.sum | 8 -------- 2 files changed, 12 deletions(-) diff --git a/go.mod b/go.mod index c2fde6a..12db72c 100644 --- a/go.mod +++ b/go.mod @@ -1,14 +1,10 @@ module github.com/jondot/goweight require ( - github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc // indirect - github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/dustin/go-humanize v0.0.0-20180713052910-9f541cc9db5d github.com/mattn/go-zglob v0.0.0-20180803001819-2ea3427bfa53 github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/spf13/pflag v1.0.2 github.com/stretchr/testify v1.2.2 // indirect github.com/thoas/go-funk v0.0.0-20180716193722-1060394a7713 - gopkg.in/alecthomas/kingpin.v2 v2.2.6 ) diff --git a/go.sum b/go.sum index 61afa7c..6a39872 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,3 @@ -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/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 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= @@ -10,11 +6,7 @@ github.com/mattn/go-zglob v0.0.0-20180803001819-2ea3427bfa53 h1:tGfIHhDghvEnneeR 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/spf13/pflag v1.0.2 h1:Fy0orTDgHdbnzHcsOgfCN4LtHf0ec3wwtiwJqwvf3Gc= -github.com/spf13/pflag v1.0.2/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= 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= From 0ab872b13a488edc943eb0cee66cdf0198a626ea Mon Sep 17 00:00:00 2001 From: Alexander Bruyako Date: Mon, 2 Nov 2020 19:52:45 +0300 Subject: [PATCH 3/8] replace funk with simple for loops --- pkg/goweight.go | 56 ++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 48 insertions(+), 8 deletions(-) diff --git a/pkg/goweight.go b/pkg/goweight.go index d9bfe25..296dc62 100644 --- a/pkg/goweight.go +++ b/pkg/goweight.go @@ -11,7 +11,6 @@ import ( "github.com/dustin/go-humanize" "github.com/mattn/go-zglob" - "github.com/thoas/go-funk" ) var moduleRegex = regexp.MustCompile("packagefile (.*)=(.*)") @@ -70,15 +69,56 @@ func (g *GoWeight) Process(work string) []*ModuleEntry { log.Fatal(err) } - allLines := funk.Uniq(funk.FlattenDeep(funk.Map(files, func(file string) []string { - f, err := ioutil.ReadFile(file) - if err != nil { - return []string{} + allLines := uniqLines(flattenSLices(filesLines(files))) + var modules []*ModuleEntry + for _, line := range allLines { + module := processModule(line) + if module == nil { + continue } - return strings.Split(string(f), "\n") - }))) - modules := funk.Compact(funk.Map(allLines, processModule)).([]*ModuleEntry) + modules = append(modules, module) + } sort.Slice(modules, func(i, j int) bool { return modules[i].Size > modules[j].Size }) return modules } + +func uniqLines(lines []string) []string { + m := make(map[string]struct{}) + + var uniqLines []string + + for _, line := range lines { + _, seen := m[line] + if !seen { + uniqLines = append(uniqLines, line) + m[line] = struct{}{} + } + } + + return uniqLines +} + +func flattenSLices(slice [][]string) []string { + var flatten []string + for _, s := range slice { + flatten = append(flatten, s...) + } + return flatten +} + +func filesLines(files []string) [][]string { + var lines [][]string + for _, f := range files { + lines = append(lines, fileLines(f)) + } + return lines +} + +func fileLines(file string) []string { + f, err := ioutil.ReadFile(file) + if err != nil { + return []string{} + } + return strings.Split(string(f), "\n") +} From 68039c0f1e1f54b43a48c7a72fc1e7ac2b2f99c2 Mon Sep 17 00:00:00 2001 From: Alexander Bruyako Date: Mon, 2 Nov 2020 19:53:32 +0300 Subject: [PATCH 4/8] go mod tidy --- go.mod | 4 ---- go.sum | 8 -------- 2 files changed, 12 deletions(-) diff --git a/go.mod b/go.mod index 12db72c..97cb024 100644 --- a/go.mod +++ b/go.mod @@ -1,10 +1,6 @@ module github.com/jondot/goweight require ( - github.com/davecgh/go-spew v1.1.1 // indirect github.com/dustin/go-humanize v0.0.0-20180713052910-9f541cc9db5d github.com/mattn/go-zglob v0.0.0-20180803001819-2ea3427bfa53 - github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/stretchr/testify v1.2.2 // indirect - github.com/thoas/go-funk v0.0.0-20180716193722-1060394a7713 ) diff --git a/go.sum b/go.sum index 6a39872..de9a002 100644 --- a/go.sum +++ b/go.sum @@ -1,12 +1,4 @@ -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -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/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/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= -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= From bd6d823f34081fbde02f6c083a23f48b24113d8a Mon Sep 17 00:00:00 2001 From: Alexander Bruyako Date: Mon, 2 Nov 2020 19:59:50 +0300 Subject: [PATCH 5/8] replace big package with small function --- pkg/goweight.go | 2 +- pkg/humanize/humanize.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 pkg/humanize/humanize.go diff --git a/pkg/goweight.go b/pkg/goweight.go index 296dc62..20333db 100644 --- a/pkg/goweight.go +++ b/pkg/goweight.go @@ -9,7 +9,7 @@ import ( "sort" "strings" - "github.com/dustin/go-humanize" + "github.com/jondot/goweight/pkg/humanize" "github.com/mattn/go-zglob" ) diff --git a/pkg/humanize/humanize.go b/pkg/humanize/humanize.go new file mode 100644 index 0000000..0c9fe43 --- /dev/null +++ b/pkg/humanize/humanize.go @@ -0,0 +1,35 @@ +package humanize + +import ( + "fmt" + "math" +) + +func logn(n, b float64) float64 { + return math.Log(n) / math.Log(b) +} + +func humanateBytes(s uint64, base float64, sizes []string) string { + if s < 10 { + return fmt.Sprintf("%d B", s) + } + e := math.Floor(logn(float64(s), base)) + suffix := sizes[int(e)] + val := math.Floor(float64(s)/math.Pow(base, e)*10+0.5) / 10 + f := "%.0f %s" + if val < 10 { + f = "%.1f %s" + } + + return fmt.Sprintf(f, val, suffix) +} + +// Bytes produces a human readable representation of an SI size. +// +// See also: ParseBytes. +// +// Bytes(82854982) -> 83 MB +func Bytes(s uint64) string { + sizes := []string{"B", "kB", "MB", "GB", "TB", "PB", "EB"} + return humanateBytes(s, 1000, sizes) +} From db1cf8133520c4817b3d6edecf46e6dcec3774d6 Mon Sep 17 00:00:00 2001 From: Alexander Bruyako Date: Mon, 2 Nov 2020 20:01:06 +0300 Subject: [PATCH 6/8] go mod tidy --- go.mod | 5 +---- go.sum | 2 -- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/go.mod b/go.mod index 97cb024..711f1d2 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,3 @@ module github.com/jondot/goweight -require ( - github.com/dustin/go-humanize v0.0.0-20180713052910-9f541cc9db5d - github.com/mattn/go-zglob v0.0.0-20180803001819-2ea3427bfa53 -) +require github.com/mattn/go-zglob v0.0.0-20180803001819-2ea3427bfa53 diff --git a/go.sum b/go.sum index de9a002..84e8fe1 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,2 @@ -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/mattn/go-zglob v0.0.0-20180803001819-2ea3427bfa53 h1:tGfIHhDghvEnneeRhODvGYOt305TPwingKt6p90F4MU= github.com/mattn/go-zglob v0.0.0-20180803001819-2ea3427bfa53/go.mod h1:9fxibJccNxU2cnpIKLRRFA7zX7qhkJIQWBb449FYHOo= From fb9968a6ab9b3a38dfd98d5e683adf39a15b647b Mon Sep 17 00:00:00 2001 From: Alexander Bruyako Date: Mon, 2 Nov 2020 20:16:33 +0300 Subject: [PATCH 7/8] replace regex file iterator with simple match --- pkg/goweight.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/pkg/goweight.go b/pkg/goweight.go index 20333db..1f5ccdd 100644 --- a/pkg/goweight.go +++ b/pkg/goweight.go @@ -5,12 +5,12 @@ import ( "log" "os" "os/exec" + "path/filepath" "regexp" "sort" "strings" "github.com/jondot/goweight/pkg/humanize" - "github.com/mattn/go-zglob" ) var moduleRegex = regexp.MustCompile("packagefile (.*)=(.*)") @@ -63,8 +63,18 @@ func (g *GoWeight) BuildCurrent() string { } func (g *GoWeight) Process(work string) []*ModuleEntry { + var files []string + + err := filepath.Walk(work, func(path string, _ os.FileInfo, err error) error { + if err != nil { + return err + } + if filepath.Base(path) == "importcfg" { + files = append(files, path) + } + return nil + }) - files, err := zglob.Glob(work + "**/importcfg") if err != nil { log.Fatal(err) } From d4083fcce8e594e996b79def5501efb1533bd0df Mon Sep 17 00:00:00 2001 From: Alexander Bruyako Date: Mon, 2 Nov 2020 20:20:16 +0300 Subject: [PATCH 8/8] go mod tidy --- go.mod | 2 -- go.sum | 2 -- 2 files changed, 4 deletions(-) diff --git a/go.mod b/go.mod index 711f1d2..80a1421 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1 @@ module github.com/jondot/goweight - -require github.com/mattn/go-zglob v0.0.0-20180803001819-2ea3427bfa53 diff --git a/go.sum b/go.sum index 84e8fe1..e69de29 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +0,0 @@ -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=