MCPcopy
hub / github.com/lima-vm/lima / askUserForRsyncBack

Function askUserForRsyncBack

cmd/limactl/shell.go:447–609  ·  view source on GitHub ↗
(ctx context.Context, cmd *cobra.Command, inst *limatype.Instance, sshCmd *exec.Cmd, hostCurrentDir, destRsyncDir string, rsync copytool.CopyTool, tty bool)

Source from the content-addressed store, hash-verified

445}
446
447func 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-*")

Callers 1

shellActionFunction · 0.85

Calls 11

SelectFunction · 0.92
rsyncDirectoryFunction · 0.85
executeSSHForRsyncFunction · 0.85
getRsyncStatsFunction · 0.85
pagerCommandFunction · 0.85
InfoMethod · 0.65
StartMethod · 0.65
StringMethod · 0.45
CloseMethod · 0.45
RunMethod · 0.45
ExitCodeMethod · 0.45

Tested by

no test coverage detected