forked from contraband/anderson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
119 lines (95 loc) · 2.5 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
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/cloudfoundry-incubator/candiedyaml"
"github.com/mitchellh/colorstring"
"github.com/xoebus/anderson/anderson"
)
type License struct {
Type anderson.LicenseStatus
Name string
}
type Lister interface {
ListDependencies() ([]string, error)
}
func main() {
say("[blue]> Hold still citizen, scanning dependencies for contraband...")
emptyConfig := true
config := anderson.Config{}
configFile, err := os.Open(".anderson.yml")
if err == nil {
if err := candiedyaml.NewDecoder(configFile).Decode(&config); err != nil {
fatal(errors.New("Looks like your .anderson.yml file is invalid YAML!"))
}
emptyConfig = false
}
lister := lister()
classifier := anderson.LicenseClassifier{
Config: config,
}
failed := false
dependencies, err := lister.ListDependencies()
if err != nil {
fatal(err)
}
classified := map[string]License{}
for _, importPath := range dependencies {
path, err := anderson.LookGopath(importPath)
if err != nil {
fatal(fmt.Errorf("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 {
fatal(fmt.Errorf("Unable to find containing GOPATH for %s: %s", licenseDeclarationPath, err))
}
relPath, err := filepath.Rel(filepath.Join(containingGopath, "src"), licenseDeclarationPath)
if err != nil {
fatal(fmt.Errorf("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 color string
if emptyConfig {
message = license.Name
color = "white"
} else {
message = license.Type.Message()
color = license.Type.Color()
}
totalSize := len(message) + len(relPath)
whitespace := " "
if totalSize < 80 {
whitespace = strings.Repeat(" ", 80-totalSize)
}
say(fmt.Sprintf("[white]%s%s[%s]%s", relPath, whitespace, color, message))
}
if failed {
os.Exit(1)
}
}
func lister() Lister {
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) == 0 {
return anderson.StdinLister{}
} else {
return anderson.PackageLister{}
}
}
func fatal(err error) {
say(fmt.Sprintf("[red]> %s", err))
os.Exit(1)
}
func say(message string) {
fmt.Println(colorstring.Color(message))
}