Skip to content

Commit 856df94

Browse files
authored
Merge pull request #1377 from snyk/feat/improve_snyk_piping
Improve snyk piping
2 parents 5bc324f + b3986d9 commit 856df94

File tree

4 files changed

+35
-21
lines changed

4 files changed

+35
-21
lines changed

main.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/snyk/driftctl/pkg/config"
1818
"github.com/snyk/driftctl/pkg/version"
1919
"github.com/snyk/driftctl/sentry"
20+
"github.com/spf13/viper"
2021
)
2122

2223
func init() {
@@ -33,12 +34,20 @@ func run() int {
3334
config.Init()
3435
logger.Init()
3536
build := build.Build{}
37+
// Check whether driftCTL is run under Snyk CLI
38+
isSnyk := viper.GetBool("IS_SNYK")
3639
logrus.WithFields(logrus.Fields{
3740
"isRelease": fmt.Sprintf("%t", build.IsRelease()),
3841
"isUsageReportingEnabled": fmt.Sprintf("%t", build.IsUsageReportingEnabled()),
3942
"version": version.Current(),
43+
"isSnyk": fmt.Sprintf("%t", isSnyk),
4044
}).Debug("Build info")
4145

46+
// Enable colorization when driftctl is launched under snyk cli (piped)
47+
if isSnyk {
48+
color.NoColor = false
49+
}
50+
4251
driftctlCmd := cmd.NewDriftctlCmd(build)
4352

4453
checkVersion := driftctlCmd.ShouldCheckVersion()

pkg/cmd/scan/output/output.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,10 @@ func GetPrinter(config OutputConfig, quiet bool) output.Printer {
8080

8181
switch config.Key {
8282
case JSONOutputType:
83-
if isStdOut(config.Path) {
84-
return &output.VoidPrinter{}
85-
}
8683
fallthrough
8784
case PlanOutputType:
88-
if isStdOut(config.Path) {
89-
return &output.VoidPrinter{}
90-
}
9185
fallthrough
9286
case HTMLOutputType:
93-
if isStdOut(config.Path) {
94-
return &output.VoidPrinter{}
95-
}
9687
fallthrough
9788
case ConsoleOutputType:
9889
fallthrough

pkg/cmd/scan/output/output_test.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -573,13 +573,13 @@ func TestGetPrinter(t *testing.T) {
573573
name: "json stdout output",
574574
path: "stdout",
575575
key: JSONOutputType,
576-
want: &output.VoidPrinter{},
576+
want: &output.ConsolePrinter{},
577577
},
578578
{
579579
name: "json /dev/stdout output",
580580
path: "/dev/stdout",
581581
key: JSONOutputType,
582-
want: &output.VoidPrinter{},
582+
want: &output.ConsolePrinter{},
583583
},
584584
{
585585
name: "console stdout output",
@@ -604,25 +604,25 @@ func TestGetPrinter(t *testing.T) {
604604
name: "jsonplan stdout output",
605605
path: "stdout",
606606
key: PlanOutputType,
607-
want: &output.VoidPrinter{},
607+
want: &output.ConsolePrinter{},
608608
},
609609
{
610610
name: "jsonplan /dev/stdout output",
611611
path: "/dev/stdout",
612612
key: PlanOutputType,
613-
want: &output.VoidPrinter{},
613+
want: &output.ConsolePrinter{},
614614
},
615615
{
616616
name: "html stdout output",
617617
path: "stdout",
618618
key: HTMLOutputType,
619-
want: &output.VoidPrinter{},
619+
want: &output.ConsolePrinter{},
620620
},
621621
{
622622
name: "html /dev/stdout output",
623623
path: "/dev/stdout",
624624
key: HTMLOutputType,
625-
want: &output.VoidPrinter{},
625+
want: &output.ConsolePrinter{},
626626
},
627627
}
628628
for _, tt := range tests {
@@ -645,14 +645,14 @@ func TestShouldPrint(t *testing.T) {
645645
want bool
646646
}{
647647
{
648-
name: "test stdout prevents printing",
648+
name: "test stdout should not prevents printing",
649649
outputs: []OutputConfig{
650650
{
651651
Path: "stdout",
652652
Key: JSONOutputType,
653653
},
654654
},
655-
want: false,
655+
want: true,
656656
},
657657
{
658658
name: "test output to file doesn't prevent printing",
@@ -665,7 +665,18 @@ func TestShouldPrint(t *testing.T) {
665665
want: true,
666666
},
667667
{
668-
name: "test stdout prevents printing",
668+
name: "test quiet should prevents printing",
669+
outputs: []OutputConfig{
670+
{
671+
Path: "result.json",
672+
Key: JSONOutputType,
673+
},
674+
},
675+
quiet: true,
676+
want: false,
677+
},
678+
{
679+
name: "test stdout should not prevents printing",
669680
outputs: []OutputConfig{
670681
{
671682
Path: "result.json",
@@ -676,7 +687,7 @@ func TestShouldPrint(t *testing.T) {
676687
Key: PlanOutputType,
677688
},
678689
},
679-
want: false,
690+
want: true,
680691
},
681692
}
682693
for _, tt := range tests {

pkg/output/printer.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package output
22

3-
import "fmt"
3+
import (
4+
"fmt"
5+
"os"
6+
)
47

58
var globalPrinter Printer = &VoidPrinter{}
69

@@ -23,7 +26,7 @@ func NewConsolePrinter() *ConsolePrinter {
2326
}
2427

2528
func (c *ConsolePrinter) Printf(format string, args ...interface{}) {
26-
fmt.Printf(format, args...)
29+
_, _ = fmt.Fprintf(os.Stderr, format, args...)
2730
}
2831

2932
type VoidPrinter struct{}

0 commit comments

Comments
 (0)