-
Notifications
You must be signed in to change notification settings - Fork 479
/
standalone_loader.go
145 lines (117 loc) · 3.05 KB
/
standalone_loader.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
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation and Dapr Contributors.
// Licensed under the MIT License.
// ------------------------------------------------------------
package conformance
import (
"bufio"
"bytes"
"io"
"io/ioutil"
"log"
"path/filepath"
"strings"
"github.com/ghodss/yaml"
)
const (
yamlSeparator = "\n---"
componentKind = "Component"
)
// StandaloneComponents loads components in a standalone mode environment
type StandaloneComponents struct {
componentsPath string
}
// NewStandaloneComponents returns a new standalone loader
func NewStandaloneComponents(componentPath string) *StandaloneComponents {
return &StandaloneComponents{
componentsPath: componentPath,
}
}
// LoadComponents loads dapr components from a given directory
func (s *StandaloneComponents) LoadComponents() ([]Component, error) {
dir := s.componentsPath
files, err := ioutil.ReadDir(dir)
if err != nil {
return nil, err
}
list := []Component{}
for _, file := range files {
if !file.IsDir() && s.isYaml(file.Name()) {
path := filepath.Join(dir, file.Name())
b, err := ioutil.ReadFile(path)
if err != nil {
log.Printf("error reading file %s : %s", path, err)
continue
}
components, _ := s.decodeYaml(path, b)
list = append(list, components...)
}
}
return list, nil
}
// isYaml checks whether the file is yaml or not
func (s *StandaloneComponents) isYaml(fileName string) bool {
extension := strings.ToLower(filepath.Ext(fileName))
if extension == ".yaml" || extension == ".yml" {
return true
}
return false
}
// decodeYaml decodes the yaml document
func (s *StandaloneComponents) decodeYaml(filename string, b []byte) ([]Component, []error) {
list := []Component{}
errors := []error{}
scanner := bufio.NewScanner(bytes.NewReader(b))
scanner.Split(s.splitYamlDoc)
for {
var comp Component
comp.Spec = ComponentSpec{}
err := s.decode(scanner, &comp)
if err == io.EOF {
break
}
if comp.Kind != componentKind {
continue
}
list = append(list, comp)
}
return list, errors
}
// decode reads the YAML resource in document
func (s *StandaloneComponents) decode(scanner *bufio.Scanner, c interface{}) error {
if scanner.Scan() {
return yaml.Unmarshal(scanner.Bytes(), &c)
}
err := scanner.Err()
if err == nil {
err = io.EOF
}
return err
}
// splitYamlDoc - splits the yaml docs
func (s *StandaloneComponents) splitYamlDoc(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
}
sep := len([]byte(yamlSeparator))
if i := bytes.Index(data, []byte(yamlSeparator)); i >= 0 {
i += sep
after := data[i:]
if len(after) == 0 {
if atEOF {
return len(data), data[:len(data)-sep], nil
}
return 0, nil, nil
}
if j := bytes.IndexByte(after, '\n'); j >= 0 {
return i + j + 1, data[0 : i-sep], nil
}
return 0, nil, nil
}
// If we're at EOF, we have a final, non-terminated line. Return it.
if atEOF {
return len(data), data, nil
}
// Request more data.
return 0, nil, nil
}