This repository has been archived by the owner on Jun 28, 2022. It is now read-only.
/
read.go
117 lines (97 loc) · 2.68 KB
/
read.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
// Copyright 2020 VMware, Inc.
//
// 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 doc
import (
"bufio"
"bytes"
"io"
"os"
"regexp"
"github.com/projectcontour/integration-tester/pkg/must"
"github.com/projectcontour/integration-tester/pkg/utils"
)
// Document is a collection of related Fragments.
type Document struct {
Name string
Parts []Fragment
}
// ReadDocument reads a stream of Fragments that are separated by a
// YAML document separator (see https://yaml.org/spec/1.0/#id2561718).
// The contents of each Fragment is opaque and need not be YAML.
func ReadDocument(in io.Reader) (*Document, error) {
startLine := 0
currentLine := 0
yamlSeparator := regexp.MustCompile("^---[\t\f\r ]*$")
buf := bytes.Buffer{}
doc := Document{}
scanner := bufio.NewScanner(in)
// Scan the input a line at a time.
for scanner.Scan() {
currentLine++
if startLine == 0 {
startLine = currentLine
}
// We just read another line, so replace the newline separator.
if buf.Len() > 0 {
must.Int(buf.WriteString("\n"))
}
if yamlSeparator.Match(scanner.Bytes()) {
// Fragment must be at least one line long.
// If we kept empty fragments, then we would
// not be able to sel the line counts properly,
// since YAML separators are not included.
if buf.Len() > 0 {
doc.Parts = append(doc.Parts, Fragment{
Bytes: utils.CopyBytes(buf.Bytes()),
Location: Location{
Start: startLine,
End: currentLine - 1,
},
})
}
startLine = 0
buf.Truncate(0)
continue
}
must.Int(buf.Write(scanner.Bytes()))
}
// Append any data from the last separator up until EOF.
if buf.Len() > 0 {
doc.Parts = append(doc.Parts, Fragment{
Bytes: utils.CopyBytes(buf.Bytes()),
Location: Location{
Start: startLine,
End: currentLine,
},
})
}
if err := scanner.Err(); err != nil {
return nil, err
}
return &doc, nil
}
// ReadFile reads a Document from the given file path.
func ReadFile(filePath string) (*Document, error) {
fh, err := os.OpenFile(filePath, os.O_RDONLY, 0)
if err != nil {
return nil, err
}
defer fh.Close()
doc, err := ReadDocument(fh)
if err != nil {
return nil, err
}
doc.Name = filePath
return doc, nil
}