Skip to content
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
15 changes: 13 additions & 2 deletions pkg/notification/notice.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"
"time"

"github.com/aquasecurity/go-version/pkg/semver"
"github.com/aquasecurity/trivy/pkg/log"
"github.com/aquasecurity/trivy/pkg/version/app"
)
Expand Down Expand Up @@ -140,8 +141,18 @@ func (v *VersionChecker) PrintNotices(output io.Writer) {
}
}

if v.currentVersion != v.LatestVersion() {
notices = append(notices, fmt.Sprintf("Version %s of Trivy is now available, current version is %s", v.latestVersion.Trivy.LatestVersion, v.currentVersion))
cv, err := semver.Parse(strings.TrimPrefix(v.currentVersion, "v"))
Copy link
Member Author

@simar7 simar7 May 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

semver expects no "v" prefix. Example: https://go.dev/play/p/knVQqzi3yNl

I just added it in case we ever erroneously made the server side change to include the prefix, this would ensure that it won't matter.

if err != nil {
return
}

lv, err := semver.Parse(strings.TrimPrefix(v.LatestVersion(), "v"))
if err != nil {
return
}

if cv.LessThan(lv) {
notices = append(notices, fmt.Sprintf("Version %s of Trivy is now available, current version is %s", lv, cv))
}

if len(notices) > 0 {
Expand Down
7 changes: 7 additions & 0 deletions pkg/notification/notice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ func TestPrintNotices(t *testing.T) {
responseExpected: true,
expectedOutput: "\n📣 \x1b[34mNotices:\x1b[0m\n - Version 0.60.0 of Trivy is now available, current version is 0.58.0\n\nTo suppress version checks, run Trivy scans with the --skip-version-check flag\n\n",
},
{
name: "New version available but includes a prefixed version number",
options: []Option{WithCurrentVersion("0.58.0")},
latestVersion: "v0.60.0",
responseExpected: true,
expectedOutput: "\n📣 \x1b[34mNotices:\x1b[0m\n - Version 0.60.0 of Trivy is now available, current version is 0.58.0\n\nTo suppress version checks, run Trivy scans with the --skip-version-check flag\n\n",
},
{
name: "new version available but --quiet mode enabled",
options: []Option{
Expand Down