Files

86 lines
2.1 KiB
Go

package writer
import (
"context"
"io"
"log/slog"
"os"
"path/filepath"
"github.com/hanzoai/o11y-foundry/internal/domain"
foundryerrors "github.com/hanzoai/o11y-foundry/internal/errors"
)
type Options struct {
Output io.Writer
TargetDirectory string
}
type Writer struct {
logger *slog.Logger
options Options
}
// NewManager creates a new output manager.
func New(logger *slog.Logger, options *Options) (*Writer, error) {
if options == nil {
options = &Options{
Output: &os.File{},
TargetDirectory: "./pours",
}
}
if options.Output == nil {
options.Output = &os.File{}
}
if err := os.MkdirAll(options.TargetDirectory, 0755); err != nil {
return nil, foundryerrors.Wrapf(err, foundryerrors.TypeInternal, "failed to create output directory '%s'", options.TargetDirectory)
}
return &Writer{
logger: logger,
options: *options,
}, nil
}
func (w *Writer) Write(ctx context.Context, material domain.Material) error {
if _, ok := w.options.Output.(*os.File); ok {
path := filepath.Join(w.options.TargetDirectory, material.Path())
// Create parent directories if they don't exist
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
w.logger.ErrorContext(ctx, "failed to create directory", slog.String("path", filepath.Dir(path)), foundryerrors.LogAttr(err))
return err
}
if err := os.WriteFile(path, material.FmtContents(), 0644); err != nil {
w.logger.ErrorContext(ctx, "failed to write material", slog.String("path", path), foundryerrors.LogAttr(err))
return err
}
w.logger.InfoContext(ctx, "successfully wrote material", slog.String("path", path))
return nil
}
_, err := w.options.Output.Write(material.FmtContents())
if err != nil {
w.logger.ErrorContext(ctx, "failed to write material", foundryerrors.LogAttr(err))
return err
}
w.logger.InfoContext(ctx, "successfully wrote material")
return nil
}
func (w *Writer) WriteMany(ctx context.Context, materials ...domain.Material) error {
for _, material := range materials {
if err := w.Write(ctx, material); err != nil {
return err
}
}
return nil
}