aboutsummaryrefslogtreecommitdiff
path: root/reporter/reporter.go
blob: 3d0766813df97a7970167eb7217e6162a73877d3 (plain)
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
// Package reporter contains functions to generate a basic license count
// report from an in-memory SPDX Package section whose Files have been
// analyzed.
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
package reporter

import (
	"fmt"
	"io"
	"sort"
	"text/tabwriter"

	"github.com/spdx/tools-golang/spdx/v2_1"
	"github.com/spdx/tools-golang/spdx/v2_2"
)

// ===== 2.1 Reporter functions =====

// Generate2_1 takes a Package whose Files have been analyzed and an
// io.Writer, and outputs to the io.Writer a tabulated count of
// the number of Files for each unique LicenseConcluded in the set.
func Generate2_1(pkg *v2_1.Package, w io.Writer) error {
	if pkg.FilesAnalyzed == false {
		return fmt.Errorf("Package FilesAnalyzed is false")
	}
	totalFound, totalNotFound, foundCounts := countLicenses2_1(pkg)

	wr := tabwriter.NewWriter(w, 0, 0, 2, ' ', tabwriter.AlignRight)

	fmt.Fprintf(wr, "%d\t  License found\n", totalFound)
	fmt.Fprintf(wr, "%d\t  License not found\n", totalNotFound)
	fmt.Fprintf(wr, "%d\t  TOTAL\n", totalFound+totalNotFound)
	fmt.Fprintf(wr, "\n")

	counts := []struct {
		lic   string
		count int
	}{}
	for k, v := range foundCounts {
		var entry struct {
			lic   string
			count int
		}
		entry.lic = k
		entry.count = v
		counts = append(counts, entry)
	}

	sort.Slice(counts, func(i, j int) bool { return counts[i].count > counts[j].count })

	for _, c := range counts {
		fmt.Fprintf(wr, "%d\t  %s\n", c.count, c.lic)
	}
	fmt.Fprintf(wr, "%d\t  TOTAL FOUND\n", totalFound)

	wr.Flush()
	return nil
}

func countLicenses2_1(pkg *v2_1.Package) (int, int, map[string]int) {
	if pkg == nil || pkg.Files == nil {
		return 0, 0, nil
	}

	totalFound := 0
	totalNotFound := 0
	foundCounts := map[string]int{}
	for _, f := range pkg.Files {
		if f.LicenseConcluded == "" || f.LicenseConcluded == "NOASSERTION" {
			totalNotFound++
		} else {
			totalFound++
			foundCounts[f.LicenseConcluded]++
		}
	}

	return totalFound, totalNotFound, foundCounts
}

// ===== 2.2 Reporter functions =====

// Generate2_2 takes a Package whose Files have been analyzed and an
// io.Writer, and outputs to the io.Writer a tabulated count of
// the number of Files for each unique LicenseConcluded in the set.
func Generate2_2(pkg *v2_2.Package, w io.Writer) error {
	if pkg.FilesAnalyzed == false {
		return fmt.Errorf("Package FilesAnalyzed is false")
	}
	totalFound, totalNotFound, foundCounts := countLicenses2_2(pkg)

	wr := tabwriter.NewWriter(w, 0, 0, 2, ' ', tabwriter.AlignRight)

	fmt.Fprintf(wr, "%d\t  License found\n", totalFound)
	fmt.Fprintf(wr, "%d\t  License not found\n", totalNotFound)
	fmt.Fprintf(wr, "%d\t  TOTAL\n", totalFound+totalNotFound)
	fmt.Fprintf(wr, "\n")

	counts := []struct {
		lic   string
		count int
	}{}
	for k, v := range foundCounts {
		var entry struct {
			lic   string
			count int
		}
		entry.lic = k
		entry.count = v
		counts = append(counts, entry)
	}

	sort.Slice(counts, func(i, j int) bool { return counts[i].count > counts[j].count })

	for _, c := range counts {
		fmt.Fprintf(wr, "%d\t  %s\n", c.count, c.lic)
	}
	fmt.Fprintf(wr, "%d\t  TOTAL FOUND\n", totalFound)

	wr.Flush()
	return nil
}

func countLicenses2_2(pkg *v2_2.Package) (int, int, map[string]int) {
	if pkg == nil || pkg.Files == nil {
		return 0, 0, nil
	}

	totalFound := 0
	totalNotFound := 0
	foundCounts := map[string]int{}
	for _, f := range pkg.Files {
		if f.LicenseConcluded == "" || f.LicenseConcluded == "NOASSERTION" {
			totalNotFound++
		} else {
			totalFound++
			foundCounts[f.LicenseConcluded]++
		}
	}

	return totalFound, totalNotFound, foundCounts
}