Files
zeekay 4e3a8ef9ca merge: upstream/main (preserve Hanzo brand, 50 commits)
# Conflicts:
#	api/v1alpha1/casting.go
#	api/v1alpha1/installation/signoz.go
#	docs/casting.md
#	docs/examples/docker/compose/pours/deployment/configs/ingester/opamp.yaml
#	docs/examples/systemd/binary/pours/deployment/signoz-ingester.service
#	docs/examples/systemd/binary/pours/deployment/signoz-signoz.service
#	docs/examples/systemd/binary/pours/deployment/signoz-telemetrystore-migrator.service
#	docs/schemas/v1alpha1.yaml
#	internal/foundry/registry.go
#	internal/instrumentation/handler.go
#	internal/molding/signozmolding/signoz.go
2026-06-02 09:59:25 -07:00

86 lines
2.1 KiB
Go

package writer
import (
"context"
"io"
"log/slog"
"os"
"path/filepath"
foundryerrors "github.com/hanzoai/o11y-foundry/internal/errors"
"github.com/hanzoai/o11y-foundry/internal/types"
)
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
}