-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
74 lines (62 loc) · 1.52 KB
/
main.go
File metadata and controls
74 lines (62 loc) · 1.52 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
/*
Copyright (c) nexB Inc. and others. All rights reserved.
ScanCode is a trademark of nexB Inc.
SPDX-License-Identifier: Apache-2.0
See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
See https://github.com/aboutcode-org/purl-validator-go for support or download.
See https://aboutcode.org for more information about nexB OSS projects.
*/
package main
import (
"log"
"os"
"path/filepath"
"sort"
"strings"
"github.com/blevesearch/vellum"
)
func main() {
f, err := os.Create("purls.fst")
var purlCount int
if err != nil {
log.Fatal(err)
}
dirname := "cmd/data/"
entries, err := os.ReadDir(dirname)
if err != nil {
log.Fatal(err)
}
builder, err := vellum.New(f, nil)
if err != nil {
log.Fatal(err)
}
for _, entry := range entries {
if !entry.IsDir() && strings.HasSuffix(strings.ToLower(entry.Name()), ".txt") {
fullPath := filepath.Join(dirname, entry.Name())
purlCount += insert_purls(builder, fullPath)
}
}
err = builder.Close()
if err != nil {
log.Fatal(err)
}
log.Printf("FST generated with %d base PackageURLs", purlCount)
log.Printf("FST generated at %s", f.Name())
}
func insert_purls(builder *vellum.Builder, file string) int {
var err error
// #nosec G304
data, _ := os.ReadFile(file)
lines := strings.FieldsFunc(string(data), func(r rune) bool {
return r == '\n'
})
sort.Strings(lines)
log.Printf("Insert PURLs from %s in FST", file)
for _, line := range lines {
err = builder.Insert([]byte(line), 0)
if err != nil {
log.Fatal(err)
}
}
return len(lines)
}