|
1 | 1 | package main
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "bufio" |
4 | 5 | "bytes"
|
5 | 6 | "context"
|
6 | 7 | "errors"
|
@@ -35,22 +36,24 @@ var RootCmd = &cobra.Command{
|
35 | 36 | Short: "Test your configuration files using Open Policy Agent",
|
36 | 37 | Version: fmt.Sprintf("Version: %s\nCommit: %s\nDate: %s\n", version, commit, date),
|
37 | 38 | Run: func(cmd *cobra.Command, args []string) {
|
| 39 | + |
38 | 40 | if len(args) < 1 {
|
39 | 41 | cmd.SilenceErrors = true
|
40 | 42 | fmt.Println("The first argument should be a file")
|
41 | 43 | os.Exit(1)
|
42 | 44 | }
|
43 |
| - cmd.SilenceUsage = true |
44 | 45 |
|
45 | 46 | compiler, err := buildCompiler(viper.GetString("policy"))
|
46 | 47 | if err != nil {
|
47 |
| - fmt.Sprintf("Unable to find policies directory: %s", err) |
| 48 | + fmt.Printf("Unable to find policy directory: %s", err) |
48 | 49 | os.Exit(1)
|
49 | 50 | }
|
50 | 51 |
|
51 | 52 | foundFailures := false
|
52 | 53 | for _, fileName := range args {
|
53 |
| - fmt.Println(fileName) |
| 54 | + if fileName != "-" { |
| 55 | + fmt.Println(fileName) |
| 56 | + } |
54 | 57 | failures, warnings := processFile(fileName, compiler)
|
55 | 58 | if failures != nil {
|
56 | 59 | foundFailures = true
|
@@ -89,8 +92,18 @@ func detectLineBreak(haystack []byte) string {
|
89 | 92 | }
|
90 | 93 |
|
91 | 94 | func processFile(fileName string, compiler *ast.Compiler) (error, error) {
|
92 |
| - filePath, _ := filepath.Abs(fileName) |
93 |
| - data, err := ioutil.ReadFile(filePath) |
| 95 | + |
| 96 | + var data []byte |
| 97 | + var err error |
| 98 | + |
| 99 | + if fileName == "-" { |
| 100 | + reader := bufio.NewReader(os.Stdin) |
| 101 | + data, err = ioutil.ReadAll(reader) |
| 102 | + } else { |
| 103 | + filePath, _ := filepath.Abs(fileName) |
| 104 | + data, err = ioutil.ReadFile(filePath) |
| 105 | + } |
| 106 | + |
94 | 107 | if err != nil {
|
95 | 108 | return fmt.Errorf("Unable to open file %s: %s", fileName, err), nil
|
96 | 109 | }
|
@@ -139,7 +152,7 @@ func makeQuery(query string, input interface{}, compiler *ast.Compiler) error {
|
139 | 152 | ctx := context.Background()
|
140 | 153 | rs, err := rego.Eval(ctx)
|
141 | 154 | if err != nil {
|
142 |
| - return fmt.Errorf("Problem evaluating rego policies: %s", err) |
| 155 | + return fmt.Errorf("Problem evaluating rego policy: %s", err) |
143 | 156 | }
|
144 | 157 |
|
145 | 158 | var errorsList *multierror.Error
|
|
0 commit comments