-
Notifications
You must be signed in to change notification settings - Fork 151
/
compile.go
60 lines (49 loc) · 1.2 KB
/
compile.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
package cmd
import (
"context"
"encoding/json"
"io/ioutil"
"os"
_ "github.com/influxdata/flux/builtin"
"github.com/influxdata/flux/lang"
"github.com/influxdata/flux/runtime"
"github.com/spf13/cobra"
)
// compileCmd represents the compile command
var compileCmd = &cobra.Command{
Use: "compile",
Short: "Compile a Flux script into a query specification",
Long: "Compile a Flux script into a query specification (use @ as prefix to the file)",
Args: cobra.ExactArgs(1),
RunE: compile,
}
var prettyPrint bool
func init() {
rootCmd.AddCommand(compileCmd)
compileCmd.Flags().BoolVarP(&prettyPrint, "pretty-print", "p", false, "pretty print the compiled query")
}
func compile(cmd *cobra.Command, args []string) error {
scriptSource := args[0]
var script string
if scriptSource[0] == '@' {
scriptBytes, err := ioutil.ReadFile(scriptSource[1:])
if err != nil {
return err
}
script = string(scriptBytes)
} else {
script = scriptSource
}
c := lang.FluxCompiler{
Query: script,
}
spec, err := c.Compile(context.Background(), runtime.Default)
if err != nil {
return err
}
enc := json.NewEncoder(os.Stdout)
if prettyPrint {
enc.SetIndent("", " ")
}
return enc.Encode(spec)
}