138 lines
6.1 KiB
Go
138 lines
6.1 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"path/filepath"
|
|
|
|
"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 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"`
|
|
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 entire local sessions, including prepared inputs and products.",
|
|
Description: "Deletes the selected local session workspace, including its manifest, prepared inputs, products, and run history. Remote data and subprocess caches remain. Use --dry-run to preview deletion. To remove only run history while retaining a reusable session, use narratio run SESSION --stage cleanup; this requires workspace.cleanup_after_publish=true and valid successful publication evidence.", NewInput: func() *cleanCommandInput { return &cleanCommandInput{} },
|
|
Flags: []commandkit.Flag[*cleanCommandInput]{common.Config, common.Overlay, common.Campaign, common.CampaignFile, common.SessionFile, common.PartyFile,
|
|
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 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 }),
|
|
},
|
|
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, withCommandReporting(deps, invocation))
|
|
if report != nil {
|
|
err = errors.Join(err, invocation.Reporter().Report(commandkit.Report{Value: report}))
|
|
}
|
|
return reportOperationOutcome(invocation, err)
|
|
},
|
|
})
|
|
}
|
|
|
|
func cleanSelectedSession(ctx context.Context, input *cleanCommandInput, deps commandDependencies) (report *cleanCommandReport, err error) {
|
|
deps = normalizeCommandDependencies(deps)
|
|
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, Actions: []cleanAction{}}
|
|
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
|
|
}
|
|
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)
|
|
if err := appendTarget(pipeline.Workspace.Root, work, "clean.workspace.session", true); err != nil {
|
|
return report, err
|
|
}
|
|
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
|
|
}
|