-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlicense.go
More file actions
78 lines (65 loc) · 1.59 KB
/
Copy pathlicense.go
File metadata and controls
78 lines (65 loc) · 1.59 KB
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
package main
import (
"encoding/json"
"fmt"
"net/url"
"os"
"strings"
)
const (
LicenseListURL = "https://api.github.com/repos/spdx/license-list-data/contents/text"
LicenseDownloadURL = "https://raw.githubusercontent.com/spdx/license-list-data/main/text"
)
type LicenseCommand struct {
List bool `name:"list" short:"l" help:"List all available license templates."`
Stdout bool `name:"stdout" help:"Print contents to stdout instead of writing to a file path (i.e. output to terminal)"`
Names []string `arg:"" name:"names" help:"License template identifiers/names." optional:""`
OutputPath string `name:"output" short:"o" default:"LICENSE"`
}
func (l *LicenseCommand) Run(ctx *Context) error {
if l.List {
body, err := fetchBytes(LicenseListURL)
if err != nil {
return err
}
var respItems []FetchItem
if err := json.Unmarshal(body, &respItems); err != nil {
return err
}
for _, v := range respItems {
fmt.Println(strings.TrimSuffix(v.Name, ".txt"))
}
return nil
}
if len(l.Names) == 0 {
return fmt.Errorf("missing arguments")
}
var f *os.File
var err error
if !l.Stdout {
f, err = os.Create(l.OutputPath)
if err != nil {
return err
}
}
defer f.Close()
for _, name := range l.Names {
name = strings.TrimSuffix(name, ".txt")
path, err := url.JoinPath(LicenseDownloadURL, name+".txt")
if err != nil {
return err
}
body, err := fetchBytes(path)
if err != nil {
return err
}
if !l.Stdout {
if _, err := f.Write(body); err != nil {
return err
}
} else {
fmt.Println(string(body))
}
}
return nil
}