-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
128 lines (102 loc) · 2.83 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
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/cloudfoundry-incubator/candiedyaml"
"github.com/mitchellh/colorstring"
"github.com/contraband/anderson/anderson"
)
type License struct {
Type anderson.LicenseStatus
Name string
}
type Lister interface {
ListDependencies() ([]string, error)
}
func main() {
config, missingConfig := loadConfig()
lister := lister()
classifier := anderson.LicenseClassifier{
Config: config,
}
info("Hold still citizen, scanning dependencies for contraband...")
dependencies, err := lister.ListDependencies()
if err != nil {
fatalf(err.Error())
}
failed := false
classified := map[string]License{}
for _, importPath := range dependencies {
path, err := anderson.LookGopath(importPath)
if err != nil {
fatalf("Could not find %s in your GOPATH...", importPath)
}
licenseType, licenseDeclarationPath, licenseName, err := classifier.Classify(path, importPath)
failed = failed || licenseType.FailsBuild()
containingGopath, err := anderson.ContainingGopath(importPath)
if err != nil {
fatalf("Unable to find containing GOPATH for %s: %s", licenseDeclarationPath, err)
}
relPath, err := filepath.Rel(filepath.Join(containingGopath, "src"), licenseDeclarationPath)
if err != nil {
fatalf("Unable to create relative path for %s: %s", licenseDeclarationPath, err)
}
classified[relPath] = License{
Type: licenseType,
Name: licenseName,
}
}
for relPath, license := range classified {
var message string
var messageLen int
if missingConfig {
message = fmt.Sprintf("[white]%s", license.Name)
messageLen = len(license.Name)
} else {
message = fmt.Sprintf("(%s) [%s]%10s", license.Name, license.Type.Color(), license.Type.Message())
messageLen = len(license.Name) + len("() ") + 9 // length of all messages
}
totalSize := messageLen + len(relPath)
whitespace := " "
if totalSize < 80 {
whitespace = strings.Repeat(" ", 80-totalSize)
}
say(fmt.Sprintf("[white]%s%s%s", relPath, whitespace, message))
}
if failed {
os.Exit(1)
}
}
func loadConfig() (config anderson.Config, missing bool) {
configFile, err := os.Open(".anderson.yml")
if err != nil {
return config, true
}
if err := candiedyaml.NewDecoder(configFile).Decode(&config); err != nil {
fatalf("Looks like your .anderson.yml file is invalid YAML!")
}
return config, false
}
func lister() Lister {
if isStdinPipe() {
return anderson.StdinLister{}
}
return anderson.PackageLister{}
}
func isStdinPipe() bool {
stat, _ := os.Stdin.Stat()
return (stat.Mode() & os.ModeCharDevice) == 0
}
func fatalf(err string, args ...interface{}) {
message := fmt.Sprintf(err, args)
say(fmt.Sprintf("[red]> %s", message))
os.Exit(1)
}
func info(message string) {
say(fmt.Sprintf("[blue]> %s", message))
}
func say(message string) {
fmt.Println(colorstring.Color(message))
}