-
Notifications
You must be signed in to change notification settings - Fork 133
/
rundata.go
65 lines (57 loc) · 1.52 KB
/
rundata.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
package main
import (
"bufio"
"errors"
"fmt"
"io"
"regexp"
"strings"
mpb "github.com/openconfig/featureprofiles/proto/metadata_go_proto"
"google.golang.org/protobuf/encoding/prototext"
)
// markdownRE matches the heading line: `# XX-1.1: Foo Functional Test`
var markdownRE = regexp.MustCompile(`#(.*?):(.*)`)
// parseMarkdown reads metadata from README.md.
func parseMarkdown(r io.Reader) (*mpb.Metadata, error) {
sc := bufio.NewScanner(r)
if !sc.Scan() {
if err := sc.Err(); err != nil {
return nil, err
}
return nil, errors.New("missing markdown heading")
}
line := sc.Text()
m := markdownRE.FindStringSubmatch(line)
if len(m) < 3 {
return nil, fmt.Errorf("cannot parse markdown: %s", line)
}
return &mpb.Metadata{
PlanId: strings.TrimSpace(m[1]),
Description: strings.TrimSpace(m[2]),
}, nil
}
// parseProto reads metadata from a textproto.
func parseProto(r io.Reader) (*mpb.Metadata, error) {
bytes, err := io.ReadAll(r)
if err != nil {
return nil, err
}
md := new(mpb.Metadata)
return md, prototext.Unmarshal(bytes, md)
}
var marshaller = prototext.MarshalOptions{Multiline: true}
// writeProto generates a complete metadata.textproto to the writer.
func writeProto(w io.Writer, md *mpb.Metadata) error {
const header = `# proto-file: github.com/openconfig/featureprofiles/proto/metadata.proto
# proto-message: Metadata
`
if _, err := w.Write([]byte(header)); err != nil {
return err
}
bytes, err := marshaller.Marshal(md)
if err != nil {
return err
}
_, err = w.Write(bytes)
return err
}