forked from frapposelli/wwhrd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.go
231 lines (188 loc) · 4.94 KB
/
cli.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package main
import (
"fmt"
"os"
"strings"
"github.com/jessevdk/go-flags"
log "github.com/sirupsen/logrus"
)
type cliOpts struct {
List `command:"list" alias:"ls" description:"List licenses"`
Check `command:"check" alias:"chk" description:"Check licenses against config file"`
VersionFlag func() error `long:"version" short:"v" description:"Show CLI version"`
Quiet func() error `short:"q" long:"quiet" description:"quiet mode, do not log accepted packages"`
}
type List struct {
NoColor bool `long:"no-color" description:"disable colored output"`
}
type Check struct {
File string `short:"f" long:"file" description:"input file" default:".wwhrd.yml"`
ReportOut string `short:"r" long:"report-out" description:"report of all licenses found" default:""`
NoColor bool `long:"no-color" description:"disable colored output"`
}
const VersionHelp flags.ErrorType = 1961
var (
version = "dev"
commit = "1961213"
date = "1961-02-13T20:06:35Z"
)
func setQuiet() error {
log.SetLevel(log.ErrorLevel)
return nil
}
func newCli() *flags.Parser {
opts := cliOpts{
VersionFlag: func() error {
return &flags.Error{
Type: VersionHelp,
Message: fmt.Sprintf("version %s\ncommit %s\ndate %s\n", version, commit, date),
}
},
Quiet: setQuiet,
}
parser := flags.NewParser(&opts, flags.HelpFlag|flags.PassDoubleDash)
parser.LongDescription = "What would Henry Rollins do?"
return parser
}
func (l *List) Execute(args []string) error {
if l.NoColor {
log.SetFormatter(&log.TextFormatter{DisableColors: true})
} else {
log.SetFormatter(&log.TextFormatter{ForceColors: true})
}
root, err := rootDir()
if err != nil {
return err
}
pkgs, err := WalkImports(root)
if err != nil {
return err
}
lics := GetLicenses(root, pkgs)
for k, v := range lics {
if v.Recognized() {
log.WithFields(log.Fields{
"package": k,
"license": v.Type,
}).Info("Found License")
} else {
log.WithFields(log.Fields{
"package": k,
}).Warning("Did not find recognized license!")
}
}
return nil
}
const licenseReportHeader = `THE FOLLOWING SETS FORTH ATTRIBUTION NOTICES FOR THIRD PARTY SOFTWARE THAT MAY BE CONTAINED IN PORTIONS OF THE FLOW PROJECT`
const licenseReportTemplate = `
---
The following software may be included in this product: %s. This software contains the following license and notice below:
%s
`
func (c *Check) Execute(args []string) error {
if c.NoColor {
log.SetFormatter(&log.TextFormatter{DisableColors: true})
} else {
log.SetFormatter(&log.TextFormatter{ForceColors: true})
}
t, err := ReadConfig(c.File)
if err != nil {
err = fmt.Errorf("Can't read config file: %s", err)
return err
}
root, err := rootDir()
if err != nil {
return err
}
pkgs, err := WalkImports(root)
if err != nil {
return err
}
lics := GetLicenses(root, pkgs)
// Make a map out of the blacklist
blacklist := make(map[string]bool)
for _, v := range t.Blacklist {
blacklist[v] = true
}
// Make a map out of the whitelist
whitelist := make(map[string]bool)
for _, v := range t.Whitelist {
whitelist[v] = true
}
// Make a map out of the exceptions list
exceptions := make(map[string]bool)
exceptionsWildcard := make(map[string]bool)
for _, v := range t.Exceptions {
if strings.HasSuffix(v, "/...") {
exceptionsWildcard[strings.TrimRight(v, "/...")] = true
} else {
exceptions[v] = true
}
}
var report *os.File
if c.ReportOut != "" {
report, err = os.Create(c.ReportOut)
if err != nil {
return err
}
defer report.Close()
_, err = report.WriteString(licenseReportHeader)
if err != nil {
return err
}
}
PackageList:
for pkg, lic := range lics {
if report != nil {
_, err = report.WriteString(fmt.Sprintf(licenseReportTemplate, pkg, lic.Text))
if err != nil {
return err
}
}
contextLogger := log.WithFields(log.Fields{
"package": pkg,
"license": lic.Type,
})
// License is whitelisted and not specified in blacklist
if whitelist[lic.Type] && !blacklist[lic.Type] {
contextLogger.Info("Found Approved license")
continue PackageList
}
// if we have exceptions wildcards, let's run through them
if len(exceptionsWildcard) > 0 {
for wc, _ := range exceptionsWildcard {
if strings.HasPrefix(pkg, wc) {
// we have a match
contextLogger.Warn("Found exceptioned package")
continue PackageList
}
}
}
// match single-package exceptions
if _, exists := exceptions[pkg]; exists {
contextLogger.Warn("Found exceptioned package")
continue PackageList
}
// no matches, it's a non-approved license
contextLogger.Error("Found Non-Approved license")
err = fmt.Errorf("Non-Approved license found")
}
return err
}
func rootDir() (string, error) {
root, err := os.Getwd()
if err != nil {
return "", err
}
info, err := os.Lstat(root)
if err != nil {
return "", err
}
if info.Mode()&os.ModeSymlink != 0 {
root, err = os.Readlink(root)
if err != nil {
return "", err
}
}
return root, nil
}