-
Notifications
You must be signed in to change notification settings - Fork 4
/
metadata.go
108 lines (90 loc) · 2.38 KB
/
metadata.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
97
98
99
100
101
102
103
104
105
106
107
108
package server
import (
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
pbVersion "github.com/Percona-Lab/percona-version-service/versionpb"
"github.com/bufbuild/protoyaml-go"
"google.golang.org/protobuf/encoding/protojson"
)
type Metadata struct {
data map[string]*pbVersion.MetadataResponse
fs fs.FS
}
func NewMetadata(fs fs.FS) (*Metadata, error) {
m := &Metadata{
fs: fs,
}
err := m.readAll()
return m, err
}
func (m *Metadata) Product(product string) (*pbVersion.MetadataResponse, error) {
res, ok := m.data[product]
if !ok {
return &pbVersion.MetadataResponse{}, nil
}
return res, nil
}
func (m *Metadata) readAll() error {
m.data = make(map[string]*pbVersion.MetadataResponse)
files, err := fs.ReadDir(m.fs, ".")
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return nil
}
return err
}
for _, f := range files {
if !f.IsDir() {
continue
}
d, err := m.getAllMetadataFromFiles(f.Name())
if err != nil {
return err
}
m.data[f.Name()] = &pbVersion.MetadataResponse{Versions: d}
}
return nil
}
func (m *Metadata) getAllMetadataFromFiles(product string) ([]*pbVersion.MetadataVersion, error) {
if !filepath.IsLocal(product) {
return nil, errors.New("product name is invalid")
}
dir := filepath.Join(".", product)
files, err := fs.ReadDir(m.fs, dir)
if err != nil {
return nil, errors.Join(err, errors.New("could not read metadata from directory"))
}
ret := make([]*pbVersion.MetadataVersion, 0, len(files))
for _, f := range files {
p := filepath.Join(dir, f.Name())
c, err := fs.ReadFile(m.fs, p)
if err != nil {
return nil, errors.Join(err, fmt.Errorf("could not read file %s", p))
}
metaV, err := m.parseFile(c, filepath.Ext(f.Name()))
if err != nil {
return nil, errors.Join(err, fmt.Errorf("could not parse file %s", f.Name()))
}
ret = append(ret, metaV)
}
return ret, nil
}
func (m *Metadata) parseFile(c []byte, fileExt string) (*pbVersion.MetadataVersion, error) {
meta := &pbVersion.MetadataVersion{}
switch fileExt {
case ".yaml", ".yml":
if err := protoyaml.Unmarshal(c, meta); err != nil {
return nil, errors.Join(err, errors.New("could not unmarshal yaml"))
}
case ".json":
if err := protojson.Unmarshal(c, meta); err != nil {
return nil, errors.Join(err, errors.New("could not unmarshal json"))
}
default:
return nil, fmt.Errorf("extension %s not supported", fileExt)
}
return meta, nil
}