forked from google/go-licenses
-
Notifications
You must be signed in to change notification settings - Fork 0
/
report.go
127 lines (112 loc) · 2.98 KB
/
report.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
// Copyright 2019 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"encoding/csv"
"io/ioutil"
"os"
"text/template"
"github.com/google/go-licenses/licenses"
"github.com/spf13/cobra"
"k8s.io/klog/v2"
)
const (
UNKNOWN = "Unknown"
)
var (
reportHelp = "Prints report of all licenses that apply to one or more Go packages and their dependencies."
reportCmd = &cobra.Command{
Use: "report <package> [package...]",
Short: reportHelp,
Long: reportHelp + packageHelp,
Args: cobra.MinimumNArgs(1),
RunE: reportMain,
}
templateFile string
)
func init() {
reportCmd.Flags().StringVar(&templateFile, "template", "", "Custom Go template file to use for report")
rootCmd.AddCommand(reportCmd)
}
type libraryData struct {
Name string
LicenseURL string
LicenseName string
Version string
}
func reportMain(_ *cobra.Command, args []string) error {
classifier, err := licenses.NewClassifier(confidenceThreshold)
if err != nil {
return err
}
libs, err := licenses.Libraries(context.Background(), classifier, ignore, args...)
if err != nil {
return err
}
var reportData []libraryData
for _, lib := range libs {
version := lib.Version()
if len(version) == 0 {
version = UNKNOWN
}
libData := libraryData{
Name: lib.Name(),
Version: version,
LicenseURL: UNKNOWN,
LicenseName: UNKNOWN,
}
if lib.LicensePath != "" {
name, _, err := classifier.Identify(lib.LicensePath)
if err == nil {
libData.LicenseName = name
} else {
klog.Errorf("Error identifying license in %q: %v", lib.LicensePath, err)
}
url, err := lib.FileURL(context.Background(), lib.LicensePath)
if err == nil {
libData.LicenseURL = url
} else {
klog.Warningf("Error discovering license URL: %s", err)
}
}
reportData = append(reportData, libData)
}
if templateFile == "" {
return reportCSV(reportData)
} else {
return reportTemplate(reportData)
}
}
func reportCSV(libs []libraryData) error {
writer := csv.NewWriter(os.Stdout)
for _, lib := range libs {
if err := writer.Write([]string{lib.Name, lib.LicenseURL, lib.LicenseName}); err != nil {
return err
}
}
writer.Flush()
return writer.Error()
}
func reportTemplate(libs []libraryData) error {
templateBytes, err := ioutil.ReadFile(templateFile)
if err != nil {
return err
}
tmpl, err := template.New("").Parse(string(templateBytes))
if err != nil {
return err
}
return tmpl.Execute(os.Stdout, libs)
}