2020-12-09 15:31:34 +00:00
|
|
|
package output
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/analyser"
|
|
|
|
)
|
|
|
|
|
|
|
|
const JSONOutputType = "json"
|
|
|
|
const JSONOutputExample = "json://PATH/TO/FILE.json"
|
|
|
|
|
|
|
|
type JSON struct {
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewJSON(path string) *JSON {
|
|
|
|
return &JSON{path}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *JSON) Write(analysis *analyser.Analysis) error {
|
2021-03-03 16:20:25 +00:00
|
|
|
file := os.Stdout
|
|
|
|
if !isStdOut(c.path) {
|
2021-03-30 15:43:21 +00:00
|
|
|
f, err := os.OpenFile(c.path, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0600)
|
2021-03-03 16:20:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
file = f
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
json, err := json.MarshalIndent(analysis, "", "\t")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := file.Write(json); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|