Skip to content

Commit

Permalink
clean up flag handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jjarboe committed Oct 1, 2020
1 parent c2a3b43 commit 1a35277
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 35 deletions.
16 changes: 16 additions & 0 deletions pkg/cli/output_writer.go
Original file line number Diff line number Diff line change
@@ -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
}
34 changes: 2 additions & 32 deletions pkg/cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down
28 changes: 25 additions & 3 deletions pkg/cli/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
package cli

import (
"os"
"fmt"
"strings"

iacProvider "github.com/accurics/terrascan/pkg/iac-providers"
"github.com/accurics/terrascan/pkg/policy"
"github.com/spf13/cobra"
"go.uber.org/zap"
"github.com/mattn/go-isatty"
)

var (
Expand All @@ -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{
Expand All @@ -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,
Expand All @@ -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)
}

0 comments on commit 1a35277

Please sign in to comment.