Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/tools/rivereval/main.go
4095 views
1
// Command rivereval reads a River file from disk, evaluates it as an
2
// expression, and prints the result as a River value.
3
package main
4
5
import (
6
"fmt"
7
"os"
8
9
"github.com/grafana/agent/pkg/river/parser"
10
"github.com/grafana/agent/pkg/river/token/builder"
11
"github.com/grafana/agent/pkg/river/vm"
12
)
13
14
func main() {
15
if err := run(); err != nil {
16
fmt.Fprintln(os.Stderr, err)
17
}
18
}
19
20
func run() error {
21
args := os.Args[1:]
22
23
if len(args) != 1 {
24
return fmt.Errorf("usage: rivereval [file]")
25
}
26
27
contents, err := os.ReadFile(args[0])
28
if err != nil {
29
return err
30
}
31
32
// We currently can't support parsing entire files since eval.Evaluate
33
// assumes you'll pass a block with a struct schema to it. This might be a
34
// restriction we can loosen in the future.
35
node, err := parser.ParseExpression(string(contents))
36
if err != nil {
37
return err
38
}
39
eval := vm.New(node)
40
41
var v interface{}
42
if err := eval.Evaluate(nil, &v); err != nil {
43
return err
44
}
45
46
expr := builder.NewExpr()
47
expr.SetValue(v)
48
49
_, _ = expr.WriteTo(os.Stdout)
50
fmt.Println() // Write an extra newline at the end
51
return nil
52
}
53
54