forked from kubernetes/kops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resources.go
220 lines (180 loc) · 4.04 KB
/
resources.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package fi
import (
"bytes"
"encoding/json"
"fmt"
"io"
"k8s.io/kops/util/pkg/vfs"
"os"
)
type Resource interface {
Open() (io.Reader, error)
}
type TemplateResource interface {
Resource
Curry(args []string) TemplateResource
}
func ResourcesMatch(a, b Resource) (bool, error) {
aReader, err := a.Open()
if err != nil {
return false, err
}
defer SafeClose(aReader)
bReader, err := b.Open()
if err != nil {
return false, err
}
defer SafeClose(bReader)
const size = 8192
aData := make([]byte, size)
bData := make([]byte, size)
for {
aN, aErr := io.ReadFull(aReader, aData)
if aErr != nil && aErr != io.EOF && aErr != io.ErrUnexpectedEOF {
return false, aErr
}
bN, bErr := io.ReadFull(bReader, bData)
if bErr != nil && bErr != io.EOF && bErr != io.ErrUnexpectedEOF {
return false, bErr
}
if aErr == nil && bErr == nil {
if aN != size || bN != size {
panic("violation of io.ReadFull contract")
}
if !bytes.Equal(aData, bData) {
return false, nil
}
continue
}
if aN != bN {
return false, nil
}
return bytes.Equal(aData[0:aN], bData[0:bN]), nil
}
}
func CopyResource(dest io.Writer, r Resource) (int64, error) {
in, err := r.Open()
if err != nil {
if os.IsNotExist(err) {
return 0, err
}
return 0, fmt.Errorf("error opening resource: %v", err)
}
defer SafeClose(in)
n, err := io.Copy(dest, in)
if err != nil {
return n, fmt.Errorf("error copying resource: %v", err)
}
return n, nil
}
func ResourceAsString(r Resource) (string, error) {
buf := new(bytes.Buffer)
_, err := CopyResource(buf, r)
if err != nil {
return "", err
}
return buf.String(), nil
}
func ResourceAsBytes(r Resource) ([]byte, error) {
buf := new(bytes.Buffer)
_, err := CopyResource(buf, r)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
type StringResource struct {
s string
}
var _ Resource = &StringResource{}
func NewStringResource(s string) *StringResource {
return &StringResource{s: s}
}
func (s *StringResource) Open() (io.Reader, error) {
r := bytes.NewReader([]byte(s.s))
return r, nil
}
func (s *StringResource) WriteTo(out io.Writer) error {
_, err := out.Write([]byte(s.s))
return err
}
type BytesResource struct {
data []byte
}
var _ Resource = &BytesResource{}
func NewBytesResource(data []byte) *BytesResource {
return &BytesResource{data: data}
}
func (r *BytesResource) Open() (io.Reader, error) {
reader := bytes.NewReader([]byte(r.data))
return reader, nil
}
type FileResource struct {
Path string
}
var _ Resource = &FileResource{}
func NewFileResource(path string) *FileResource {
return &FileResource{Path: path}
}
func (r *FileResource) Open() (io.Reader, error) {
in, err := os.Open(r.Path)
if err != nil {
if os.IsNotExist(err) {
return nil, err
}
return nil, fmt.Errorf("error opening file %q: %v", r.Path, err)
}
return in, err
}
type VFSResource struct {
Path vfs.Path
}
var _ Resource = &VFSResource{}
func NewVFSResource(path vfs.Path) *VFSResource {
return &VFSResource{Path: path}
}
func (r *VFSResource) Open() (io.Reader, error) {
data, err := r.Path.ReadFile()
if err != nil {
if os.IsNotExist(err) {
return nil, err
}
return nil, fmt.Errorf("error opening file %q: %v", r.Path, err)
}
b := bytes.NewBuffer(data)
return b, err
}
type ResourceHolder struct {
Name string
Resource Resource
}
var _ Resource = &ResourceHolder{}
func (o *ResourceHolder) Open() (io.Reader, error) {
if o.Resource == nil {
return nil, fmt.Errorf("ResourceHolder %q is not bound", o.Name)
}
return o.Resource.Open()
}
func (o *ResourceHolder) UnmarshalJSON(data []byte) error {
var jsonName string
err := json.Unmarshal(data, &jsonName)
if err != nil {
return err
}
o.Name = jsonName
return nil
}
func (o *ResourceHolder) Unwrap() Resource {
return o.Resource
}
func (o *ResourceHolder) AsString() (string, error) {
return ResourceAsString(o.Unwrap())
}
func (o *ResourceHolder) AsBytes() ([]byte, error) {
return ResourceAsBytes(o.Unwrap())
}
func WrapResource(r Resource) *ResourceHolder {
return &ResourceHolder{
Resource: r,
}
}