-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
143 lines (117 loc) · 3.4 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
package main
import (
"github.com/dustin/go-humanize"
"github.com/spectronp/sizr/data"
"github.com/spectronp/sizr/types"
"fmt"
"os"
"sort"
"github.com/spf13/pflag"
)
var (
VERSION string
HELP_MESSAGE string
ENV string
)
func calcSize(pack string, dataObj *data.Data) uint {
target := dataObj.GetPackage(pack)
explictPackages := dataObj.GetExplicit()
delete(explictPackages, pack)
depsToIgnore := make(map[string]bool)
for _, explicitPack := range explictPackages {
listTree(explicitPack, depsToIgnore, dataObj)
}
alreadyCounted := map[string]bool{}
return sumSize(target, depsToIgnore, alreadyCounted, dataObj)
}
func listTree(target types.Package, depsToIgnore map[string]bool, dataObj *data.Data) {
for _, packName := range target.Deps {
pack := dataObj.GetPackage(packName)
if depsToIgnore[packName] {
continue
}
depsToIgnore[pack.Name] = true
listTree(pack, depsToIgnore, dataObj)
}
}
func sumSize(start types.Package, ignorePackages map[string]bool, alreadyCounted map[string]bool, dataObj *data.Data) uint {
totalSize := start.Size
for _, packName := range start.Deps {
if ignorePackages[packName] {
continue
}
if alreadyCounted[packName] {
continue
}
pack := dataObj.GetPackage(packName)
alreadyCounted[packName] = true
totalSize += sumSize(pack, ignorePackages, alreadyCounted, dataObj)
}
return totalSize
}
type PackageNameWithSum struct {
Name string
Size uint
}
func orderBySumSize(dataObj *data.Data) []PackageNameWithSum {
explicitPackages := dataObj.GetExplicit()
orderedPacks := []PackageNameWithSum{}
for _, pack := range explicitPackages {
packSize := calcSize(pack.Name, dataObj)
insertIndex := sort.Search(len(orderedPacks), func(i int) bool { return orderedPacks[i].Size <= packSize })
if insertIndex == len(orderedPacks) {
orderedPacks = append(orderedPacks, PackageNameWithSum{Name: pack.Name, Size: packSize})
continue
}
orderedPacks = append(orderedPacks[:insertIndex+1], orderedPacks[insertIndex:]...)
orderedPacks[insertIndex] = PackageNameWithSum{Name: pack.Name, Size: packSize}
}
return orderedPacks
}
func report(packages []PackageNameWithSum, limit uint8) {
var limitedPackages []PackageNameWithSum
if uint8(len(packages)) > limit {
limitedPackages = packages[:limit]
} else {
limitedPackages = packages
}
longestPackageName := 0
for _, pack := range limitedPackages {
runeCount := len([]rune(pack.Name))
if runeCount > longestPackageName {
longestPackageName = runeCount
}
}
for _, pack := range limitedPackages {
gapNum := 5 + (longestPackageName - len([]rune(pack.Name)))
gapString := ""
for i := gapNum; i > 0; i-- {
gapString += " "
}
fmt.Printf("%s%s%s\n", pack.Name, gapString, humanize.IBytes(uint64(pack.Size)))
}
}
func Run(args []string) int {
ENV = os.Getenv("SIZR_ENV")
flag := pflag.NewFlagSet("", 1)
helpWanted := flag.BoolP("help", "h", false, "Show this help message")
wantVersion := flag.BoolP("version", "v", false, "Show sizr version")
reportLimit := flag.Uint8P("limit", "n", 30, "Set the limit of packages to show (Default: 30)")
flag.Parse(args)
if *helpWanted {
fmt.Print(HELP_MESSAGE)
return 0
}
if *wantVersion {
fmt.Printf("sizr %s\n", VERSION)
return 0
}
dataObject, _ := data.NewData(data.RunScript)
// CLI or TUI
report(orderBySumSize(&dataObject), *reportLimit)
return 0
}
func main() {
statusCode := Run(os.Args)
os.Exit(statusCode)
}