forked from OpenPeeDeeP/depguard
/
main.go
130 lines (119 loc) · 2.82 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
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"go/parser"
"go/types"
"log"
"os"
"strings"
"text/template"
"github.com/OpenPeeDeeP/depguard"
"github.com/kisielk/gotool"
"golang.org/x/tools/go/loader"
)
const (
whitelistMsg = " was not in the whitelist"
blacklistMsg = " was in the blacklist"
)
var configFile string
func init() {
flag.StringVar(&configFile, "c", ".depguard.json", "Location of the config file")
}
type config struct {
Type string `json:"type"`
Packages []string `json:"packages"`
IncludeGoRoot bool `json:"includeGoRoot"`
InTests []string `json:"inTests"`
listType depguard.ListType
}
func parseConfigFile() (*config, error) {
file, err := os.Open(configFile)
if err != nil {
if os.IsNotExist(err) {
if configFile != ".depguard.json" {
return nil, err
}
return &config{}, nil
}
return nil, err
}
defer file.Close()
var c *config
err = json.NewDecoder(file).Decode(&c)
if err != nil {
return nil, err
}
var found bool
c.listType, found = depguard.StringToListType[strings.ToLower(c.Type)]
if !found {
if c.Type != "" {
return nil, fmt.Errorf("Unsure what list type %s is", c.Type)
}
c.listType = depguard.LTBlacklist
}
return c, nil
}
func main() {
flag.Parse()
config, err := parseConfigFile()
if err != nil {
log.Fatalln(err)
}
conf, prog, err := getConfigAndProgram(config)
if err != nil {
log.Fatalln(err)
}
dg := &depguard.Depguard{
Packages: config.Packages,
IncludeGoRoot: config.IncludeGoRoot,
ListType: config.listType,
TestPackages: config.InTests,
}
issues, err := dg.Run(conf, prog)
if err != nil {
log.Fatalln(err)
}
printIssues(config, issues)
}
func getConfigAndProgram(depguardConf *config) (*loader.Config, *loader.Program, error) {
paths := gotool.ImportPaths(flag.Args())
conf := new(loader.Config)
conf.ParserMode = parser.ImportsOnly
conf.AllowErrors = true
conf.TypeChecker = types.Config{
Error: eatErrors,
}
rest, err := conf.FromArgs(paths, len(depguardConf.InTests) > 0)
if err != nil {
return nil, nil, err
}
if len(rest) > 0 {
return nil, nil, fmt.Errorf("Too many args: %v", rest)
}
prog, err := conf.Load()
if err != nil {
return nil, nil, err
}
return conf, prog, nil
}
func printIssues(c *config, issues []*depguard.Issue) {
if len(issues) == 0 {
return
}
temp := template.Must(template.New("issues").Parse(`{{ .Position.Filename }}:{{ .Position.Line }}:{{ .Position.Column }}:{{ .PackageName }}`))
buf := new(bytes.Buffer)
for _, issue := range issues {
temp.Execute(buf, issue)
if c.listType == depguard.LTWhitelist {
fmt.Println(buf.String() + whitelistMsg)
} else {
fmt.Println(buf.String() + blacklistMsg)
}
buf.Reset()
}
}
// Since I am allowing errors to happen, I don't want them to print to screen.
func eatErrors(err error) {}