(ctx context.Context, cmd *cobra.Command, inst *limatype.Instance, sshCmd *exec.Cmd, hostCurrentDir, destRsyncDir string, rsync copytool.CopyTool, tty bool)
| 445 | } |
| 446 | |
| 447 | func askUserForRsyncBack(ctx context.Context, cmd *cobra.Command, inst *limatype.Instance, sshCmd *exec.Cmd, hostCurrentDir, destRsyncDir string, rsync copytool.CopyTool, tty bool) error { |
| 448 | remoteSource := fmt.Sprintf("%s:%s", inst.Name, destRsyncDir) |
| 449 | clean := filepath.Clean(hostCurrentDir) |
| 450 | dirForCleanup := shellescape.Quote(filepath.Join(*inst.Config.User.Home, clean)) |
| 451 | cleanupGuestWorkdir := false |
| 452 | |
| 453 | rsyncBack := func() error { |
| 454 | paths := []string{ |
| 455 | remoteSource, |
| 456 | hostCurrentDir, |
| 457 | } |
| 458 | |
| 459 | if err := rsyncDirectory(ctx, cmd, rsync, paths); err != nil { |
| 460 | return fmt.Errorf("failed to sync back the changes from guest instance to host: %w", err) |
| 461 | } |
| 462 | logrus.Info("Successfully synced back the changes to host.") |
| 463 | cleanupGuestWorkdir = true |
| 464 | return nil |
| 465 | } |
| 466 | |
| 467 | defer func() { |
| 468 | if !cleanupGuestWorkdir { |
| 469 | return |
| 470 | } |
| 471 | // Clean up the guest synced workdir |
| 472 | if err := executeSSHForRsync(ctx, *sshCmd, inst.SSHLocalPort, inst.SSHAddress, fmt.Sprintf("rm -rf %s", dirForCleanup)); err != nil { |
| 473 | logrus.WithError(err).Warn("Failed to clean up guest synced workdir") |
| 474 | } |
| 475 | }() |
| 476 | |
| 477 | if !tty { |
| 478 | return rsyncBack() |
| 479 | } |
| 480 | |
| 481 | rawOutput, stats, err := getRsyncStats(ctx, remoteSource, filepath.Dir(hostCurrentDir)) |
| 482 | if err != nil { |
| 483 | logrus.WithError(err).Warn("failed to get rsync stats") |
| 484 | } |
| 485 | if stats != nil && stats.String() == "" { |
| 486 | logrus.Info("No changes detected") |
| 487 | cleanupGuestWorkdir = true |
| 488 | return nil |
| 489 | } |
| 490 | statsMsg := "" |
| 491 | if stats != nil { |
| 492 | if s := stats.String(); s != "" { |
| 493 | statsMsg = fmt.Sprintf(" (%s)", s) |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | message := fmt.Sprintf("⚠️ Accept the changes?%s", statsMsg) |
| 498 | options := []string{ |
| 499 | "Yes", |
| 500 | "No", |
| 501 | "View the changed contents", |
| 502 | } |
| 503 | |
| 504 | baseDir, err := os.MkdirTemp("", "lima-guest-synced-*") |
no test coverage detected