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
1 change: 1 addition & 0 deletions cmd/osv-scanner/scan/image/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ func TestCommand_OCIImage_JSONFormat(t *testing.T) {

func TestCommand_HtmlFile(t *testing.T) {
t.Parallel()
testutility.SkipIfNotAcceptanceTesting(t, "Needs container image")
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

oops yeah good point 😅


testDir := testutility.CreateTestDir(t)

Expand Down
24 changes: 20 additions & 4 deletions internal/cmdlogger/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"strings"
)

var GlobalHandler slog.Handler

type Handler struct {
stdout io.Writer
stderr io.Writer
Expand Down Expand Up @@ -39,15 +41,19 @@ func (c *Handler) writer(level slog.Level) io.Writer {
return c.stdout
}

func (c *Handler) Enabled(_ context.Context, level slog.Level) bool {
func (c *Handler) Enabled(ctx context.Context, level slog.Level) bool {
if level == slog.LevelError {
c.hasErrored = true
}

if GlobalHandler != nil {
return GlobalHandler.Enabled(ctx, level)
}

return level >= c.Level.Level()
}

func (c *Handler) Handle(_ context.Context, record slog.Record) error {
func (c *Handler) Handle(ctx context.Context, record slog.Record) error {
if record.Level == slog.LevelError {
c.hasErrored = true

Expand All @@ -56,6 +62,10 @@ func (c *Handler) Handle(_ context.Context, record slog.Record) error {
}
}

if GlobalHandler != nil {
return GlobalHandler.Handle(ctx, record)
}

_, err := fmt.Fprint(c.writer(record.Level), record.Message+"\n")

return err
Expand All @@ -73,11 +83,17 @@ func (c *Handler) HasErroredBecauseInvalidConfig() bool {
return c.hasErroredBecauseInvalidConfig
}

func (c *Handler) WithAttrs(_ []slog.Attr) slog.Handler {
func (c *Handler) WithAttrs(a []slog.Attr) slog.Handler {
if GlobalHandler != nil {
return GlobalHandler.WithAttrs(a)
}
panic("not supported")
}

func (c *Handler) WithGroup(_ string) slog.Handler {
func (c *Handler) WithGroup(g string) slog.Handler {
if GlobalHandler != nil {
return GlobalHandler.WithGroup(g)
}
panic("not supported")
}

Expand Down
6 changes: 6 additions & 0 deletions pkg/osvscanner/osvscanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"log/slog"
"maps"
"net/http"
"os"
Expand Down Expand Up @@ -546,3 +547,8 @@ func overrideGoVersion(scanResults *results.ScanResults) {
}
}
}

// SetLogger sets the global slog handler for the cmdlogger.
func SetLogger(handler slog.Handler) {
cmdlogger.GlobalHandler = handler
}
Loading