-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
95 lines (73 loc) · 1.79 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
// Copyright (C) 2023 Mya Pitzeruse
// SPDX-License-Identifier: MIT
package main
import (
"encoding/json"
"flag"
"log"
"os"
"sort"
"strings"
"text/template"
"github.com/Masterminds/sprig/v3"
"github.com/spdx/tools-golang/spdx/v2/v2_3"
"go.pitz.tech/spdx-fmt/internal/templates"
)
//go:generate
func run(input, output, report string) (err error) {
fileIn := os.Stdin
fileOut := os.Stdout
if input != "-" {
fileIn, err = os.Open(input)
if err != nil {
return err
}
defer fileIn.Close()
}
if output != "-" {
fileOut, err = os.Create(output)
if err != nil {
return err
}
defer fileOut.Close()
}
doc := &v2_3.Document{}
err = json.NewDecoder(fileIn).Decode(doc)
if err != nil {
return err
}
sort.Slice(doc.Packages, func(i, j int) bool {
return strings.Compare(doc.Packages[i].PackageName, doc.Packages[j].PackageName) < 0
})
sort.Slice(doc.Files, func(i, j int) bool {
return strings.Compare(doc.Files[i].FileName, doc.Files[j].FileName) < 0
})
contents, ok := templates.Lookup(report)
if !ok {
contents, err = os.ReadFile(report)
if err != nil {
return err
}
}
t, err := template.New("spdx-fmt").
Funcs(sprig.FuncMap()).
Parse(string(contents))
if err != nil {
return err
}
return t.Execute(fileOut, doc)
}
func main() {
cli := flag.NewFlagSet("spdx-fmt", flag.ExitOnError)
input := cli.String("input", "-", "Specify the input spdx.json file. Defaults to '-' for stdin.")
output := cli.String("output", "-", "Specify the output markdown file. Defaults to '-' for stdout.")
report := cli.String("report", "spdx", "Specify which report to render (spdx, third-party-licenses, or a file path).")
err := cli.Parse(os.Args[1:])
if err != nil {
log.Fatal(err)
}
err = run(*input, *output, *report)
if err != nil {
log.Fatal(err)
}
}