This repository has been archived by the owner on Oct 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
/
output_xlsx.go
155 lines (132 loc) · 4.4 KB
/
output_xlsx.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
package main
import (
"fmt"
"sort"
"strconv"
"sync"
"github.com/360EntSecGroup-Skylar/excelize"
"github.com/mitchellh/golicense/config"
"github.com/mitchellh/golicense/license"
"github.com/mitchellh/golicense/module"
)
// XLSXOutput writes the results of license lookups to an XLSX file.
type XLSXOutput struct {
// Path is the path to the file to write. This will be overwritten if
// it exists.
Path string
// Config is the configuration (if any). This will be used to check
// if a license is allowed or not.
Config *config.Config
modules map[*module.Module]interface{}
lock sync.Mutex
}
// Start implements Output
func (o *XLSXOutput) Start(m *module.Module) {}
// Update implements Output
func (o *XLSXOutput) Update(m *module.Module, t license.StatusType, msg string) {}
// Finish implements Output
func (o *XLSXOutput) Finish(m *module.Module, l *license.License, err error) {
o.lock.Lock()
defer o.lock.Unlock()
if o.modules == nil {
o.modules = make(map[*module.Module]interface{})
}
o.modules[m] = l
if err != nil {
o.modules[m] = err
}
}
// Close implements Output
func (o *XLSXOutput) Close() error {
o.lock.Lock()
defer o.lock.Unlock()
const s = "Sheet1"
f := excelize.NewFile()
// Headers
f.SetCellValue(s, "A1", "Dependency")
f.SetCellValue(s, "B1", "Version")
f.SetCellValue(s, "C1", "SPDX ID")
f.SetCellValue(s, "D1", "License")
f.SetCellValue(s, "E1", "Allowed")
f.SetColWidth(s, "A", "A", 40)
f.SetColWidth(s, "B", "B", 20)
f.SetColWidth(s, "C", "C", 20)
f.SetColWidth(s, "D", "D", 40)
f.SetColWidth(s, "E", "E", 10)
// Create all our styles
redStyle, _ := f.NewStyle(`{"fill":{"type":"pattern","pattern":1,"color":["#FFCCCC"]}}`)
yellowStyle, _ := f.NewStyle(`{"fill":{"type":"pattern","pattern":1,"color":["#FFC107"]}}`)
greenStyle, _ := f.NewStyle(`{"fill":{"type":"pattern","pattern":1,"color":["#9CCC65"]}}`)
// Sort the modules by name
keys := make([]string, 0, len(o.modules))
index := map[string]*module.Module{}
for m := range o.modules {
keys = append(keys, m.Path)
index[m.Path] = m
}
sort.Strings(keys)
// Go through each module and output it into the spreadsheet
for i, k := range keys {
row := strconv.FormatInt(int64(i+2), 10)
m := index[k]
f.SetCellValue(s, "A"+row, m.Path)
f.SetCellValue(s, "B"+row, m.Version)
f.SetCellValue(s, "E"+row, "unknown")
f.SetCellStyle(s, "A"+row, "A"+row, yellowStyle)
f.SetCellStyle(s, "B"+row, "B"+row, yellowStyle)
f.SetCellStyle(s, "C"+row, "C"+row, yellowStyle)
f.SetCellStyle(s, "D"+row, "D"+row, yellowStyle)
f.SetCellStyle(s, "E"+row, "E"+row, yellowStyle)
raw := o.modules[m]
if raw == nil {
f.SetCellValue(s, "D"+row, "no")
f.SetCellStyle(s, "A"+row, "A"+row, redStyle)
f.SetCellStyle(s, "B"+row, "B"+row, redStyle)
f.SetCellStyle(s, "C"+row, "C"+row, redStyle)
f.SetCellStyle(s, "D"+row, "D"+row, redStyle)
f.SetCellStyle(s, "E"+row, "E"+row, redStyle)
continue
}
// If the value is an error, then note the error
if err, ok := raw.(error); ok {
f.SetCellValue(s, "D"+row, fmt.Sprintf("ERROR: %s", err))
f.SetCellValue(s, "E"+row, "no")
f.SetCellStyle(s, "A"+row, "A"+row, redStyle)
f.SetCellStyle(s, "B"+row, "B"+row, redStyle)
f.SetCellStyle(s, "C"+row, "C"+row, redStyle)
f.SetCellStyle(s, "D"+row, "D"+row, redStyle)
f.SetCellStyle(s, "E"+row, "E"+row, redStyle)
continue
}
// If the value is a license, then mark the license
if lic, ok := raw.(*license.License); ok {
if lic != nil {
f.SetCellValue(s, fmt.Sprintf("C%d", i+2), lic.SPDX)
}
f.SetCellValue(s, fmt.Sprintf("D%d", i+2), lic.String())
if o.Config != nil {
switch o.Config.Allowed(lic) {
case config.StateAllowed:
f.SetCellValue(s, fmt.Sprintf("E%d", i+2), "yes")
f.SetCellStyle(s, "A"+row, "A"+row, greenStyle)
f.SetCellStyle(s, "B"+row, "B"+row, greenStyle)
f.SetCellStyle(s, "C"+row, "C"+row, greenStyle)
f.SetCellStyle(s, "D"+row, "D"+row, greenStyle)
f.SetCellStyle(s, "E"+row, "E"+row, greenStyle)
case config.StateDenied:
f.SetCellValue(s, fmt.Sprintf("E%d", i+2), "no")
f.SetCellStyle(s, "A"+row, "A"+row, redStyle)
f.SetCellStyle(s, "B"+row, "B"+row, redStyle)
f.SetCellStyle(s, "C"+row, "C"+row, redStyle)
f.SetCellStyle(s, "D"+row, "D"+row, redStyle)
f.SetCellStyle(s, "E"+row, "E"+row, redStyle)
}
}
}
}
// Save
if err := f.SaveAs(o.Path); err != nil {
return err
}
return nil
}