72 lines
2.1 KiB
Go
72 lines
2.1 KiB
Go
package diagnostics
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
type probeFile interface {
|
|
Name() string
|
|
Close() error
|
|
}
|
|
|
|
type probeOperations struct {
|
|
create func(string, string) (probeFile, error)
|
|
remove func(string) error
|
|
}
|
|
|
|
// ProbeOutput checks whether temporary files can be created beside active regular outputs.
|
|
func ProbeOutput(ctx context.Context, checked *CheckedDestinations) error {
|
|
return probeOutput(ctx, checked, probeOperations{
|
|
create: func(dir, pattern string) (probeFile, error) { return os.CreateTemp(dir, pattern) },
|
|
remove: os.Remove,
|
|
})
|
|
}
|
|
|
|
func probeOutput(ctx context.Context, checked *CheckedDestinations, ops probeOperations) error {
|
|
type target struct{ display, path string }
|
|
var targets []target
|
|
if checked.transcript != "" && !checked.direct {
|
|
targets = append(targets, target{checked.transcript, checked.publishPath})
|
|
}
|
|
if checked.enabled {
|
|
for i, path := range checked.reportDestinations {
|
|
targets = append(targets, target{checked.reports[i], path})
|
|
}
|
|
}
|
|
for _, target := range targets {
|
|
if err := ctx.Err(); err != nil {
|
|
return fmt.Errorf("probe output %q: %w", target.display, err)
|
|
}
|
|
pattern := ".audita-probe-*"
|
|
if strings.HasPrefix(filepath.Base(target.path), ".audita-probe-") {
|
|
pattern = ".audita-check-*"
|
|
}
|
|
file, err := ops.create(filepath.Dir(target.path), pattern)
|
|
if err != nil {
|
|
return errors.Join(fmt.Errorf("probe output %q: create temporary file: %w", target.display, err), ctx.Err())
|
|
}
|
|
name := file.Name()
|
|
closeErr := file.Close()
|
|
removeErr := ops.remove(name)
|
|
var failures []error
|
|
if closeErr != nil {
|
|
failures = append(failures, fmt.Errorf("probe output %q: close temporary file %q: %w", target.display, name, closeErr))
|
|
}
|
|
if removeErr != nil {
|
|
failures = append(failures, fmt.Errorf("probe output %q: remove temporary file %q: %w", target.display, name, removeErr))
|
|
}
|
|
if err := ctx.Err(); err != nil {
|
|
failures = append(failures, fmt.Errorf("probe output %q: %w", target.display, err))
|
|
}
|
|
if err := errors.Join(failures...); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return ctx.Err()
|
|
}
|