Files
audita/internal/core/io/files.go

30 lines
624 B
Go

package io
import (
"encoding/json"
"fmt"
"os"
)
func ReadRequiredFile(path string, label string) ([]byte, error) {
contents, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("failed to read %s file %q: %w", label, path, err)
}
return contents, nil
}
func ValidateWellFormedJSON(raw []byte) error {
if !json.Valid(raw) {
return fmt.Errorf("transcript file is not valid JSON")
}
return nil
}
func WriteFile(path string, contents []byte) error {
if err := os.WriteFile(path, contents, 0o644); err != nil {
return fmt.Errorf("failed to write output file %q: %w", path, err)
}
return nil
}