package app import ( "context" "errors" "fmt" "path/filepath" "strings" "gitea.maximumdirect.net/eric/narratio/internal/artifacts" "gitea.maximumdirect.net/eric/narratio/internal/config" "gitea.maximumdirect.net/eric/narratio/internal/fileops" "go.maximumdirect.net/commandkit" ) type cleanCommandInput struct { Selection configurationSelection PreviousSession commandkit.Supplied[string] All, DryRun, ClearCache bool } type cleanCommandReport struct { Campaign string `json:"campaign,omitempty"` SessionID string `json:"session_id,omitempty"` All bool `json:"all"` DryRun bool `json:"dry_run"` Complete bool `json:"complete"` Cache string `json:"cache"` Actions []cleanAction `json:"actions"` Error string `json:"error,omitempty"` } type cleanAction struct { Path string `json:"path"` Status string `json:"status"` Error string `json:"error,omitempty"` } func checkCleanInput(input *cleanCommandInput) error { if err := checkConfigurationSelection(input.Selection); err != nil { return err } if input.All { if input.Selection.Session.Set || input.Selection.SessionFile.Set || input.Selection.Campaign.Set || input.Selection.CampaignFile.Set || input.Selection.PartyFile.Set || input.PreviousSession.Set { return fmt.Errorf("--all cannot be combined with session, campaign, party, or previous-session selections") } } else if !input.Selection.Session.Set { return fmt.Errorf("session identity is required unless --all is set") } return nil } func cleanCommand(deps commandDependencies) commandkit.Command { common := newConfigurationFlags(func(input **cleanCommandInput) *configurationSelection { return &(*input).Selection }) return commandkit.NewCommand(commandkit.CommandSpec[*cleanCommandInput]{Name: "clean", Summary: "Remove selected local session data, preserving cache unless requested.", NewInput: func() *cleanCommandInput { return &cleanCommandInput{} }, Flags: []commandkit.Flag[*cleanCommandInput]{common.Config, common.Overlay, common.Campaign, common.CampaignFile, common.SessionFile, common.PartyFile, common.SecretsDir, commandkit.ValueFlag(commandkit.FlagMeta{Name: "previous-session-id", Usage: "Assert the previous session identity."}, nil, stringValue, func(input **cleanCommandInput, value commandkit.Supplied[string]) { (*input).PreviousSession = value }), commandkit.BoolFlag(commandkit.FlagMeta{Name: "all", Usage: "Clean all local session workspace and spool entries."}, false, func(input **cleanCommandInput, value commandkit.Supplied[bool]) { (*input).All = value.Value }), commandkit.BoolFlag(commandkit.FlagMeta{Name: "dry-run", Usage: "Preview exact paths without deleting them."}, false, func(input **cleanCommandInput, value commandkit.Supplied[bool]) { (*input).DryRun = value.Value }), commandkit.BoolFlag(commandkit.FlagMeta{Name: "clear-cache", Usage: "Also remove the selected S3 audio cache entries."}, false, func(input **cleanCommandInput, value commandkit.Supplied[bool]) { (*input).ClearCache = value.Value }), }, Arguments: []commandkit.Argument[*cleanCommandInput]{commandkit.ValueArgument(commandkit.ArgumentMeta{Name: "SESSION", Usage: "Session identity, omitted with --all."}, false, stringValue, func(input **cleanCommandInput, value commandkit.Supplied[string]) { (*input).Selection.Session = value })}, CheckInput: checkCleanInput, Run: func(invocation commandkit.Invocation, input *cleanCommandInput) commandkit.Outcome { report, err := cleanSelectedSession(invocation.Context, input, deps) if report != nil { err = errors.Join(err, invocation.Reporter().Report(commandkit.Report{Value: report})) } return operationOutcome(err) }, }) } func cleanSelectedSession(ctx context.Context, input *cleanCommandInput, deps commandDependencies) (report *cleanCommandReport, err error) { deps = normalizeCommandDependencies(deps) deps.secretsBootstrap = &secretsBootstrap{} acquired := newConfigurationAcquisition(input.Selection, deps) acquired.previousSessionID = input.PreviousSession.Value defer func() { err = errors.Join(err, acquired.Close()) if report != nil { report.Complete = err == nil && !input.DryRun if err != nil { report.Error = err.Error() } } }() report = &cleanCommandReport{All: input.All, DryRun: input.DryRun, Cache: "preserved", Actions: []cleanAction{}} if input.ClearCache { report.Cache = "requested" } pipeline, err := acquired.Pipeline(ctx) if err != nil { return report, err } appendTarget := func(root, target, policy string, directory bool) error { if err := ctx.Err(); err != nil { return err } action, err := cleanScopedTarget(root, target, policy, directory, input.DryRun) report.Actions = append(report.Actions, action) return err } if input.All { if err := appendTarget(pipeline.Workspace.Root, filepath.Join(pipeline.Workspace.Root, config.PathWorkDirSegment), "clean.workspace.all", true); err != nil { return report, err } actions, err := cleanRootChildren(ctx, pipeline.Spool.Root, "clean.spool.all", input.DryRun) report.Actions = append(report.Actions, actions...) if err != nil { return report, err } if input.ClearCache { if pipeline.Storage.S3 == nil || strings.TrimSpace(pipeline.Storage.S3.Bucket) == "" { return report, fmt.Errorf("clear cache requires pipeline.storage.s3.bucket") } namespace, err := artifacts.S3AudioCacheNamespaceDir(pipeline.Cache.Root, pipeline.Storage.S3.Bucket, pipeline.Storage.S3.RootPrefix) if err != nil { return report, err } if err := appendTarget(pipeline.Cache.Root, namespace, "clean.cache.all", true); err != nil { return report, err } report.Cache = "processed" } return report, nil } cfg, err := acquired.Domain(ctx) if err != nil { return report, err } report.Campaign = cfg.Session.Campaign report.SessionID = cfg.Session.SessionID work := artifacts.SessionWorkDirForCampaign(pipeline.Workspace.Root, report.Campaign, report.SessionID) spool := artifacts.SessionSpoolDir(pipeline.Spool.Root, report.Campaign, report.SessionID) if err := appendTarget(pipeline.Workspace.Root, work, "clean.workspace.session", true); err != nil { return report, err } if err := appendTarget(pipeline.Spool.Root, spool, "clean.spool.session", true); err != nil { return report, err } if input.ClearCache { if cfg.Session.Inputs.AudioS3 == nil { report.Cache = "not_applicable" return report, nil } actions, err := cleanSessionCache(ctx, cfg, input.DryRun, deps) report.Actions = append(report.Actions, actions...) if err != nil { return report, err } report.Cache = "processed" } return report, nil } func cleanScopedTarget(root, target, policy string, directory, dryRun bool) (cleanAction, error) { action := cleanAction{Path: target, Status: "failed"} scoped, err := validateScopedTarget(root, target, policy, directory) if err != nil { action.Error = err.Error() return action, err } action.Path = scoped.TargetAbs if !scoped.Exists { action.Status = "missing" return action, nil } if dryRun { action.Status = "would_delete" return action, nil } if err := fileops.RemoveAllUnderRoot(scoped.RootAbs, scoped.TargetAbs); err != nil { action.Error = err.Error() return action, fmt.Errorf("cleanup policy %s: remove %q: %w", policy, scoped.TargetAbs, err) } action.Status = "deleted" return action, nil } func cleanRootChildren(ctx context.Context, root, policy string, dryRun bool) ([]cleanAction, error) { rootAbs, entries, err := cleanableRootChildren(root, policy) if err != nil { return []cleanAction{{Path: root, Status: "failed", Error: err.Error()}}, err } if len(entries) == 0 { return []cleanAction{{Path: rootAbs, Status: "empty"}}, nil } actions := make([]cleanAction, 0, len(entries)) for _, entry := range entries { if err := ctx.Err(); err != nil { return actions, err } action := cleanAction{Path: entry, Status: "would_delete"} if !dryRun { if err := fileops.RemoveAllUnderRoot(rootAbs, entry); err != nil { action.Status = "failed" action.Error = err.Error() return append(actions, action), fmt.Errorf("cleanup policy %s: remove %q: %w", policy, entry, err) } action.Status = "deleted" } actions = append(actions, action) } return actions, nil } func cleanSessionCache(ctx context.Context, cfg *config.Config, dryRun bool, deps commandDependencies) ([]cleanAction, error) { if cfg.Pipeline.Storage.S3 == nil || strings.TrimSpace(cfg.Pipeline.Storage.S3.Bucket) == "" { return nil, fmt.Errorf("clear cache requires pipeline.storage.s3.bucket") } store, err := newCommandObjectStoreWithDependencies(ctx, cfg, nil, deps) if err != nil { return nil, fmt.Errorf("initialize object store for cache cleanup: %w", err) } prefix := artifacts.S3SessionPrefix(cfg.Pipeline.Storage.S3.RootPrefix, cfg.Session.Campaign, cfg.Session.SessionID) audioPrefix := artifacts.S3AudioPrefix(prefix, cfg.Session.Inputs.AudioS3.Prefix) objects, err := store.List(ctx, audioPrefix) if err != nil { return nil, fmt.Errorf("list s3 audio objects under %q: %w", audioPrefix, err) } actions := []cleanAction{} for _, object := range objects { if err := ctx.Err(); err != nil { return actions, err } key := strings.TrimSpace(object.Key) if key == "" || strings.HasSuffix(key, "/") || !cleanIsFlac(key) { continue } cache, err := artifacts.S3AudioCachePath(cfg.Pipeline.Cache.Root, cfg.Pipeline.Storage.S3.Bucket, key) if err != nil { return actions, err } action, err := cleanScopedTarget(cfg.Pipeline.Cache.Root, cache, "clean.cache.session", false, dryRun) actions = append(actions, action) if err != nil { return actions, err } } return actions, nil }