Skip to content

Commit

Permalink
Merge pull request #1377 from snyk/feat/improve_snyk_piping
Browse files Browse the repository at this point in the history
Improve snyk piping
  • Loading branch information
eliecharra authored Mar 9, 2022
2 parents 5bc324f + b3986d9 commit 856df94
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 21 deletions.
9 changes: 9 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/snyk/driftctl/pkg/config"
"github.com/snyk/driftctl/pkg/version"
"github.com/snyk/driftctl/sentry"
"github.com/spf13/viper"
)

func init() {
Expand All @@ -33,12 +34,20 @@ func run() int {
config.Init()
logger.Init()
build := build.Build{}
// Check whether driftCTL is run under Snyk CLI
isSnyk := viper.GetBool("IS_SNYK")
logrus.WithFields(logrus.Fields{
"isRelease": fmt.Sprintf("%t", build.IsRelease()),
"isUsageReportingEnabled": fmt.Sprintf("%t", build.IsUsageReportingEnabled()),
"version": version.Current(),
"isSnyk": fmt.Sprintf("%t", isSnyk),
}).Debug("Build info")

// Enable colorization when driftctl is launched under snyk cli (piped)
if isSnyk {
color.NoColor = false
}

driftctlCmd := cmd.NewDriftctlCmd(build)

checkVersion := driftctlCmd.ShouldCheckVersion()
Expand Down
9 changes: 0 additions & 9 deletions pkg/cmd/scan/output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,10 @@ func GetPrinter(config OutputConfig, quiet bool) output.Printer {

switch config.Key {
case JSONOutputType:
if isStdOut(config.Path) {
return &output.VoidPrinter{}
}
fallthrough
case PlanOutputType:
if isStdOut(config.Path) {
return &output.VoidPrinter{}
}
fallthrough
case HTMLOutputType:
if isStdOut(config.Path) {
return &output.VoidPrinter{}
}
fallthrough
case ConsoleOutputType:
fallthrough
Expand Down
31 changes: 21 additions & 10 deletions pkg/cmd/scan/output/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -573,13 +573,13 @@ func TestGetPrinter(t *testing.T) {
name: "json stdout output",
path: "stdout",
key: JSONOutputType,
want: &output.VoidPrinter{},
want: &output.ConsolePrinter{},
},
{
name: "json /dev/stdout output",
path: "/dev/stdout",
key: JSONOutputType,
want: &output.VoidPrinter{},
want: &output.ConsolePrinter{},
},
{
name: "console stdout output",
Expand All @@ -604,25 +604,25 @@ func TestGetPrinter(t *testing.T) {
name: "jsonplan stdout output",
path: "stdout",
key: PlanOutputType,
want: &output.VoidPrinter{},
want: &output.ConsolePrinter{},
},
{
name: "jsonplan /dev/stdout output",
path: "/dev/stdout",
key: PlanOutputType,
want: &output.VoidPrinter{},
want: &output.ConsolePrinter{},
},
{
name: "html stdout output",
path: "stdout",
key: HTMLOutputType,
want: &output.VoidPrinter{},
want: &output.ConsolePrinter{},
},
{
name: "html /dev/stdout output",
path: "/dev/stdout",
key: HTMLOutputType,
want: &output.VoidPrinter{},
want: &output.ConsolePrinter{},
},
}
for _, tt := range tests {
Expand All @@ -645,14 +645,14 @@ func TestShouldPrint(t *testing.T) {
want bool
}{
{
name: "test stdout prevents printing",
name: "test stdout should not prevents printing",
outputs: []OutputConfig{
{
Path: "stdout",
Key: JSONOutputType,
},
},
want: false,
want: true,
},
{
name: "test output to file doesn't prevent printing",
Expand All @@ -665,7 +665,18 @@ func TestShouldPrint(t *testing.T) {
want: true,
},
{
name: "test stdout prevents printing",
name: "test quiet should prevents printing",
outputs: []OutputConfig{
{
Path: "result.json",
Key: JSONOutputType,
},
},
quiet: true,
want: false,
},
{
name: "test stdout should not prevents printing",
outputs: []OutputConfig{
{
Path: "result.json",
Expand All @@ -676,7 +687,7 @@ func TestShouldPrint(t *testing.T) {
Key: PlanOutputType,
},
},
want: false,
want: true,
},
}
for _, tt := range tests {
Expand Down
7 changes: 5 additions & 2 deletions pkg/output/printer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package output

import "fmt"
import (
"fmt"
"os"
)

var globalPrinter Printer = &VoidPrinter{}

Expand All @@ -23,7 +26,7 @@ func NewConsolePrinter() *ConsolePrinter {
}

func (c *ConsolePrinter) Printf(format string, args ...interface{}) {
fmt.Printf(format, args...)
_, _ = fmt.Fprintf(os.Stderr, format, args...)
}

type VoidPrinter struct{}
Expand Down

0 comments on commit 856df94

Please sign in to comment.