github.com/grafana/tanka@v0.26.1-0.20240506093700-c22cfc35c21a/cmd/tk/jsonnet.go (about) 1 package main 2 3 import ( 4 "encoding/json" 5 6 "github.com/go-clix/cli" 7 8 "github.com/grafana/tanka/pkg/tanka" 9 ) 10 11 func evalCmd() *cli.Command { 12 cmd := &cli.Command{ 13 Short: "evaluate the jsonnet to json", 14 Use: "eval <path>", 15 Args: workflowArgs, 16 } 17 18 evalPattern := cmd.Flags().StringP("eval", "e", "", "Evaluate expression on output of jsonnet") 19 jsonnetImplementation := cmd.Flags().String("jsonnet-implementation", "go", "Use `go` to use native go-jsonnet implementation and `binary:<path>` to delegate evaluation to a binary (with the same API as the regular `jsonnet` binary, see the BinaryImplementation docstrings for more details)") 20 21 getJsonnetOpts := jsonnetFlags(cmd.Flags()) 22 23 cmd.Run = func(cmd *cli.Command, args []string) error { 24 jsonnetOpts := tanka.Opts{ 25 JsonnetImplementation: *jsonnetImplementation, 26 JsonnetOpts: getJsonnetOpts(), 27 } 28 if *evalPattern != "" { 29 jsonnetOpts.EvalScript = tanka.PatternEvalScript(*evalPattern) 30 } 31 raw, err := tanka.Eval(args[0], jsonnetOpts) 32 33 if raw == nil && err != nil { 34 return err 35 } 36 37 out, err := json.MarshalIndent(raw, "", " ") 38 if err != nil { 39 return err 40 } 41 42 return pageln(string(out)) 43 } 44 45 return cmd 46 }