Skip to content

Commit 4d8ffed

Browse files
committed
support stdin for passing files
1 parent 63ef92f commit 4d8ffed

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ testdata/deployment.yaml
3434
Deployments are not allowed
3535
```
3636

37+
`conftest` can also be used with stdin:
38+
39+
```console
40+
$ cat deployment.yaml | conftest -
41+
testdata/deployment.yaml
42+
Containers must not run as root
43+
Deployments are not allowed
44+
```
45+
46+
3747
## Build
3848

3949
The only way of trying out `conftest` today is to build from source. For that you'll need

conftest.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"bufio"
45
"bytes"
56
"context"
67
"errors"
@@ -35,22 +36,24 @@ var RootCmd = &cobra.Command{
3536
Short: "Test your configuration files using Open Policy Agent",
3637
Version: fmt.Sprintf("Version: %s\nCommit: %s\nDate: %s\n", version, commit, date),
3738
Run: func(cmd *cobra.Command, args []string) {
39+
3840
if len(args) < 1 {
3941
cmd.SilenceErrors = true
4042
fmt.Println("The first argument should be a file")
4143
os.Exit(1)
4244
}
43-
cmd.SilenceUsage = true
4445

4546
compiler, err := buildCompiler(viper.GetString("policy"))
4647
if err != nil {
47-
fmt.Sprintf("Unable to find policies directory: %s", err)
48+
fmt.Printf("Unable to find policy directory: %s", err)
4849
os.Exit(1)
4950
}
5051

5152
foundFailures := false
5253
for _, fileName := range args {
53-
fmt.Println(fileName)
54+
if fileName != "-" {
55+
fmt.Println(fileName)
56+
}
5457
failures, warnings := processFile(fileName, compiler)
5558
if failures != nil {
5659
foundFailures = true
@@ -89,8 +92,18 @@ func detectLineBreak(haystack []byte) string {
8992
}
9093

9194
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+
94107
if err != nil {
95108
return fmt.Errorf("Unable to open file %s: %s", fileName, err), nil
96109
}
@@ -139,7 +152,7 @@ func makeQuery(query string, input interface{}, compiler *ast.Compiler) error {
139152
ctx := context.Background()
140153
rs, err := rego.Eval(ctx)
141154
if err != nil {
142-
return fmt.Errorf("Problem evaluating rego policies: %s", err)
155+
return fmt.Errorf("Problem evaluating rego policy: %s", err)
143156
}
144157

145158
var errorsList *multierror.Error

0 commit comments

Comments
 (0)