-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
90 lines (75 loc) · 2.01 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
package main
//go:generate licrep -o licenses.go --prefix "Licrep"
import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"text/tabwriter"
"github.com/kopoli/appkit"
licrep "github.com/kopoli/licrep/lib"
)
var (
version = "Undefined"
timestamp = "Undefined"
buildGOOS = "Undefined"
buildGOARCH = "Undefined"
progVersion = "" + version
)
func fault(err error, message string) {
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Error: %s: %s\n",
message, err)
os.Exit(1)
}
}
func main() {
opts := appkit.NewOptions()
opts.Set("program-name", os.Args[0])
opts.Set("program-args", strings.Join(os.Args, " "))
opts.Set("program-version", progVersion)
opts.Set("program-timestamp", timestamp)
opts.Set("program-buildgoos", buildGOOS)
opts.Set("program-buildgoarch", buildGOARCH)
err := licrep.Cli(opts, os.Args)
fault(err, "command line parsing failed")
dir, err := filepath.Abs(opts.Get("directory", "."))
fault(err, "Given directory not valid")
// Show the licenses of this program
if opts.IsSet("show-self-licenses") {
var licenses map[string]LicrepLicense
licenses, err = LicrepGetLicenses()
fault(err, "Internal error: License decoding failed")
var names []string
for i := range licenses {
names = append(names, i)
}
sort.Strings(names)
for _, i := range names {
fmt.Printf("* %s:\n\n%s\n\n", i, licenses[i].Text)
}
os.Exit(0)
}
pkg, err := licrep.GetPackages(".", dir)
fault(err, "Getting package licenses failed")
pkg = licrep.FilterPackages(opts, pkg)
if opts.IsSet("show-license") {
show := opts.Get("show-license", "")
wr := tabwriter.NewWriter(os.Stdout, 0, 4, 2, ' ', 0)
for i := range pkg {
switch show {
case "summary":
fmt.Fprintf(wr, "%s\t%s\n", pkg[i].ImportPath, pkg[i].License)
case "full":
fmt.Printf("* %s %s\n%s\n", pkg[i].ImportPath,
pkg[i].License, pkg[i].LicenseString)
}
}
_ = wr.Flush()
} else {
err = licrep.GenerateEmbeddedLicenses(opts, pkg)
fault(err, "Generating embedded licenses failed")
}
os.Exit(0)
}