-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
171 lines (144 loc) · 3.12 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"errors"
"fmt"
"go/build"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strings"
"github.com/coreos/go-semver/semver"
"github.com/dustin/go-humanize"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var (
version = "dev"
)
type depSize struct {
name string
size int64
}
func getDepSize(path string) (int64, error) {
var size int64
if fi, err := os.Stat(path); err == nil {
if !fi.IsDir() {
return 0, errors.New("not a directory")
}
}
err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
size += info.Size()
}
return err
})
return size, err
}
func parseGoSum(contents string) []string {
deps := map[string]string{}
var result []string
lines := strings.Split(contents, "\n")
for _, line := range lines {
parts := strings.Split(line, " ")
if len(parts) < 2 {
continue
}
name := parts[0]
version := parts[1][1:]
// I have no idea what this means in a go.sum file...
if strings.HasSuffix(version, "go.mod") {
continue
}
v, err := semver.NewVersion(version)
if err != nil {
logrus.Warn(err)
continue
}
if dv, ok := deps[name]; ok {
v1, err := semver.NewVersion(dv)
if err != nil {
logrus.Warn(err)
continue
}
if v1.LessThan(*v) {
deps[name] = version
}
} else {
deps[name] = version
}
}
for k, v := range deps {
result = append(result, k+"@v"+v)
}
return result
}
func runDepSum(opts rootOpts, path string) error {
f := filepath.Join(path, "go.sum")
buf, err := ioutil.ReadFile(f)
if err != nil {
return err
}
deps := parseGoSum(string(buf))
var total int64
depsizes := []depSize{}
gopath := os.Getenv("GOPATH")
if gopath == "" {
gopath = build.Default.GOPATH
}
for _, dep := range deps {
size, err := getDepSize(filepath.Join(gopath, "pkg", "mod", dep))
if err == nil {
depsizes = append(depsizes, depSize{
name: dep,
size: size,
})
total += size
}
}
sort.Slice(depsizes, func(i, j int) bool {
if opts.reverse {
return depsizes[i].size > depsizes[j].size
}
return depsizes[i].size <= depsizes[j].size
})
if opts.quiet {
fmt.Println(total)
return nil
}
for _, d := range depsizes {
fmt.Printf("%s\t%s\n", humanize.Bytes(uint64(d.size)), d.name)
}
fmt.Printf("\nTotal dependencies size: %s\n", humanize.Bytes(uint64(total)))
return nil
}
type rootOpts struct {
reverse bool
quiet bool
verbose bool
}
func main() {
var opts rootOpts
root := cobra.Command{
Use: "dep-sum",
Version: version,
Args: cobra.ExactArgs(1),
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if opts.verbose {
logrus.SetLevel(logrus.DebugLevel)
}
},
RunE: func(cmd *cobra.Command, args []string) error {
return runDepSum(opts, args[0])
},
}
root.Flags().BoolVarP(&opts.reverse, "reverse", "r", false, "Sort in reverse order")
root.Flags().BoolVarP(&opts.quiet, "quiet", "q", false, "Only print the total size in bytes")
root.Flags().BoolVarP(&opts.verbose, "verbose", "v", false, "Verbose output")
if err := root.Execute(); err != nil {
logrus.Fatal(err)
}
}