package app import ( "context" "errors" "flag" "fmt" "io" "strings" "go.maximumdirect.net/commandkit" ) // Restore loads the selected session and restores its committed remote snapshot. func Restore(ctx context.Context, args []string, out io.Writer) (resultErr error) { return restoreWithDependencies(ctx, args, out, commandDependencies{}) } func restoreWithDependencies(ctx context.Context, args []string, out io.Writer, deps commandDependencies) (resultErr error) { deps = normalizeCommandDependencies(deps) positionalSessionID, args := pullLeadingSessionID(args) fs := flag.NewFlagSet("restore", flag.ContinueOnError) fs.SetOutput(out) var flags commonConfigFlags var dryRun bool var force bool var includeAudio bool addCommonConfigFlags(fs, &flags) fs.BoolVar(&dryRun, "dry-run", false, "plan restore actions without writing local files") fs.BoolVar(&force, "force", false, "overwrite local conflicts with remote state") fs.BoolVar(&includeAudio, "include-audio", false, "include remote session-level audio objects") fs.Usage = func() { _, _ = fmt.Fprintln(out, "Usage: narratio session restore [--config ] [--campaign ] [--campaign-file ] [--session ] [--previous-session-id ] [--dry-run] [--force] [--include-audio]") _, _ = fmt.Fprintln(out) _, _ = fmt.Fprintln(out, "Flags:") fs.PrintDefaults() } if err := fs.Parse(args); err != nil { if errors.Is(err, flag.ErrHelp) { return nil } return fmt.Errorf("restore: invalid flags: %w", err) } if err := resolveParsedSessionID("restore", positionalSessionID, fs, &flags.sessionID); err != nil { return err } if strings.TrimSpace(flags.sessionID) == "" { return fmt.Errorf("restore: session_id is required") } loaded, err := loadCommandConfigWithDependencies(ctx, flags.pipelinePath, flags.campaignPath, flags.campaignFilePath, flags.sessionPath, flags.sessionOptions(), flags.secretsDir.pointer(), deps) if err != nil { return fmt.Errorf("restore: %w", err) } defer func() { _ = loaded.Close() }() cfg := loaded.Config report, err := restoreSession(ctx, cfg, RestorePlanOptions{DryRun: dryRun, Force: force, IncludeAudio: includeAudio}, deps) if report != nil { err = errors.Join(err, commandkit.RenderReport(out, commandkit.Report{Value: report}, "")) } return err }