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

97 lines
1.9 KiB
Go
Raw Normal View History

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,
2021-04-21 16:51:58 +00:00
HTMLOutputType,
2021-06-09 14:06:21 +00:00
PlanOutputType,
}
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,
}
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
}
func GetOutput(config OutputConfig, quiet bool) Output {
output.ChangePrinter(GetPrinter(config, quiet))
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
default:
return NewConsole()
}
}
func GetPrinter(config OutputConfig, quiet bool) output.Printer {
if quiet {
return &output.VoidPrinter{}
}
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
case ConsoleOutputType:
fallthrough
default:
return output.NewConsolePrinter()
}
}
func isStdOut(path string) bool {
return path == "/dev/stdout" || path == "stdout"
}