-
Notifications
You must be signed in to change notification settings - Fork 144
/
read_partial.go
69 lines (59 loc) · 1.54 KB
/
read_partial.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
package venom
import (
"bufio"
"context"
"strings"
"unicode"
"unicode/utf8"
"github.com/pkg/errors"
"github.com/rockbears/yaml"
)
func getUserExecutorInputYML(ctx context.Context, btesIn []byte) (H, error) {
btes := readPartialYML(btesIn, "input")
var result = map[string]interface{}{}
var tmpResult = map[string]interface{}{}
if len(btes) > 0 {
if err := yaml.Unmarshal([]byte(btes), &tmpResult); err != nil {
return nil, err
}
}
for k, v := range tmpResult {
result[k] = v
}
return result, nil
}
func getVarFromPartialYML(ctx context.Context, btesIn []byte) (H, error) {
btes := readPartialYML(btesIn, "vars")
type partialVars struct {
Vars H `yaml:"vars" json:"vars"`
}
var partial partialVars
if len(btes) > 0 {
if err := yaml.Unmarshal([]byte(btes), &partial); err != nil {
Error(context.Background(), "file content: %s", string(btes))
return nil, errors.Wrapf(err, "error while unmarshal - see venom.log")
}
}
return partial.Vars, nil
}
// readPartialYML extract a yml part from a given string
func readPartialYML(btes []byte, attribute string) string {
var result []string
scanner := bufio.NewScanner(strings.NewReader(string(btes)))
var record bool
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, attribute+":") {
record = true
} else if len(line) > 0 {
c, _ := utf8.DecodeRuneInString(line[0:1])
if !unicode.IsSpace(c) && !strings.HasPrefix(line, "-") {
record = false
}
}
if record {
result = append(result, line)
}
}
return strings.Join(result, "\n")
}