/
file.go
175 lines (152 loc) · 4.2 KB
/
file.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright Istio Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package yml
import (
"fmt"
"os"
"strings"
"istio.io/istio/pkg/test"
)
type docType string
const (
namespacesAndCRDs docType = "namespaces_and_crds"
misc docType = "misc"
)
// FileWriter write YAML content to files.
type FileWriter interface {
// WriteYAML writes the given YAML content to one or more YAML files.
WriteYAML(filenamePrefix string, contents ...string) ([]string, error)
// WriteYAMLOrFail calls WriteYAML and fails the test if an error occurs.
WriteYAMLOrFail(t test.Failer, filenamePrefix string, contents ...string) []string
}
type writerImpl struct {
workDir string
}
// NewFileWriter creates a new FileWriter that stores files under workDir.
func NewFileWriter(workDir string) FileWriter {
return &writerImpl{
workDir: workDir,
}
}
// WriteYAML writes the given YAML content to one or more YAML files.
func (w *writerImpl) WriteYAML(filenamePrefix string, contents ...string) ([]string, error) {
out := make([]string, 0, len(contents))
content := JoinString(contents...)
files, err := splitContentsToFiles(w.workDir, content, filenamePrefix)
if err != nil {
return nil, err
}
if len(files) == 0 {
f, err := writeContentsToTempFile(w.workDir, content)
if err != nil {
return nil, err
}
files = append(files, f)
}
out = append(out, files...)
return out, nil
}
// WriteYAMLOrFial calls WriteYAML and fails the test if an error occurs.
func (w *writerImpl) WriteYAMLOrFail(t test.Failer, filenamePrefix string, contents ...string) []string {
t.Helper()
out, err := w.WriteYAML(filenamePrefix, contents...)
if err != nil {
t.Fatal(err)
}
return out
}
func writeContentsToTempFile(workDir, contents string) (filename string, err error) {
defer func() {
if err != nil && filename != "" {
_ = os.Remove(filename)
filename = ""
}
}()
var f *os.File
f, err = os.CreateTemp(workDir, yamlToFilename(contents)+".*.yaml")
if err != nil {
return
}
defer f.Close()
filename = f.Name()
_, err = f.WriteString(contents)
return
}
func yamlToFilename(contents string) string {
spl := SplitYamlByKind(contents)
delete(spl, "")
types := []string{}
for k := range spl {
types = append(types, k)
}
switch len(types) {
case 0:
return "empty"
case 1:
m := GetMetadata(contents)
if len(m) == 0 {
return fmt.Sprintf("%s.%s", types[0], m[0].Name)
}
return types[0]
case 2, 3, 4:
return strings.Join(types, "-")
default:
return strings.Join(types[:4], "-") + "-more"
}
}
func splitContentsToFiles(workDir, content, filenamePrefix string) ([]string, error) {
split := SplitYamlByKind(content)
namespacesAndCrds := &yamlDoc{
docType: namespacesAndCRDs,
content: split["Namespace"],
}
misc := &yamlDoc{
docType: misc,
content: split["CustomResourceDefinition"],
}
// If all elements were put into a single doc just return an empty list, indicating that the original
// content should be used.
docs := []*yamlDoc{namespacesAndCrds, misc}
for _, doc := range docs {
if len(doc.content) == 0 {
return make([]string, 0), nil
}
}
filesToApply := make([]string, 0, len(docs))
for _, doc := range docs {
tfile, err := doc.toTempFile(workDir, filenamePrefix)
if err != nil {
return nil, err
}
filesToApply = append(filesToApply, tfile)
}
return filesToApply, nil
}
type yamlDoc struct {
content string
docType docType
}
func (d *yamlDoc) toTempFile(workDir, fileNamePrefix string) (string, error) {
f, err := os.CreateTemp(workDir, fmt.Sprintf("%s_%s.yaml", fileNamePrefix, d.docType))
if err != nil {
return "", err
}
defer func() { _ = f.Close() }()
name := f.Name()
_, err = f.WriteString(d.content)
if err != nil {
return "", err
}
return name, nil
}