diff --git a/cli/reporter/file_handler.go b/cli/reporter/file_handler.go index 43fc07ee..60d2e5c9 100644 --- a/cli/reporter/file_handler.go +++ b/cli/reporter/file_handler.go @@ -6,6 +6,7 @@ package reporter import ( "context" "os" + "strings" "github.com/rs/zerolog/log" "go.mondoo.com/cnspec/v9/policy" @@ -19,7 +20,8 @@ type localFileHandler struct { // we reuse the already implemented Reporter's WriteReport method by simply pointing the writer // towards a file instead of stdout func (h *localFileHandler) WriteReport(ctx context.Context, report *policy.ReportCollection) error { - f, err := os.Create(h.file) + trimmedFile := strings.TrimPrefix(h.file, "file://") + f, err := os.Create(trimmedFile) if err != nil { return err } @@ -33,6 +35,6 @@ func (h *localFileHandler) WriteReport(ctx context.Context, report *policy.Repor if err != nil { return err } - log.Info().Str("file", h.file).Msg("wrote report to file") + log.Info().Str("file", trimmedFile).Msg("wrote report to file") return nil } diff --git a/cli/reporter/file_handler_test.go b/cli/reporter/file_handler_test.go index 0096e74e..16b0f046 100644 --- a/cli/reporter/file_handler_test.go +++ b/cli/reporter/file_handler_test.go @@ -8,6 +8,7 @@ import ( "encoding/json" "fmt" "os" + "strings" "testing" "time" @@ -25,21 +26,44 @@ func TestFileHandler(t *testing.T) { require.NoError(t, err) now := time.Now().Format(time.RFC3339) - fileName := fmt.Sprintf("/tmp/%s-testfilehandler.json", now) - config := HandlerConfig{Format: "compact", OutputTarget: fileName} - handler, err := NewOutputHandler(config) - require.NoError(t, err) - err = handler.WriteReport(context.Background(), yr) - require.NoError(t, err) - data, err := os.ReadFile(fileName) - require.NoError(t, err) + t.Run("with no prefix", func(t *testing.T) { + fileName := fmt.Sprintf("/tmp/%s-testfilehandler.json", now) + config := HandlerConfig{Format: "compact", OutputTarget: fileName} + handler, err := NewOutputHandler(config) + require.NoError(t, err) + err = handler.WriteReport(context.Background(), yr) + require.NoError(t, err) + data, err := os.ReadFile(fileName) + require.NoError(t, err) - strData := string(data) - assert.Contains(t, strData, "✕ Fail: Ensure") - assert.Contains(t, strData, ". Skipped: Set") - assert.Contains(t, strData, "! Error: Set") - assert.Contains(t, strData, "✓ Pass: A 100 Ensure") - assert.Contains(t, strData, "✕ Fail: F 0 Ensure") - err = os.Remove(fileName) - require.NoError(t, err) + strData := string(data) + assert.Contains(t, strData, "✕ Fail: Ensure") + assert.Contains(t, strData, ". Skipped: Set") + assert.Contains(t, strData, "! Error: Set") + assert.Contains(t, strData, "✓ Pass: A 100 Ensure") + assert.Contains(t, strData, "✕ Fail: F 0 Ensure") + err = os.Remove(fileName) + require.NoError(t, err) + }) + + t.Run("with file:// prefix", func(t *testing.T) { + fileName := fmt.Sprintf("file:///tmp/%s-testfilehandler.json", now) + config := HandlerConfig{Format: "compact", OutputTarget: fileName} + handler, err := NewOutputHandler(config) + require.NoError(t, err) + err = handler.WriteReport(context.Background(), yr) + require.NoError(t, err) + trimmed := strings.TrimPrefix(fileName, "file://") + data, err := os.ReadFile(trimmed) + require.NoError(t, err) + + strData := string(data) + assert.Contains(t, strData, "✕ Fail: Ensure") + assert.Contains(t, strData, ". Skipped: Set") + assert.Contains(t, strData, "! Error: Set") + assert.Contains(t, strData, "✓ Pass: A 100 Ensure") + assert.Contains(t, strData, "✕ Fail: F 0 Ensure") + err = os.Remove(trimmed) + require.NoError(t, err) + }) }