50 lines
916 B
Go
50 lines
916 B
Go
package normalize
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
|
|
"gitea.maximumdirect.net/eric/seriatim/internal/config"
|
|
)
|
|
|
|
// Run validates command wiring for normalize and will later execute
|
|
// artifact-level normalization.
|
|
func Run(ctx context.Context, cfg config.NormalizeConfig) error {
|
|
if err := ctx.Err(); err != nil {
|
|
return err
|
|
}
|
|
|
|
parsed, err := ParseFile(cfg.InputFile)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
output, err := Build(parsed, cfg)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := writeOutputJSON(cfg.OutputFile, output); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func writeOutputJSON(path string, value any) error {
|
|
file, err := os.Create(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer file.Close()
|
|
|
|
encoder := json.NewEncoder(file)
|
|
encoder.SetIndent("", " ")
|
|
if err := encoder.Encode(value); err != nil {
|
|
return fmt.Errorf("encode normalize output JSON: %w", err)
|
|
}
|
|
return nil
|
|
}
|