Skip to content

Commit

Permalink
Merge pull request #171 from debricked/6621-verbose-flag
Browse files Browse the repository at this point in the history
Verbose Flag for Resolution
  • Loading branch information
ProgHaj authored Dec 20, 2023
2 parents 13cf6d0 + de433da commit 41801a6
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 26 deletions.
5 changes: 4 additions & 1 deletion internal/cmd/resolve/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import (
)

var exclusions = file.Exclusions()
var verbose bool

const (
ExclusionFlag = "exclusion"
VerboseFlag = "verbose"
)

func NewResolveCmd(resolver resolution.IResolver) *cobra.Command {
Expand Down Expand Up @@ -45,6 +47,7 @@ Exclude flags could alternatively be set using DEBRICKED_EXCLUSIONS="path1,path2
Example:
$ debricked resolve . `+exampleFlags)
cmd.Flags().BoolVar(&verbose, VerboseFlag, true, "set to false to disable extensive resolution error messages")

viper.MustBindEnv(ExclusionFlag)

Expand All @@ -56,7 +59,7 @@ func RunE(resolver resolution.IResolver) func(_ *cobra.Command, args []string) e
if len(args) == 0 {
args = append(args, ".")
}
_, err := resolver.Resolve(args, viper.GetStringSlice(ExclusionFlag))
_, err := resolver.Resolve(args, viper.GetStringSlice(ExclusionFlag), viper.GetBool(VerboseFlag))

return err
}
Expand Down
4 changes: 4 additions & 0 deletions internal/cmd/scan/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var commitAuthor string
var repositoryUrl string
var integrationName string
var exclusions = file.Exclusions()
var verbose bool
var noResolve bool
var noFingerprint bool
var passOnDowntime bool
Expand All @@ -34,6 +35,7 @@ const (
RepositoryUrlFlag = "repository-url"
IntegrationFlag = "integration"
ExclusionFlag = "exclusion"
VerboseFlag = "verbose"
NoResolveFlag = "no-resolve"
FingerprintFlag = "fingerprint"
PassOnTimeOut = "pass-on-timeout"
Expand Down Expand Up @@ -89,6 +91,7 @@ Exclude flags could alternatively be set using DEBRICKED_EXCLUSIONS="path1,path2
Examples:
$ debricked scan . `+exampleFlags)
cmd.Flags().BoolVar(&verbose, VerboseFlag, true, "set to false to disable extensive resolution error messages")
cmd.Flags().BoolVarP(&passOnDowntime, PassOnTimeOut, "p", false, "pass scan if there is a service access timeout")
cmd.Flags().BoolVar(&noResolve, NoResolveFlag, false, `disables resolution of manifest files that lack lock files. Resolving manifest files enables more accurate dependency scanning since the whole dependency tree will be analysed.
For example, if there is a "go.mod" in the target path, its dependencies are going to get resolved onto a lock file, and latter scanned.`)
Expand Down Expand Up @@ -120,6 +123,7 @@ func RunE(s *scan.IScanner) func(_ *cobra.Command, args []string) error {
Resolve: !viper.GetBool(NoResolveFlag),
Fingerprint: viper.GetBool(FingerprintFlag),
Exclusions: viper.GetStringSlice(ExclusionFlag),
Verbose: viper.GetBool(VerboseFlag),
RepositoryName: viper.GetString(RepositoryFlag),
CommitName: viper.GetString(CommitFlag),
BranchName: viper.GetString(BranchFlag),
Expand Down
6 changes: 3 additions & 3 deletions internal/resolution/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

type IResolver interface {
Resolve(paths []string, exclusions []string) (IResolution, error)
Resolve(paths []string, exclusions []string, verbose bool) (IResolution, error)
}

type Resolver struct {
Expand All @@ -36,7 +36,7 @@ func NewResolver(
}
}

func (r Resolver) Resolve(paths []string, exclusions []string) (IResolution, error) {
func (r Resolver) Resolve(paths []string, exclusions []string, verbose bool) (IResolution, error) {
files, err := r.refinePaths(paths, exclusions)
if err != nil {
return nil, err
Expand All @@ -60,7 +60,7 @@ func (r Resolver) Resolve(paths []string, exclusions []string) (IResolution, err

if resolution.HasErr() {
jobErrList := tui.NewJobsErrorList(os.Stdout, resolution.Jobs())
err = jobErrList.Render()
err = jobErrList.Render(verbose)
}

return resolution, err
Expand Down
20 changes: 10 additions & 10 deletions internal/resolution/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestResolve(t *testing.T) {
NewScheduler(workers),
)

res, err := r.Resolve([]string{"../../go.mod"}, nil)
res, err := r.Resolve([]string{"../../go.mod"}, nil, true)
assert.NotEmpty(t, res.Jobs())
assert.NoError(t, err)
}
Expand All @@ -53,7 +53,7 @@ func TestResolveInvokeError(t *testing.T) {
NewScheduler(workers),
)

_, err := r.Resolve([]string{"../../go.mod"}, nil)
_, err := r.Resolve([]string{"../../go.mod"}, nil, true)
assert.NotNil(t, err)
}

Expand All @@ -65,7 +65,7 @@ func TestResolveStrategyError(t *testing.T) {
NewScheduler(workers),
)

res, err := r.Resolve([]string{"../../go.mod"}, nil)
res, err := r.Resolve([]string{"../../go.mod"}, nil, true)
assert.Empty(t, res.Jobs())
assert.NoError(t, err)
}
Expand All @@ -79,7 +79,7 @@ func TestResolveScheduleError(t *testing.T) {
SchedulerMock{Err: errAssertion},
)

res, err := r.Resolve([]string{"../../go.mod"}, nil)
res, err := r.Resolve([]string{"../../go.mod"}, nil, true)
assert.NotEmpty(t, res.Jobs())
assert.ErrorIs(t, err, errAssertion)
}
Expand All @@ -92,7 +92,7 @@ func TestResolveDirWithoutManifestFiles(t *testing.T) {
SchedulerMock{},
)

res, err := r.Resolve([]string{"."}, nil)
res, err := r.Resolve([]string{"."}, nil, true)
assert.Empty(t, res.Jobs())
assert.NoError(t, err)
}
Expand All @@ -105,7 +105,7 @@ func TestResolveInvalidDir(t *testing.T) {
SchedulerMock{},
)

_, err := r.Resolve([]string{"invalid-dir"}, nil)
_, err := r.Resolve([]string{"invalid-dir"}, nil, true)
assert.Error(t, err)
}

Expand All @@ -121,7 +121,7 @@ func TestResolveGetGroupsErr(t *testing.T) {
SchedulerMock{},
)

_, err := r.Resolve([]string{"."}, nil)
_, err := r.Resolve([]string{"."}, nil, true)
assert.ErrorIs(t, testErr, err)
}

Expand Down Expand Up @@ -149,7 +149,7 @@ func TestResolveDirWithManifestFiles(t *testing.T) {

for _, dir := range cases {
t.Run(fmt.Sprintf("Case: %s", dir), func(t *testing.T) {
res, err := r.Resolve([]string{dir}, nil)
res, err := r.Resolve([]string{dir}, nil, true)
assert.Len(t, res.Jobs(), 1)
j := res.Jobs()[0]
assert.False(t, j.Errors().HasError())
Expand All @@ -172,7 +172,7 @@ func TestResolveDirWithExclusions(t *testing.T) {
SchedulerMock{},
)

res, err := r.Resolve([]string{"."}, []string{"dir"})
res, err := r.Resolve([]string{"."}, []string{"dir"}, true)

assert.Len(t, res.Jobs(), 1)
j := res.Jobs()[0]
Expand All @@ -199,7 +199,7 @@ func TestResolveHasResolutionErrs(t *testing.T) {
schedulerMock,
)

res, err := r.Resolve([]string{""}, []string{""})
res, err := r.Resolve([]string{""}, []string{""}, true)

assert.NoError(t, err)
assert.Len(t, res.Jobs(), 1)
Expand Down
2 changes: 1 addition & 1 deletion internal/resolution/testdata/resolver_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type ResolverMock struct {
files []string
}

func (r *ResolverMock) Resolve(_ []string, _ []string) (resolution.IResolution, error) {
func (r *ResolverMock) Resolve(_ []string, _ []string, _ bool) (resolution.IResolution, error) {
for _, f := range r.files {
createdFile, err := os.Create(f)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion internal/scan/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type DebrickedOptions struct {
Fingerprint bool
CallGraph bool
Exclusions []string
Verbose bool
RepositoryName string
CommitName string
BranchName string
Expand Down Expand Up @@ -132,7 +133,7 @@ func (dScanner *DebrickedScanner) Scan(o IOptions) error {

func (dScanner *DebrickedScanner) scanResolve(options DebrickedOptions) error {
if options.Resolve {
_, resErr := dScanner.resolver.Resolve([]string{options.Path}, options.Exclusions)
_, resErr := dScanner.resolver.Resolve([]string{options.Path}, options.Exclusions, options.Verbose)
if resErr != nil {
return resErr
}
Expand Down
16 changes: 11 additions & 5 deletions internal/tui/resolution_error_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func NewJobsErrorList(mirror io.Writer, jobs []job.IJob) JobsErrorList {
return JobsErrorList{mirror: mirror, jobs: jobs}
}

func (jobsErrList JobsErrorList) Render() error {
func (jobsErrList JobsErrorList) Render(verbose bool) error {
var listBuffer bytes.Buffer

formattedTitle := fmt.Sprintf("%s\n", color.BlueString(title))
Expand All @@ -32,15 +32,15 @@ func (jobsErrList JobsErrorList) Render() error {
listBuffer.Write([]byte(underlining))

for _, j := range jobsErrList.jobs {
jobsErrList.addJob(&listBuffer, j)
jobsErrList.addJob(&listBuffer, j, verbose)
}

_, err := jobsErrList.mirror.Write(listBuffer.Bytes())

return err
}

func (jobsErrList JobsErrorList) addJob(list *bytes.Buffer, job job.IJob) {
func (jobsErrList JobsErrorList) addJob(list *bytes.Buffer, job job.IJob, verbose bool) {
var jobString string
if !job.Errors().HasError() {
return
Expand All @@ -49,7 +49,10 @@ func (jobsErrList JobsErrorList) addJob(list *bytes.Buffer, job job.IJob) {
list.Write([]byte(fmt.Sprintf("%s\n", color.YellowString(job.GetFile()))))

for _, warning := range job.Errors().GetWarningErrors() {
err := jobsErrList.createErrorString(warning, true)
err := ""
if verbose {
err = jobsErrList.createErrorString(warning, true)
}
cmd := warning.Command()
doc := warning.Documentation()
status := warning.Status()
Expand All @@ -65,7 +68,10 @@ func (jobsErrList JobsErrorList) addJob(list *bytes.Buffer, job job.IJob) {
}

for _, critical := range job.Errors().GetCriticalErrors() {
err := jobsErrList.createErrorString(critical, false)
err := ""
if verbose {
err = jobsErrList.createErrorString(critical, false)
}
cmd := critical.Command()
doc := critical.Documentation()
status := critical.Status()
Expand Down
31 changes: 26 additions & 5 deletions internal/tui/resolution_error_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestRenderNoJobs(t *testing.T) {
var listBuffer bytes.Buffer
errList := NewJobsErrorList(&listBuffer, []job.IJob{})

err := errList.Render()
err := errList.Render(true)

assert.NoError(t, err)
output := listBuffer.String()
Expand All @@ -35,7 +35,7 @@ func TestRenderWarningJob(t *testing.T) {
jobMock.Errors().Warning(warningErr)
errList := NewJobsErrorList(&listBuffer, []job.IJob{jobMock})

err := errList.Render()
err := errList.Render(true)

assert.NoError(t, err)
output := listBuffer.String()
Expand All @@ -57,7 +57,7 @@ func TestRenderCriticalJob(t *testing.T) {
jobMock.Errors().Critical(warningErr)
errList := NewJobsErrorList(&listBuffer, []job.IJob{jobMock})

err := errList.Render()
err := errList.Render(true)

assert.NoError(t, err)
output := listBuffer.String()
Expand All @@ -71,6 +71,27 @@ func TestRenderCriticalJob(t *testing.T) {
assertOutput(t, output, contains)
}

func TestRenderCriticalJobNoVerbose(t *testing.T) {
var listBuffer bytes.Buffer

warningErr := job.NewBaseJobError("critical-message")
jobMock := testdata.NewJobMock("file")
jobMock.Errors().Critical(warningErr)
errList := NewJobsErrorList(&listBuffer, []job.IJob{jobMock})

err := errList.Render(false)

assert.NoError(t, err)
output := listBuffer.String()
contains := []string{
"file",
"\n* ",
"Critical",
}
assertOutput(t, output, contains)
assert.NotContains(t, output, "critical-message\n")
}

func TestRenderCriticalAndWarningJob(t *testing.T) {
var listBuffer bytes.Buffer

Expand All @@ -84,7 +105,7 @@ func TestRenderCriticalAndWarningJob(t *testing.T) {

errList := NewJobsErrorList(&listBuffer, []job.IJob{jobMock})

err := errList.Render()
err := errList.Render(true)

assert.NoError(t, err)
output := listBuffer.String()
Expand Down Expand Up @@ -112,7 +133,7 @@ func TestRenderCriticalAndWorkingJob(t *testing.T) {

errList := NewJobsErrorList(&listBuffer, []job.IJob{jobWithErrMock, jobWorkingMock})

err := errList.Render()
err := errList.Render(true)

assert.NoError(t, err)
output := listBuffer.String()
Expand Down

0 comments on commit 41801a6

Please sign in to comment.