30 lines
646 B
Go
30 lines
646 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(path string, raw []byte) error {
|
|
if !json.Valid(raw) {
|
|
return fmt.Errorf("transcript file %q is not valid JSON", path)
|
|
}
|
|
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
|
|
}
|