driftctl/pkg/cmd/scan/output/output.go

93 lines
1.8 KiB
Go

package output
import (
"sort"
"github.com/cloudskiff/driftctl/pkg/analyser"
"github.com/cloudskiff/driftctl/pkg/output"
)
type Output interface {
Write(analysis *analyser.Analysis) error
}
var supportedOutputTypes = []string{
ConsoleOutputType,
JSONOutputType,
HTMLOutputType,
PlanOutputType,
}
var supportedOutputExample = map[string]string{
ConsoleOutputType: ConsoleOutputExample,
JSONOutputType: JSONOutputExample,
HTMLOutputType: HTMLOutputExample,
PlanOutputType: PlanOutputExample,
}
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
}
func GetOutput(config OutputConfig, quiet bool) Output {
output.ChangePrinter(GetPrinter(config, quiet))
switch config.Key {
case JSONOutputType:
return NewJSON(config.Path)
case HTMLOutputType:
return NewHTML(config.Path)
case PlanOutputType:
return NewPlan(config.Path)
case ConsoleOutputType:
fallthrough
default:
return NewConsole()
}
}
func GetPrinter(config OutputConfig, quiet bool) output.Printer {
if quiet {
return &output.VoidPrinter{}
}
switch config.Key {
case JSONOutputType:
if isStdOut(config.Path) {
return &output.VoidPrinter{}
}
fallthrough
case PlanOutputType:
if isStdOut(config.Path) {
return &output.VoidPrinter{}
}
fallthrough
case ConsoleOutputType:
fallthrough
default:
return output.NewConsolePrinter()
}
}
func isStdOut(path string) bool {
return path == "/dev/stdout" || path == "stdout"
}