diff --git a/pkg/cli/output_writer.go b/pkg/cli/output_writer.go new file mode 100644 index 000000000..9b07e3845 --- /dev/null +++ b/pkg/cli/output_writer.go @@ -0,0 +1,16 @@ +package cli + +import ( + "io" + "os" + "github.com/accurics/terrascan/pkg/termcolor" +) + +func NewOutputWriter(useColors bool) io.Writer { + + // Color codes will corrupt output, so suppress if not on terminal + if useColors == true { + return termcolor.NewColorizedWriter(os.Stdout) + } + return os.Stdout +} diff --git a/pkg/cli/run.go b/pkg/cli/run.go index b0885db35..4b247571b 100644 --- a/pkg/cli/run.go +++ b/pkg/cli/run.go @@ -19,19 +19,14 @@ package cli import ( "flag" "os" - "io" - "strings" - - "github.com/mattn/go-isatty" "github.com/accurics/terrascan/pkg/runtime" "github.com/accurics/terrascan/pkg/writer" - "github.com/accurics/terrascan/pkg/termcolor" ) // Run executes terrascan in CLI mode func Run(iacType, iacVersion, cloudType, iacFilePath, iacDirPath, configFile, - policyPath, format string, configOnly bool, useColors string) { + policyPath, format string, configOnly bool, useColors bool) { // create a new runtime executor for processing IaC executor, err := runtime.NewExecutor(iacType, iacVersion, cloudType, iacFilePath, @@ -46,32 +41,7 @@ func Run(iacType, iacVersion, cloudType, iacFilePath, iacDirPath, configFile, return } - // Color codes will corrupt output, so suppress if not on terminal - switch strings.ToLower(useColors) { - case "auto": - if isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd()) { - useColors = "true" - } else { - useColors = "false" - } - - case "true": // nothing to do - case "t": fallthrough - case "y": fallthrough - case "1": fallthrough - case "force": - useColors = "true" - - default: - useColors = "false" - } - - var outputWriter io.Writer - if useColors == "true" { - outputWriter = termcolor.NewColorizedWriter(os.Stdout) - } else { - outputWriter = os.Stdout - } + outputWriter := NewOutputWriter(useColors) if configOnly { writer.Write(format, results.ResourceConfig, outputWriter) diff --git a/pkg/cli/scan.go b/pkg/cli/scan.go index e5142e852..7050f17f0 100644 --- a/pkg/cli/scan.go +++ b/pkg/cli/scan.go @@ -17,6 +17,7 @@ package cli import ( + "os" "fmt" "strings" @@ -24,6 +25,7 @@ import ( "github.com/accurics/terrascan/pkg/policy" "github.com/spf13/cobra" "go.uber.org/zap" + "github.com/mattn/go-isatty" ) var ( @@ -41,8 +43,9 @@ var ( IacDirPath string //ConfigOnly will output resource config (should only be used for debugging purposes) ConfigOnly bool - // UseColors enables color output (t, f, auto) - UseColors string + // UseColors indicates whether to use color output + UseColors bool + useColors string // used for flag processing ) var scanCmd = &cobra.Command{ @@ -53,6 +56,24 @@ var scanCmd = &cobra.Command{ Detect compliance and security violations across Infrastructure as Code to mitigate risk before provisioning cloud native infrastructure. `, PreRun: func(cmd *cobra.Command, args []string) { + switch strings.ToLower(useColors) { + case "auto": + if isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd()) { + UseColors = true + } else { + UseColors = false + } + + case "true": fallthrough + case "t": fallthrough + case "y": fallthrough + case "1": fallthrough + case "force": + UseColors = true + + default: + UseColors = false + } initial(cmd, args) }, Run: scan, @@ -71,7 +92,8 @@ func init() { scanCmd.Flags().StringVarP(&IacDirPath, "iac-dir", "d", ".", "path to a directory containing one or more IaC files") scanCmd.Flags().StringVarP(&PolicyPath, "policy-path", "p", "", "policy path directory") scanCmd.Flags().BoolVarP(&ConfigOnly, "config-only", "", false, "will output resource config (should only be used for debugging purposes)") - scanCmd.Flags().StringVar(&UseColors, "use-colors", "auto", "color output (auto, t, f)") + // flag passes a string, but we normalize to bool in PreRun + scanCmd.Flags().StringVar(&useColors, "use-colors", "auto", "color output (auto, t, f)") scanCmd.MarkFlagRequired("policy-type") RegisterCommand(rootCmd, scanCmd) }