Skip to content

Commit

Permalink
add xml writer support
Browse files Browse the repository at this point in the history
  • Loading branch information
Yusuf Kanchwala authored and cesar-rodriguez committed Aug 14, 2020
1 parent c5ecf5b commit 1ff02e8
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 6 deletions.
4 changes: 3 additions & 1 deletion pkg/writer/json.go
Expand Up @@ -19,6 +19,8 @@ package writer
import (
"encoding/json"
"io"

"github.com/accurics/terrascan/pkg/policy"
)

const (
Expand All @@ -30,7 +32,7 @@ func init() {
}

// JSONWriter prints data in JSON format
func JSONWriter(data interface{}, writer io.Writer) error {
func JSONWriter(data policy.EngineOutput, writer io.Writer) error {
j, _ := json.MarshalIndent(data, "", " ")
writer.Write(j)
writer.Write([]byte{'\n'})
Expand Down
10 changes: 7 additions & 3 deletions pkg/writer/register.go
Expand Up @@ -16,15 +16,19 @@

package writer

import "io"
import (
"io"

"github.com/accurics/terrascan/pkg/policy"
)

// supportedFormat data type for supported formats
type supportedFormat string

// writerMap stores mapping of supported writer formats with respective functions
var writerMap = make(map[supportedFormat](func(interface{}, io.Writer) error))
var writerMap = make(map[supportedFormat](func(policy.EngineOutput, io.Writer) error))

// RegisterWriter registers a writer for terrascan
func RegisterWriter(format supportedFormat, writerFunc func(interface{}, io.Writer) error) {
func RegisterWriter(format supportedFormat, writerFunc func(policy.EngineOutput, io.Writer) error) {
writerMap[format] = writerFunc
}
3 changes: 2 additions & 1 deletion pkg/writer/writer.go
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"io"

"github.com/accurics/terrascan/pkg/policy"
"go.uber.org/zap"
)

Expand All @@ -28,7 +29,7 @@ var (
)

// Write method writes in the given format using the respective writer func
func Write(format string, data interface{}, writer io.Writer) error {
func Write(format string, data policy.EngineOutput, writer io.Writer) error {

writerFunc, present := writerMap[supportedFormat(format)]
if !present {
Expand Down
40 changes: 40 additions & 0 deletions pkg/writer/xml.go
@@ -0,0 +1,40 @@
/*
Copyright (C) 2020 Accurics, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package writer

import (
"encoding/xml"
"io"

"github.com/accurics/terrascan/pkg/policy"
)

const (
xmlFormat supportedFormat = "xml"
)

func init() {
RegisterWriter(xmlFormat, XMLWriter)
}

// XMLWriter prints data in XML format
func XMLWriter(data policy.EngineOutput, writer io.Writer) error {
j, _ := xml.MarshalIndent(data, "", " ")
writer.Write(j)
writer.Write([]byte{'\n'})
return nil
}
3 changes: 2 additions & 1 deletion pkg/writer/yaml.go
Expand Up @@ -19,6 +19,7 @@ package writer
import (
"io"

"github.com/accurics/terrascan/pkg/policy"
"gopkg.in/yaml.v2"
)

Expand All @@ -31,7 +32,7 @@ func init() {
}

// YAMLWriter prints data in YAML format
func YAMLWriter(data interface{}, writer io.Writer) error {
func YAMLWriter(data policy.EngineOutput, writer io.Writer) error {
j, _ := yaml.Marshal(data)
writer.Write(j)
writer.Write([]byte{'\n'})
Expand Down

0 comments on commit 1ff02e8

Please sign in to comment.