Skip to content

Commit 1a839d8

Browse files
committed
Silence errors and usage helper upon pipeline failure
1 parent 3147f75 commit 1a839d8

File tree

4 files changed

+38
-9
lines changed

4 files changed

+38
-9
lines changed

cmd/debricked/main.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package main
22

33
import (
4-
"fmt"
54
"github.com/debricked/cli/pkg/cmd/root"
65
"os"
76
)
87

98
func main() {
109
if err := root.NewRootCmd().Execute(); err != nil {
11-
fmt.Println(err)
1210
os.Exit(1)
1311
}
1412
}

pkg/cmd/scan/scan.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ $ debricked scan . `+exampleFlags)
8282
}
8383

8484
func RunE(s *scan.IScanner) func(_ *cobra.Command, args []string) error {
85-
return func(_ *cobra.Command, args []string) error {
85+
return func(cmd *cobra.Command, args []string) error {
8686
path := ""
8787
if len(args) > 0 {
8888
path = args[0]
@@ -103,7 +103,11 @@ func RunE(s *scan.IScanner) func(_ *cobra.Command, args []string) error {
103103
scanCmdError = errors.New("scanner was nil")
104104
}
105105

106-
if scanCmdError != nil {
106+
if scanCmdError == scan.FailPipelineErr {
107+
cmd.SilenceUsage = true
108+
cmd.SilenceErrors = true
109+
return scanCmdError
110+
} else if scanCmdError != nil {
107111
return errors.New(fmt.Sprintf("%s %s\n", color.RedString("⨯"), scanCmdError.Error()))
108112
}
109113

pkg/cmd/scan/scan_test.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"github.com/debricked/cli/pkg/client"
66
"github.com/debricked/cli/pkg/scan"
7+
"github.com/spf13/cobra"
78
"github.com/spf13/viper"
89
"strings"
910
"testing"
@@ -65,6 +66,25 @@ func TestRunENoPath(t *testing.T) {
6566
}
6667
}
6768

69+
func TestRunEFailPipelineErr(t *testing.T) {
70+
var s scan.IScanner
71+
mock := &scannerMock{}
72+
mock.setErr(scan.FailPipelineErr)
73+
s = mock
74+
runE := RunE(&s)
75+
cmd := &cobra.Command{}
76+
err := runE(cmd, nil)
77+
if err != scan.FailPipelineErr {
78+
t.Error("failed to assert that scan. FailPipelineErr occurred. Error:", err)
79+
}
80+
if !cmd.SilenceUsage {
81+
t.Error("failed to assert that usage was silenced")
82+
}
83+
if !cmd.SilenceErrors {
84+
t.Error("failed to assert that errors were silenced")
85+
}
86+
}
87+
6888
func TestRunEError(t *testing.T) {
6989
runE := RunE(nil)
7090
err := runE(nil, []string{"."})
@@ -76,8 +96,14 @@ func TestRunEError(t *testing.T) {
7696
}
7797
}
7898

79-
type scannerMock struct{}
99+
type scannerMock struct {
100+
err error
101+
}
102+
103+
func (s *scannerMock) Scan(_ scan.IOptions) error {
104+
return s.err
105+
}
80106

81-
func (*scannerMock) Scan(_ scan.IOptions) error {
82-
return nil
107+
func (s *scannerMock) setErr(err error) {
108+
s.err = err
83109
}

pkg/scan/scanner.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ import (
1515
)
1616

1717
var (
18-
BadOptsErr = errors.New("failed to type case IOptions")
18+
BadOptsErr = errors.New("failed to type case IOptions")
19+
FailPipelineErr = errors.New("")
1920
)
2021

2122
type IScanner interface {
@@ -108,7 +109,7 @@ func (dScanner *DebrickedScanner) Scan(o IOptions) error {
108109
}
109110
fmt.Printf("For full details, visit: %s\n\n", color.BlueString(result.DetailsUrl))
110111
if failPipeline {
111-
return errors.New("")
112+
return FailPipelineErr
112113
}
113114

114115
return nil

0 commit comments

Comments
 (0)