forked from nginxinc/nginx-go-crossplane
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer.go
58 lines (48 loc) · 1.23 KB
/
buffer.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
/**
* Copyright (c) F5, Inc.
*
* This source code is licensed under the Apache License, Version 2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
package crossplane
import (
"io"
"strings"
)
// Creator abstracts file creation (to write configs to something other than files).
type Creator interface {
Create(string) (io.WriteCloser, error)
Reset()
}
// FileString is a string representation of a file.
type FileString struct {
Name string
w strings.Builder
}
// Write makes this an io.Writer.
func (fs *FileString) Write(b []byte) (int, error) {
return fs.w.Write(b)
}
// Close makes this an io.Closer.
func (fs *FileString) Close() error {
fs.w.WriteByte('\n')
return nil
}
// String makes this a Stringer.
func (fs *FileString) String() string {
return fs.w.String()
}
// StringsCreator is an option for rendering config files to strings(s).
type StringsCreator struct {
Files []*FileString
}
// Create makes this a Creator.
func (sc *StringsCreator) Create(file string) (io.WriteCloser, error) {
wc := &FileString{Name: file}
sc.Files = append(sc.Files, wc)
return wc, nil
}
// Reset returns the Creator to its initial state.
func (sc *StringsCreator) Reset() {
sc.Files = []*FileString{}
}