Skip to content

Commit

Permalink
Silence errors and usage helper upon pipeline failure
Browse files Browse the repository at this point in the history
  • Loading branch information
viktigpetterr committed Jan 19, 2023
1 parent 3147f75 commit 1a839d8
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
2 changes: 0 additions & 2 deletions cmd/debricked/main.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package main

import (
"fmt"
"github.com/debricked/cli/pkg/cmd/root"
"os"
)

func main() {
if err := root.NewRootCmd().Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
8 changes: 6 additions & 2 deletions pkg/cmd/scan/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ $ debricked scan . `+exampleFlags)
}

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

if scanCmdError != nil {
if scanCmdError == scan.FailPipelineErr {
cmd.SilenceUsage = true
cmd.SilenceErrors = true
return scanCmdError
} else if scanCmdError != nil {
return errors.New(fmt.Sprintf("%s %s\n", color.RedString("⨯"), scanCmdError.Error()))
}

Expand Down
32 changes: 29 additions & 3 deletions pkg/cmd/scan/scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/debricked/cli/pkg/client"
"github.com/debricked/cli/pkg/scan"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"strings"
"testing"
Expand Down Expand Up @@ -65,6 +66,25 @@ func TestRunENoPath(t *testing.T) {
}
}

func TestRunEFailPipelineErr(t *testing.T) {
var s scan.IScanner
mock := &scannerMock{}
mock.setErr(scan.FailPipelineErr)
s = mock
runE := RunE(&s)
cmd := &cobra.Command{}
err := runE(cmd, nil)
if err != scan.FailPipelineErr {
t.Error("failed to assert that scan. FailPipelineErr occurred. Error:", err)
}
if !cmd.SilenceUsage {
t.Error("failed to assert that usage was silenced")
}
if !cmd.SilenceErrors {
t.Error("failed to assert that errors were silenced")
}
}

func TestRunEError(t *testing.T) {
runE := RunE(nil)
err := runE(nil, []string{"."})
Expand All @@ -76,8 +96,14 @@ func TestRunEError(t *testing.T) {
}
}

type scannerMock struct{}
type scannerMock struct {
err error
}

func (s *scannerMock) Scan(_ scan.IOptions) error {
return s.err
}

func (*scannerMock) Scan(_ scan.IOptions) error {
return nil
func (s *scannerMock) setErr(err error) {
s.err = err
}
5 changes: 3 additions & 2 deletions pkg/scan/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import (
)

var (
BadOptsErr = errors.New("failed to type case IOptions")
BadOptsErr = errors.New("failed to type case IOptions")
FailPipelineErr = errors.New("")
)

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

return nil
Expand Down

0 comments on commit 1a839d8

Please sign in to comment.