71 lines
2.5 KiB
Go
71 lines
2.5 KiB
Go
|
|
package audita
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
_ "embed"
|
||
|
|
"fmt"
|
||
|
|
"os"
|
||
|
|
"path/filepath"
|
||
|
|
"strings"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"gitea.maximumdirect.net/eric/narratio/internal/adapters/subprocess"
|
||
|
|
)
|
||
|
|
|
||
|
|
//go:embed synthetic-transcript.json
|
||
|
|
var syntheticTranscript []byte
|
||
|
|
|
||
|
|
// PreflightResult records the native invocation outcome.
|
||
|
|
type PreflightResult struct {
|
||
|
|
ExitCode int
|
||
|
|
Duration time.Duration
|
||
|
|
}
|
||
|
|
|
||
|
|
// Preflight checks actual processing settings with a synthetic transcript and native smoke checks.
|
||
|
|
func (r *SubprocessRunner) Preflight(ctx context.Context, req PolishRequest) (PreflightResult, error) {
|
||
|
|
if r == nil {
|
||
|
|
return PreflightResult{}, fmt.Errorf("audita subprocess runner is nil")
|
||
|
|
}
|
||
|
|
if strings.TrimSpace(req.OutputProcessedPath) == "" {
|
||
|
|
return PreflightResult{}, fmt.Errorf("audita output processed path is required")
|
||
|
|
}
|
||
|
|
for i, path := range req.GlossaryFiles {
|
||
|
|
if strings.TrimSpace(path) == "" {
|
||
|
|
return PreflightResult{}, fmt.Errorf("audita glossary file at index %d is empty", i)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
dir, err := os.MkdirTemp("", "narratio-audita-preflight-")
|
||
|
|
if err != nil {
|
||
|
|
return PreflightResult{}, fmt.Errorf("create audita preflight directory: %w", err)
|
||
|
|
}
|
||
|
|
defer os.RemoveAll(dir)
|
||
|
|
req.MergedTranscriptPath = filepath.Join(dir, "transcript.json")
|
||
|
|
req.StdoutLogPath = filepath.Join(dir, "stdout.log")
|
||
|
|
req.StderrLogPath = filepath.Join(dir, "stderr.log")
|
||
|
|
req.GeneratedConfigPath = filepath.Join(dir, "invocation.yml")
|
||
|
|
if err := os.WriteFile(req.MergedTranscriptPath, syntheticTranscript, 0600); err != nil {
|
||
|
|
return PreflightResult{}, fmt.Errorf("write synthetic transcript: %w", err)
|
||
|
|
}
|
||
|
|
args := r.buildArgs("preflight", req)
|
||
|
|
env, names, err := subprocess.CredentialEnvironment(r.cfg.Credentials)
|
||
|
|
if err != nil {
|
||
|
|
return PreflightResult{}, fmt.Errorf("audita credentials: %w", err)
|
||
|
|
}
|
||
|
|
if err := r.writeInvocationConfig(req, args, names); err != nil {
|
||
|
|
return PreflightResult{}, fmt.Errorf("write preflight invocation: %w", err)
|
||
|
|
}
|
||
|
|
run := r.run
|
||
|
|
if run == nil {
|
||
|
|
run = subprocess.Run
|
||
|
|
}
|
||
|
|
out, err := run(ctx, subprocess.RunRequest{Executable: r.cfg.Binary, Args: args, Timeout: r.cfg.Timeout, EnvOverrides: env, SensitiveEnvNames: names, DiagnosticOwner: "audita preflight", StdoutLogPath: req.StdoutLogPath, StderrLogPath: req.StderrLogPath})
|
||
|
|
result := PreflightResult{ExitCode: out.ExitCode, Duration: out.Duration}
|
||
|
|
if err != nil {
|
||
|
|
return result, fmt.Errorf("audita preflight failed: %w", err)
|
||
|
|
}
|
||
|
|
if out.ExitCode != 0 {
|
||
|
|
return result, fmt.Errorf("audita preflight failed with exit code %d", out.ExitCode)
|
||
|
|
}
|
||
|
|
return result, nil
|
||
|
|
}
|