-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add json output support (#264)
* feat: add json output support Signed-off-by: Charles-Edouard Brétéché <[email protected]> * codegen Signed-off-by: Charles-Edouard Brétéché <[email protected]> --------- Signed-off-by: Charles-Edouard Brétéché <[email protected]>
- Loading branch information
1 parent
f5e3325
commit c42cc9b
Showing
4 changed files
with
60 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package scan | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
|
||
jsonengine "github.com/kyverno/kyverno-json/pkg/json-engine" | ||
) | ||
|
||
type output interface { | ||
println(args ...any) | ||
responses(responses ...jsonengine.RuleResponse) | ||
} | ||
|
||
type textOutput struct { | ||
out io.Writer | ||
} | ||
|
||
func (t *textOutput) println(args ...any) { | ||
fmt.Fprintln(t.out, args...) | ||
} | ||
|
||
func (t *textOutput) responses(responses ...jsonengine.RuleResponse) { | ||
} | ||
|
||
type jsonOutput struct { | ||
out io.Writer | ||
} | ||
|
||
func (t *jsonOutput) println(args ...any) { | ||
} | ||
|
||
func (t *jsonOutput) responses(responses ...jsonengine.RuleResponse) { | ||
payload, err := json.MarshalIndent(&jsonengine.Response{Results: responses}, "", " ") | ||
if err != nil { | ||
fmt.Fprintln(t.out, err) | ||
} else { | ||
fmt.Fprintln(t.out, string(payload)) | ||
} | ||
} | ||
|
||
func newOutput(out io.Writer, format string) output { | ||
if format == "json" { | ||
return &jsonOutput{out: out} | ||
} | ||
return &textOutput{out: out} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters