-
Notifications
You must be signed in to change notification settings - Fork 0
/
publish.go
96 lines (76 loc) · 1.5 KB
/
publish.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
96
package main
import (
"encoding/json"
"fmt"
"io/fs"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"github.com/pkic/ltl/ltl"
"github.com/go-playground/validator"
yaml "gopkg.in/yaml.v3"
)
func main() {
var result []ltl.PKI
validate := validator.New()
err := filepath.WalkDir("./data", func(s string, f fs.DirEntry, e error) error {
if e != nil {
return e
}
if f.IsDir() {
return nil
}
// Skip hidden files and directories such as .git
if s[0:1] == "." || strings.HasPrefix(s, "ltl.") {
return nil
}
switch filepath.Ext(f.Name()) {
case ".yaml":
file, err := os.Open(s)
if err != nil {
return err
}
var pki ltl.PKI
d := yaml.NewDecoder(file)
d.KnownFields(true)
err = d.Decode(&pki)
if err != nil {
return err
}
err = validate.Struct(pki)
if err != nil {
return err
}
if pki.ID == "" {
pki.ID = strings.ToLower(strings.TrimSuffix(f.Name(), filepath.Ext(f.Name())))
}
result = append(result, pki)
case ".go", ".mod", ".sum", ".md", "":
return nil
default:
return fmt.Errorf("unexpected file type: %s", s)
}
return nil
})
if err != nil {
log.Fatal(err)
}
yamlData, err := yaml.Marshal(result)
if err != nil {
log.Fatal(err)
}
err = ioutil.WriteFile("ltl.yaml", yamlData, os.ModePerm)
if err != nil {
log.Fatal(err)
}
jsonData, err := json.Marshal(result)
if err != nil {
log.Fatal(err)
}
err = ioutil.WriteFile("ltl.json", jsonData, os.ModePerm)
if err != nil {
log.Fatal(err)
}
}