Implement artifact-level render command with Markdown output and update docs
This commit is contained in:
72
internal/render/run.go
Normal file
72
internal/render/run.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package render
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"gitea.maximumdirect.net/eric/seriatim/internal/artifact"
|
||||
"gitea.maximumdirect.net/eric/seriatim/internal/config"
|
||||
)
|
||||
|
||||
// Run executes artifact-level render orchestration.
|
||||
func Run(ctx context.Context, cfg config.RenderConfig) error {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
data, err := os.ReadFile(cfg.InputFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("read --input-file %q: %w", cfg.InputFile, err)
|
||||
}
|
||||
|
||||
inputArtifact, err := artifact.ParseOutputArtifactJSON(data)
|
||||
if err != nil {
|
||||
return fmt.Errorf("--input-file %q: %w", cfg.InputFile, err)
|
||||
}
|
||||
|
||||
model, err := FromOutputArtifact(inputArtifact)
|
||||
if err != nil {
|
||||
return fmt.Errorf("normalize artifact for render: %w", err)
|
||||
}
|
||||
|
||||
registry := NewRegistry()
|
||||
renderer, err := registry.Resolve(cfg.Format)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rendered, err := renderer.Render(model, Options{
|
||||
Title: cfg.Title,
|
||||
IncludeTimestamps: cfg.IncludeTimestamps,
|
||||
IncludeSegmentIDs: cfg.IncludeSegmentIDs,
|
||||
IncludeMetadata: cfg.IncludeMetadata,
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("render %q output: %w", cfg.Format, err)
|
||||
}
|
||||
|
||||
if err := writeFile(cfg.OutputFile, rendered); err != nil {
|
||||
return fmt.Errorf("write --output-file %q: %w", cfg.OutputFile, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func writeFile(path string, content string) (err error) {
|
||||
file, err := os.Create(path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("create %q: %w", path, err)
|
||||
}
|
||||
defer func() {
|
||||
closeErr := file.Close()
|
||||
if err == nil && closeErr != nil {
|
||||
err = fmt.Errorf("close %q: %w", path, closeErr)
|
||||
}
|
||||
}()
|
||||
|
||||
if _, err := file.WriteString(content); err != nil {
|
||||
return fmt.Errorf("write %q: %w", path, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user