-
Notifications
You must be signed in to change notification settings - Fork 486
/
config.go
97 lines (83 loc) · 2.48 KB
/
config.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
package flow
import (
"fmt"
"strings"
"github.com/grafana/agent/pkg/river/ast"
"github.com/grafana/agent/pkg/river/diag"
"github.com/grafana/agent/pkg/river/parser"
)
// An Argument is an input to a Flow module.
type Argument struct {
// Name of the argument.
Name string `river:",label"`
// Whether the Argument must be provided when evaluating the file.
Optional bool `river:"optional,attr,optional"`
// Description for the Argument.
Comment string `river:"comment,attr,optional"`
// Default value for the argument.
Default any `river:"default,attr,optional"`
}
// File holds the contents of a parsed Flow file.
type File struct {
Name string // File name given to ReadFile.
Node *ast.File // Raw File node.
// Components holds the list of raw River AST blocks describing components.
// The Flow controller can interpret them.
Components []*ast.BlockStmt
ConfigBlocks []*ast.BlockStmt
}
// ReadFile parses the River file specified by bb into a File. name should be
// the name of the file used for reporting errors.
func ReadFile(name string, bb []byte) (*File, error) {
node, err := parser.ParseFile(name, bb)
if err != nil {
return nil, err
}
// Look for predefined non-components blocks (i.e., logging), and store
// everything else into a list of components.
//
// TODO(rfratto): should this code be brought into a helper somewhere? Maybe
// in ast?
var (
components []*ast.BlockStmt
configs []*ast.BlockStmt
)
for _, stmt := range node.Body {
switch stmt := stmt.(type) {
case *ast.AttributeStmt:
return nil, diag.Diagnostic{
Severity: diag.SeverityLevelError,
StartPos: ast.StartPos(stmt.Name).Position(),
EndPos: ast.EndPos(stmt.Name).Position(),
Message: "unrecognized attribute " + stmt.Name.Name,
}
case *ast.BlockStmt:
fullName := strings.Join(stmt.Name, ".")
switch fullName {
case "logging":
configs = append(configs, stmt)
case "tracing":
configs = append(configs, stmt)
case "argument":
configs = append(configs, stmt)
case "export":
configs = append(configs, stmt)
default:
components = append(components, stmt)
}
default:
return nil, diag.Diagnostic{
Severity: diag.SeverityLevelError,
StartPos: ast.StartPos(stmt).Position(),
EndPos: ast.EndPos(stmt).Position(),
Message: fmt.Sprintf("unsupported statement type %T", stmt),
}
}
}
return &File{
Name: name,
Node: node,
Components: components,
ConfigBlocks: configs,
}, nil
}