-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
121 lines (98 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
package main
import (
"bytes"
"flag"
"fmt"
"go/build"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"strconv"
"strings"
"text/template"
"time"
"github.com/charith-elastic/licence-detector/detector"
)
var (
inFlag = flag.String("in", "-", "Dependency list (output from go list -m -json all)")
includeIndirectFlag = flag.Bool("includeIndirect", false, "Include indirect dependencies")
outFlag = flag.String("out", "-", "Path to output the notice information")
templateFlag = flag.String("template", "NOTICE.txt.tmpl", "Path to the template file")
goModCache = filepath.Join(build.Default.GOPATH, "pkg", "mod")
)
func main() {
flag.Parse()
depInput, err := mkReader(*inFlag)
if err != nil {
log.Fatalf("Failed to create reader for %s: %v", *inFlag, err)
}
defer depInput.Close()
dependencies, err := detector.Detect(depInput, *includeIndirectFlag)
if err != nil {
log.Fatalf("Failed to detect licences: %v", err)
}
if err := renderNotice(dependencies, *templateFlag, *outFlag); err != nil {
log.Fatalf("Failed to render notice: %v", err)
}
}
func mkReader(path string) (io.ReadCloser, error) {
if path == "-" {
return ioutil.NopCloser(os.Stdin), nil
}
return os.Open(path)
}
func renderNotice(dependencies *detector.Dependencies, templatePath, outputPath string) error {
funcMap := template.FuncMap{
"currentYear": CurrentYear,
"line": Line,
"licenceText": LicenceText,
}
tmpl, err := template.New(filepath.Base(templatePath)).Funcs(funcMap).ParseFiles(templatePath)
if err != nil {
return fmt.Errorf("failed to parse template at %s: %w", templatePath, err)
}
w, cleanup, err := mkWriter(outputPath)
if err != nil {
return fmt.Errorf("failed to create output file %s: %w", outputPath, err)
}
defer cleanup()
if err := tmpl.Execute(w, dependencies); err != nil {
return fmt.Errorf("failed to render template: %w", err)
}
return nil
}
func mkWriter(path string) (io.Writer, func(), error) {
if path == "-" {
return os.Stdout, func() {}, nil
}
f, err := os.Create(path)
return f, func() { f.Close() }, err
}
/* Template functions */
func CurrentYear() string {
return strconv.Itoa(time.Now().Year())
}
func Line(ch string) string {
return strings.Repeat(ch, 80)
}
func LicenceText(licInfo detector.LicenceInfo) string {
if licInfo.Error != nil {
return licInfo.Error.Error()
}
var buf bytes.Buffer
buf.WriteString("Contents of probable licence file ")
buf.WriteString(strings.Replace(licInfo.LicenceFile, goModCache, "$GOMODCACHE", -1))
buf.WriteString(":\n\n")
f, err := os.Open(licInfo.LicenceFile)
if err != nil {
log.Fatalf("Failed to open licence file %s: %v", licInfo.LicenceFile, err)
}
defer f.Close()
_, err = io.Copy(&buf, f)
if err != nil {
log.Fatalf("Failed to read licence file %s: %v", licInfo.LicenceFile, err)
}
return buf.String()
}