| 1 | // Copyright 2022 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | // Package main provides a CLI REPL session for CEL. |
| 16 | // |
| 17 | // introduces commands for manipulating evaluation context to simulate a realistic host |
| 18 | // environment for evaluating expresions. |
| 19 | // |
| 20 | // example session: |
| 21 | // |
| 22 | // ``` |
| 23 | // $ go run . |
| 24 | // CEL REPL |
| 25 | // %exit or EOF to quit. |
| 26 | |
| 27 | // cel-repl> %let x = 42 |
| 28 | // cel-repl> %let y = {'a': x, 'b': y} |
| 29 | // Adding let failed: |
| 30 | // Error updating y = {'a': x, 'b': y} |
| 31 | // ERROR: <input>:1:15: undeclared reference to 'y' (in container ”) |
| 32 | // |
| 33 | // | {'a': x, 'b': y} |
| 34 | // | ..............^ |
| 35 | // |
| 36 | // cel-repl> %let z = 41 |
| 37 | // cel-repl> %let y = {'a': x, 'b': z} |
| 38 | // cel-repl> y.map(key, y[key]).filter(x, x > 41) |
| 39 | // [42] (list_type:{elem_type:{primitive:INT64}}) |
| 40 | // ``` |
| 41 | package main |
| 42 | |
| 43 | import ( |
| 44 | "fmt" |
| 45 | "os" |
| 46 | |
| 47 | "github.com/google/cel-go/repl" |
| 48 | |
| 49 | "github.com/chzyer/readline" |
| 50 | ) |
| 51 | |
| 52 | func main() { |
| 53 | var c readline.Config |
| 54 | c.Prompt = "cel-repl> " |
| 55 | |
| 56 | err := c.Init() |
| 57 | if err != nil { |
| 58 | fmt.Fprintf(os.Stderr, "Init readline failed: %v\n", err) |
| 59 | os.Exit(1) |
| 60 | } |
| 61 | |
| 62 | rl, err := readline.NewEx(&c) |
| 63 | if err != nil { |
| 64 | fmt.Fprintf(os.Stderr, "NewEx readline failed: %v\n", err) |
| 65 | os.Exit(1) |
| 66 | } |
| 67 | |
| 68 | defer rl.Close() |
| 69 | |
| 70 | fmt.Println("CEL REPL") |
| 71 | fmt.Printf("%%exit or EOF to quit.\n\n") |
| 72 | |
| 73 | eval, err := repl.NewEvaluator() |
| 74 | if err != nil { |
| 75 | fmt.Fprintf(os.Stderr, "NewEvaluator failed: %v\n", err) |
| 76 | os.Exit(1) |
| 77 | } |
| 78 | |
| 79 | PromptLoop: |
| 80 | for { |
| 81 | line, err := rl.Readline() |
| 82 | if err != nil { |
| 83 | // Likely eof or interrupt so exit. |
| 84 | break |
| 85 | } |
| 86 | |
| 87 | cmd, err := repl.Parse(line) |
| 88 | if err != nil { |
| 89 | fmt.Fprintf(os.Stderr, "%v\n", err) |
| 90 | continue |
| 91 | } |
| 92 | status, exit, err := eval.Process(cmd) |
| 93 | if exit { |
| 94 | break PromptLoop |
| 95 | } |
| 96 | if err != nil { |
| 97 | fmt.Fprintf(os.Stderr, "%s\n", err.Error()) |
| 98 | } |
| 99 | if status != "" { |
| 100 | fmt.Println(status) |
| 101 | } |
| 102 | } |
| 103 | } |