-
Notifications
You must be signed in to change notification settings - Fork 3
/
yaml_storage.go
executable file
·153 lines (127 loc) · 3.94 KB
/
yaml_storage.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright (C) 2021 CGI France
//
// This file is part of LINO.
//
// LINO is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// LINO is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with LINO. If not, see <http://www.gnu.org/licenses/>.
package id
import (
"bytes"
"io/ioutil"
"github.com/cgi-fr/lino/pkg/id"
"gopkg.in/yaml.v3"
)
// YAMLStructure of the file.
type YAMLStructure struct {
Version string `yaml:"version"`
IngressDescriptor YAMLIngressDescriptor `yaml:"IngressDescriptor"`
}
// YAMLIngressDescriptor defines how to store an ingress descriptor in YAML format.
type YAMLIngressDescriptor struct {
StartTable string `yaml:"startTable"`
Relations []YAMLRelation `yaml:"relations"`
}
// YAMLRelation defines how to store a relation in YAML format.
type YAMLRelation struct {
Name string `yaml:"name"`
Parent YAMLTable `yaml:"parent"`
Child YAMLTable `yaml:"child"`
}
// YAMLTable defines how to store a table in YAML format.
type YAMLTable struct {
Name string `yaml:"name"`
Lookup bool `yaml:"lookup"`
}
// YAMLStorage provides storage in a local YAML file
type YAMLStorage struct {
filename string
}
// NewYAMLStorage create a new YAML storage
func NewYAMLStorage(filename string) *YAMLStorage {
return &YAMLStorage{filename: filename}
}
// Store ingress descriptor in the YAML file
func (s *YAMLStorage) Store(id id.IngressDescriptor) *id.Error {
structure := YAMLStructure{
Version: Version,
}
relations := []YAMLRelation{}
list := id.Relations()
for i := uint(0); i < list.Len(); i++ {
relation := list.Relation(i)
relations = append(relations, YAMLRelation{
Name: relation.Name(),
Parent: YAMLTable{Name: relation.Parent().Name(), Lookup: relation.LookUpParent()},
Child: YAMLTable{Name: relation.Child().Name(), Lookup: relation.LookUpChild()},
})
}
structure.IngressDescriptor = YAMLIngressDescriptor{
StartTable: id.StartTable().Name(),
Relations: relations,
}
err := writeFile(&structure, s.filename)
if err != nil {
return err
}
return nil
}
func (s *YAMLStorage) Read() (id.IngressDescriptor, *id.Error) {
structure, err := readFile(s.filename)
if err != nil {
return nil, err
}
relations := []id.IngressRelation{}
for _, relation := range structure.IngressDescriptor.Relations {
relations = append(relations,
id.NewIngressRelation(
id.NewRelation(
relation.Name,
id.NewTable(relation.Parent.Name),
id.NewTable(relation.Child.Name),
),
relation.Parent.Lookup, relation.Child.Lookup),
)
}
return id.NewIngressDescriptor(id.NewTable(structure.IngressDescriptor.StartTable), id.NewIngressRelationList(relations)), nil
}
func writeFile(structure *YAMLStructure, filename string) *id.Error {
out := &bytes.Buffer{}
enc := yaml.NewEncoder(out)
enc.SetIndent(2)
err := enc.Encode(structure)
if err != nil {
return &id.Error{Description: err.Error()}
}
err = ioutil.WriteFile(filename, out.Bytes(), 0600)
if err != nil {
return &id.Error{Description: err.Error()}
}
return nil
}
func readFile(filename string) (*YAMLStructure, *id.Error) {
structure := &YAMLStructure{
Version: Version,
}
dat, err := ioutil.ReadFile(filename)
if err != nil {
return nil, &id.Error{Description: err.Error()}
}
err = yaml.Unmarshal(dat, structure)
if err != nil {
return nil, &id.Error{Description: err.Error()}
}
if structure.Version != Version {
return nil, &id.Error{Description: "invalid version in ./ingress-descriptor.yaml (" + structure.Version + ")"}
}
return structure, nil
}