2020-12-09 15:31:34 +00:00
|
|
|
package output
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/analyser"
|
2021-03-03 16:20:25 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/output"
|
2020-12-09 15:31:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Output interface {
|
|
|
|
Write(analysis *analyser.Analysis) error
|
|
|
|
}
|
|
|
|
|
|
|
|
var supportedOutputTypes = []string{
|
|
|
|
ConsoleOutputType,
|
|
|
|
JSONOutputType,
|
2021-04-21 16:51:58 +00:00
|
|
|
HTMLOutputType,
|
2021-06-09 14:06:21 +00:00
|
|
|
PlanOutputType,
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var supportedOutputExample = map[string]string{
|
|
|
|
ConsoleOutputType: ConsoleOutputExample,
|
|
|
|
JSONOutputType: JSONOutputExample,
|
2021-04-21 16:51:58 +00:00
|
|
|
HTMLOutputType: HTMLOutputExample,
|
2021-06-09 14:06:21 +00:00
|
|
|
PlanOutputType: PlanOutputExample,
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func SupportedOutputs() []string {
|
|
|
|
return supportedOutputTypes
|
|
|
|
}
|
|
|
|
|
|
|
|
func SupportedOutputsExample() []string {
|
|
|
|
examples := make([]string, 0, len(supportedOutputExample))
|
|
|
|
for _, ex := range supportedOutputExample {
|
|
|
|
examples = append(examples, ex)
|
|
|
|
}
|
|
|
|
sort.Strings(examples)
|
|
|
|
return examples
|
|
|
|
}
|
|
|
|
|
|
|
|
func Example(key string) string {
|
|
|
|
return supportedOutputExample[key]
|
|
|
|
}
|
|
|
|
|
|
|
|
func IsSupported(key string) bool {
|
|
|
|
for _, o := range supportedOutputTypes {
|
|
|
|
if o == key {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-03-15 17:30:18 +00:00
|
|
|
func GetOutput(config OutputConfig, quiet bool) Output {
|
|
|
|
output.ChangePrinter(GetPrinter(config, quiet))
|
2021-03-03 16:20:25 +00:00
|
|
|
|
2020-12-09 15:31:34 +00:00
|
|
|
switch config.Key {
|
|
|
|
case JSONOutputType:
|
|
|
|
return NewJSON(config.Options["path"])
|
2021-04-21 16:51:58 +00:00
|
|
|
case HTMLOutputType:
|
|
|
|
return NewHTML(config.Options["path"])
|
2021-06-09 14:06:21 +00:00
|
|
|
case PlanOutputType:
|
|
|
|
return NewPlan(config.Options["path"])
|
2021-06-14 14:38:54 +00:00
|
|
|
case ConsoleOutputType:
|
|
|
|
fallthrough
|
2020-12-09 15:31:34 +00:00
|
|
|
default:
|
|
|
|
return NewConsole()
|
|
|
|
}
|
|
|
|
}
|
2021-03-03 16:20:25 +00:00
|
|
|
|
2021-03-15 17:30:18 +00:00
|
|
|
func GetPrinter(config OutputConfig, quiet bool) output.Printer {
|
|
|
|
if quiet {
|
|
|
|
return &output.VoidPrinter{}
|
|
|
|
}
|
|
|
|
|
2021-03-03 16:20:25 +00:00
|
|
|
switch config.Key {
|
|
|
|
case JSONOutputType:
|
|
|
|
if isStdOut(config.Options["path"]) {
|
|
|
|
return &output.VoidPrinter{}
|
|
|
|
}
|
|
|
|
fallthrough
|
2021-06-09 14:06:21 +00:00
|
|
|
case PlanOutputType:
|
|
|
|
if isStdOut(config.Options["path"]) {
|
|
|
|
return &output.VoidPrinter{}
|
|
|
|
}
|
|
|
|
fallthrough
|
2021-03-03 16:20:25 +00:00
|
|
|
case ConsoleOutputType:
|
|
|
|
fallthrough
|
|
|
|
default:
|
|
|
|
return output.NewConsolePrinter()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func isStdOut(path string) bool {
|
|
|
|
return path == "/dev/stdout" || path == "stdout"
|
|
|
|
}
|