Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): error out when ignore file cannot be found #7624

Merged
merged 2 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pkg/flag/report_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/mattn/go-shellwords"
"github.com/samber/lo"
"github.com/spf13/viper"
"golang.org/x/xerrors"

dbTypes "github.com/aquasecurity/trivy-db/pkg/types"
Expand All @@ -14,6 +15,7 @@ import (
"github.com/aquasecurity/trivy/pkg/log"
"github.com/aquasecurity/trivy/pkg/result"
"github.com/aquasecurity/trivy/pkg/types"
"github.com/aquasecurity/trivy/pkg/utils/fsutils"
xstrings "github.com/aquasecurity/trivy/pkg/x/strings"
)

Expand Down Expand Up @@ -238,6 +240,10 @@ func (f *ReportFlagGroup) ToOptions() (ReportOptions, error) {
}
}

if viper.IsSet(f.IgnoreFile.ConfigName) && !fsutils.FileExists(f.IgnoreFile.Value()) {
return ReportOptions{}, xerrors.Errorf("ignore file not found: %s", f.IgnoreFile.Value())
}

return ReportOptions{
Format: format,
ReportFormat: f.ReportFormat.Value(),
Expand Down
12 changes: 12 additions & 0 deletions pkg/flag/report_flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,16 @@ func TestReportFlagGroup_ToOptions(t *testing.T) {
assert.Equal(t, tt.wantLogs, out.Messages(), tt.name)
})
}

t.Run("Error on non existing ignore file", func(t *testing.T) {
t.Cleanup(viper.Reset)

setValue(flag.IgnoreFileFlag.ConfigName, string("doesntexist"))
f := &flag.ReportFlagGroup{
IgnoreFile: flag.IgnoreFileFlag.Clone(),
}

_, err := f.ToOptions()
assert.ErrorContains(t, err, "ignore file not found: doesntexist")
})
}