2026-05-19 21:39:13 -05:00
|
|
|
package app
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"errors"
|
|
|
|
|
"flag"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
2026-05-22 22:09:17 -05:00
|
|
|
"strings"
|
2026-05-19 21:39:13 -05:00
|
|
|
|
2026-11-20 05:41:16 +00:00
|
|
|
"go.maximumdirect.net/commandkit"
|
2026-05-19 21:39:13 -05:00
|
|
|
)
|
|
|
|
|
|
2026-11-20 05:41:16 +00:00
|
|
|
// Restore loads the selected session and restores its committed remote snapshot.
|
2026-08-10 18:14:09 +00:00
|
|
|
func Restore(ctx context.Context, args []string, out io.Writer) (resultErr error) {
|
2026-09-05 18:05:18 +00:00
|
|
|
return restoreWithDependencies(ctx, args, out, commandDependencies{})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func restoreWithDependencies(ctx context.Context, args []string, out io.Writer, deps commandDependencies) (resultErr error) {
|
|
|
|
|
deps = normalizeCommandDependencies(deps)
|
2026-05-22 22:09:17 -05:00
|
|
|
positionalSessionID, args := pullLeadingSessionID(args)
|
2026-05-19 21:39:13 -05:00
|
|
|
fs := flag.NewFlagSet("restore", flag.ContinueOnError)
|
|
|
|
|
fs.SetOutput(out)
|
|
|
|
|
|
2026-05-23 13:47:39 +00:00
|
|
|
var flags commonConfigFlags
|
2026-05-19 21:39:13 -05:00
|
|
|
var dryRun bool
|
|
|
|
|
var force bool
|
|
|
|
|
var includeAudio bool
|
2026-05-23 13:47:39 +00:00
|
|
|
addCommonConfigFlags(fs, &flags)
|
2026-05-19 21:39:13 -05:00
|
|
|
fs.BoolVar(&dryRun, "dry-run", false, "plan restore actions without writing local files")
|
|
|
|
|
fs.BoolVar(&force, "force", false, "overwrite local conflicts with remote state")
|
2026-05-23 10:05:14 -05:00
|
|
|
fs.BoolVar(&includeAudio, "include-audio", false, "include remote session-level audio objects")
|
2026-05-19 21:39:13 -05:00
|
|
|
fs.Usage = func() {
|
2026-05-22 23:01:27 -05:00
|
|
|
_, _ = fmt.Fprintln(out, "Usage: narratio session restore <session_id> [--config <path>] [--campaign <id>] [--campaign-file <path>] [--session <path>] [--previous-session-id <value>] [--dry-run] [--force] [--include-audio]")
|
2026-05-19 21:39:13 -05:00
|
|
|
_, _ = 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)
|
|
|
|
|
}
|
2026-05-23 13:47:39 +00:00
|
|
|
if err := resolveParsedSessionID("restore", positionalSessionID, fs, &flags.sessionID); err != nil {
|
|
|
|
|
return err
|
2026-05-22 22:09:17 -05:00
|
|
|
}
|
2026-05-23 13:47:39 +00:00
|
|
|
if strings.TrimSpace(flags.sessionID) == "" {
|
2026-05-22 22:09:17 -05:00
|
|
|
return fmt.Errorf("restore: session_id is required")
|
2026-05-19 21:39:13 -05:00
|
|
|
}
|
2026-09-07 04:13:58 +00:00
|
|
|
loaded, err := loadCommandConfigWithDependencies(ctx, flags.pipelinePath, flags.campaignPath, flags.campaignFilePath, flags.sessionPath, flags.sessionOptions(), flags.secretsDir.pointer(), deps)
|
2026-05-19 21:39:13 -05:00
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("restore: %w", err)
|
|
|
|
|
}
|
2026-08-10 21:41:00 +00:00
|
|
|
defer func() { _ = loaded.Close() }()
|
|
|
|
|
cfg := loaded.Config
|
2026-11-20 05:41:16 +00:00
|
|
|
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}, ""))
|
2026-05-19 22:06:48 -05:00
|
|
|
}
|
2026-11-20 05:41:16 +00:00
|
|
|
return err
|
2026-05-19 21:39:13 -05:00
|
|
|
}
|