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"
)
type Argument struct {
Name string `river:",label"`
Optional bool `river:"optional,attr,optional"`
Comment string `river:"comment,attr,optional"`
Default any `river:"default,attr,optional"`
}
type File struct {
Name string
Node *ast.File
Components []*ast.BlockStmt
ConfigBlocks []*ast.BlockStmt
}
func ReadFile(name string, bb []byte) (*File, error) {
node, err := parser.ParseFile(name, bb)
if err != nil {
return nil, err
}
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
}