Skip to content

Commit

Permalink
feat: use run.relative-path-mode for output format paths (#5363)
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez authored Feb 1, 2025
1 parent fc94060 commit 10c03d7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pkg/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func (c *runCommand) preRunE(_ *cobra.Command, args []string) error {

c.dbManager = dbManager

printer, err := printers.NewPrinter(c.log, &c.cfg.Output, c.reportData)
printer, err := printers.NewPrinter(c.log, &c.cfg.Output, c.reportData, c.cfg.GetBasePath())
if err != nil {
return err
}
Expand Down
8 changes: 7 additions & 1 deletion pkg/printers/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type issuePrinter interface {
type Printer struct {
cfg *config.Output
reportData *report.Data
basePath string

log logutils.Log

Expand All @@ -33,7 +34,7 @@ type Printer struct {
}

// NewPrinter creates a new Printer.
func NewPrinter(log logutils.Log, cfg *config.Output, reportData *report.Data) (*Printer, error) {
func NewPrinter(log logutils.Log, cfg *config.Output, reportData *report.Data, basePath string) (*Printer, error) {
if log == nil {
return nil, errors.New("missing log argument in constructor")
}
Expand All @@ -47,6 +48,7 @@ func NewPrinter(log logutils.Log, cfg *config.Output, reportData *report.Data) (
return &Printer{
cfg: cfg,
reportData: reportData,
basePath: basePath,
log: log,
stdOut: logutils.StdOut,
stdErr: logutils.StdErr,
Expand Down Expand Up @@ -98,6 +100,10 @@ func (c *Printer) createWriter(path string) (io.Writer, bool, error) {
return c.stdErr, false, nil
}

if !filepath.IsAbs(path) {
path = filepath.Join(c.basePath, path)
}

err := os.MkdirAll(filepath.Dir(path), os.ModePerm)
if err != nil {
return nil, false, err
Expand Down
8 changes: 4 additions & 4 deletions pkg/printers/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestPrinter_Print_stdout(t *testing.T) {
t.Run(test.desc, func(t *testing.T) {
t.Parallel()

p, err := NewPrinter(logger, test.cfg, data)
p, err := NewPrinter(logger, test.cfg, data, "")
require.NoError(t, err)

var stdOutBuffer bytes.Buffer
Expand Down Expand Up @@ -106,7 +106,7 @@ func TestPrinter_Print_stderr(t *testing.T) {
},
}

p, err := NewPrinter(logger, cfg, data)
p, err := NewPrinter(logger, cfg, data, "")
require.NoError(t, err)

var stdOutBuffer bytes.Buffer
Expand Down Expand Up @@ -145,7 +145,7 @@ func TestPrinter_Print_file(t *testing.T) {
},
}

p, err := NewPrinter(logger, cfg, data)
p, err := NewPrinter(logger, cfg, data, "")
require.NoError(t, err)

var stdOutBuffer bytes.Buffer
Expand Down Expand Up @@ -197,7 +197,7 @@ func TestPrinter_Print_multiple(t *testing.T) {
},
}

p, err := NewPrinter(logger, cfg, data)
p, err := NewPrinter(logger, cfg, data, "")
require.NoError(t, err)

var stdOutBuffer bytes.Buffer
Expand Down

0 comments on commit 10c03d7

Please sign in to comment.