-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
166 lines (135 loc) · 3.67 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
package main
import (
"flag"
"go/parser"
"go/token"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/Masterminds/glide/action"
"github.com/Masterminds/glide/cfg"
"github.com/Masterminds/glide/msg"
gpath "github.com/Masterminds/glide/path"
)
var (
glideYaml = gpath.DefaultGlideFile
argDebug = false
argVerbose = false
argQuiet = false
)
func init() {
flag.StringVar(&glideYaml, "yaml", gpath.DefaultGlideFile, "Set a YAML configuration file")
flag.BoolVar(&argDebug, "debug", false, "Print debug verbose informational messages")
flag.BoolVar(&argQuiet, "quiet", false, "Quiet (no info or debug messages)")
}
func main() {
flag.Parse()
action.Debug(argDebug)
action.Quiet(argQuiet)
gpath.GlideFile = glideYaml
// load package from glide.yml config
msg.Debug("Loading Glide config from %s...", glideYaml)
glideConfig := loadGlideConfig()
msg.Debug("Collecting imported packages...")
importPkgs := make(map[string]interface{})
err := filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
if path == "vendor" {
return filepath.SkipDir
}
if path == "Godeps" {
return filepath.SkipDir
}
if path != "." && strings.HasPrefix(path, ".") {
return filepath.SkipDir
}
return nil
}
if !strings.HasSuffix(path, ".go") {
return nil
}
msg.Debug("--> getting import package in %q", path)
pkgs, err := getImports(path)
if err == nil {
for _, pkg := range pkgs {
importPkgs[pkg] = nil
}
} else {
msg.Err("Error when get import package for file %v: %v", path, err)
}
return nil
})
if err != nil {
msg.Die(err.Error())
}
msg.Debug("Checking unused packages...")
unusedPkgs := make(map[string]interface{})
gi:
for _, dep := range glideConfig.Imports {
if _, found := importPkgs[dep.Name]; found {
delete(importPkgs, dep.Name)
continue
}
// todo: if subpackages is defined, need to check it all
for pkg := range importPkgs {
if strings.HasPrefix(pkg, dep.Name) {
continue gi
}
}
msg.Debug("--> package %q is not used", dep.Name)
unusedPkgs[dep.Name] = nil
}
if len(unusedPkgs) == 0 {
msg.Info("Well done! All packages are needed.")
os.Exit(0)
}
msg.Debug("Removing unused packages...")
deps := make([]*cfg.Dependency, 0, len(glideConfig.Imports))
for _, pkg := range glideConfig.Imports {
if _, unused := unusedPkgs[pkg.Name]; !unused {
deps = append(deps, pkg)
}
}
glideConfig.Imports = deps
glideYamlFile, _ := gpath.Glide()
if err = glideConfig.WriteFile(glideYamlFile); err != nil {
msg.Die("Error while write Glide config to file back: %v", err)
}
msg.Info("New Glide config has been updated with removing unused packages.")
}
func loadGlideConfig() (config *cfg.Config) {
glideYamlFile, err := gpath.Glide()
if err != nil {
msg.Die("Could not find Glide config file")
}
var yml []byte
if yml, err = ioutil.ReadFile(glideYamlFile); err != nil {
msg.Die("Error while reading config file: %v", err)
}
if config, err = cfg.ConfigFromYaml(yml); err != nil {
msg.Die("Error while parsing config file: %v", err)
}
return config
}
func getImports(file string) ([]string, error) {
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, file, nil, parser.ImportsOnly)
if err != nil {
return nil, err
}
if f.Imports == nil || len(f.Imports) == 0 {
return []string{}, nil
}
pkgs := make([]string, 0, len(f.Imports))
for _, importSpec := range f.Imports {
if importSpec.Path == nil {
continue
}
// todo: check if package is a Go built-in package
// todo: check if package is a sub-package of checking package
pkg := strings.Trim(importSpec.Path.Value, `"`)
pkgs = append(pkgs, pkg)
}
return pkgs, nil
}