@@ -7,9 +7,10 @@ package main
7
7
8
8
import (
9
9
"encoding/json"
10
+ "fmt"
10
11
"io/fs"
11
12
"os"
12
- goPath "path"
13
+ "path"
13
14
"path/filepath"
14
15
"regexp"
15
16
"sort"
@@ -27,9 +28,14 @@ type LicenseEntry struct {
27
28
}
28
29
29
30
func main () {
31
+ if len (os .Args ) != 3 {
32
+ fmt .Println ("usage: go run generate-go-licenses.go <base-dir> <out-json-file>" )
33
+ os .Exit (1 )
34
+ }
35
+
30
36
base , out := os .Args [1 ], os .Args [2 ]
31
37
32
- paths := []string {}
38
+ var paths []string
33
39
err := filepath .WalkDir (base , func (path string , entry fs.DirEntry , err error ) error {
34
40
if err != nil {
35
41
return err
@@ -46,28 +52,27 @@ func main() {
46
52
47
53
sort .Strings (paths )
48
54
49
- entries := []LicenseEntry {}
50
- for _ , path := range paths {
51
- path := filepath .ToSlash (path )
52
-
53
- licenseText , err := os .ReadFile (path )
55
+ var entries []LicenseEntry
56
+ for _ , filePath := range paths {
57
+ licenseText , err := os .ReadFile (filePath )
54
58
if err != nil {
55
59
panic (err )
56
60
}
57
61
58
- path = strings .Replace (path , base + "/" , "" , 1 )
59
- name := goPath .Dir (path )
62
+ pkgPath := filepath .ToSlash (filePath )
63
+ pkgPath = strings .TrimPrefix (pkgPath , base + "/" )
64
+ pkgName := path .Dir (pkgPath )
60
65
61
66
// There might be a bug somewhere in go-licenses that sometimes interprets the
62
67
// root package as "." and sometimes as "code.gitea.io/gitea". Workaround by
63
68
// removing both of them for the sake of stable output.
64
- if name == "." || name == "code.gitea.io/gitea" {
69
+ if pkgName == "." || pkgName == "code.gitea.io/gitea" {
65
70
continue
66
71
}
67
72
68
73
entries = append (entries , LicenseEntry {
69
- Name : name ,
70
- Path : path ,
74
+ Name : pkgName ,
75
+ Path : pkgPath ,
71
76
LicenseText : string (licenseText ),
72
77
})
73
78
}
0 commit comments