package app import ( "context" "errors" "fmt" "os" "path/filepath" "gitea.maximumdirect.net/eric/narratio/internal/adapters/storage" "gitea.maximumdirect.net/eric/narratio/internal/artifacts" "gitea.maximumdirect.net/eric/narratio/internal/audio" "gitea.maximumdirect.net/eric/narratio/internal/config" "gitea.maximumdirect.net/eric/narratio/internal/previouscache" ) type stableInputCheck struct { Name string Path string Err error } type localAudioCheck struct { Checked bool Paths []string Err error } type remoteAudioCheck struct { Checked bool Prefix string Keys []string Err error } type previousArtifactReadiness struct { Requirements []artifacts.PreviousArtifactRequirement SkippedMissing []string Err error } type remoteCurrentStateCheck struct { State *RemoteCurrentState Err error } type remoteLocksCheck struct { Locks []artifacts.RemoteLock Err error } func inspectStableInputs(cfg *config.Config) []stableInputCheck { items := []struct { name string in config.ResolvedInputFile derived string }{ {name: "speakers", in: cfg.StableInputs.SpeakersFile}, {name: "autocorrect", derived: "derived from configuration"}, {name: "players", derived: "derived from canonical party"}, {name: "party", in: cfg.StableInputs.PartyFile}, } out := make([]stableInputCheck, 0, len(items)) for _, item := range items { if item.derived != "" { out = append(out, stableInputCheck{Name: item.name, Path: item.derived}) continue } path, err := resolveHelperConfigRelativePath(item.in) if err != nil { out = append(out, stableInputCheck{Name: item.name, Err: err}) continue } if err := requireInspectionFile(path, item.name); err != nil { out = append(out, stableInputCheck{Name: item.name, Path: path, Err: err}) continue } out = append(out, stableInputCheck{Name: item.name, Path: path}) } return out } func inspectLocalAudioPresence(cfg *config.Config) localAudioCheck { if cfg.Session.Inputs.AudioS3 != nil { return localAudioCheck{} } sessionDir := filepath.Dir(cfg.SessionPath) sources, err := audio.DiscoverLocal(sessionDir, cfg.Session.Inputs.AudioFiles, cfg.Session.Inputs.AudioDir) if err != nil { return localAudioCheck{Checked: true, Err: err} } paths := make([]string, 0, len(sources)) for _, source := range sources { paths = append(paths, source.Path) } return localAudioCheck{ Checked: true, Paths: paths, } } func inspectRemoteAudioPresence(ctx context.Context, cfg *config.Config, store storage.ObjectStore) remoteAudioCheck { if cfg.Session.Inputs.AudioS3 == nil { return remoteAudioCheck{} } if store == nil { return remoteAudioCheck{Checked: true, Err: fmt.Errorf("storage backend is required for remote audio checks")} } sessionPrefix := artifacts.S3SessionPrefix(cfg.Pipeline.Storage.S3.RootPrefix, cfg.Session.Campaign, cfg.Session.SessionID) audioPrefix := artifacts.S3AudioPrefix(sessionPrefix, cfg.Session.Inputs.AudioS3.Prefix) objects, err := audio.DiscoverS3(ctx, store, audioPrefix) if err != nil { return remoteAudioCheck{Checked: true, Prefix: audioPrefix, Err: err} } keys := make([]string, 0, len(objects)) for _, obj := range objects { keys = append(keys, obj.Key) } return remoteAudioCheck{ Checked: true, Prefix: audioPrefix, Keys: keys, } } func inspectPreviousArtifactReadiness( ctx context.Context, cfg *config.Config, store storage.ObjectStore, requirements []artifacts.PreviousArtifactRequirement, ) previousArtifactReadiness { out := previousArtifactReadiness{ Requirements: append([]artifacts.PreviousArtifactRequirement(nil), requirements...), } if len(requirements) == 0 { return out } if cfg == nil || cfg.Pipeline == nil || cfg.Session == nil { out.Err = fmt.Errorf("resolved config with pipeline/session is required") return out } paths := artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root).SessionPathsFor(cfg.Session.Campaign, cfg.Session.SessionID) plan, err := previouscache.Resolve(ctx, cfg, paths, requirements, store) if err != nil { var pointerMissing *artifacts.CurrentCommitPointerMissingError if errors.As(err, &pointerMissing) { out.Err = fmt.Errorf("remote %w", pointerMissing) return out } out.Err = fmt.Errorf("remote %w", err) return out } out.SkippedMissing = append([]string(nil), plan.SkippedMissing...) return out } func inspectRemoteCurrentStateWithDependencies(ctx context.Context, cfg *config.Config, store storage.ObjectStore, deps commandDependencies) remoteCurrentStateCheck { if store == nil { return remoteCurrentStateCheck{} } deps = normalizeCommandDependencies(deps) current, err := deps.discoverRemotePublication(ctx, cfg, store) if err != nil { return remoteCurrentStateCheck{Err: err} } return remoteCurrentStateCheck{State: current} } func inspectRemoteLocks(ctx context.Context, cfg *config.Config, store storage.ObjectStore) remoteLocksCheck { if store == nil { return remoteLocksCheck{} } sessionPrefix, err := artifacts.ResolvePublishSessionPrefix(cfg, nil) if err != nil { return remoteLocksCheck{Err: err} } locks, err := artifacts.ListRemoteLocks(ctx, store, sessionPrefix) if err != nil { return remoteLocksCheck{Err: err} } return remoteLocksCheck{Locks: locks} } func requireInspectionFile(path, label string) error { info, err := os.Stat(path) if err != nil { if os.IsNotExist(err) { return fmt.Errorf("%s %q does not exist: %w", label, path, err) } return fmt.Errorf("stat %s %q: %w", label, path, err) } if !info.Mode().IsRegular() { return fmt.Errorf("%s %q is not a regular file", label, path) } return nil } func previousRequirementSkipped(values []string, name string) bool { for _, value := range values { if value == name { return true } } return false }