| 1 | |
| 2 | |
| 3 | |
| 4 | package main |
| 5 | |
| 6 | import ( |
| 7 | "github.com/sirupsen/logrus" |
| 8 | "github.com/spf13/cobra" |
| 9 | |
| 10 | "github.com/lima-vm/lima/v2/pkg/copytool" |
| 11 | ) |
| 12 | |
| 13 | const copyHelp = `Copy files between host and guest |
| 14 | |
| 15 | Prefix guest filenames with the instance name and a colon. |
| 16 | |
| 17 | Backends: |
| 18 | auto - Automatically selects the best available backend (rsync preferred, falls back to scp) |
| 19 | rsync - Uses rsync for faster transfers with resume capability (requires rsync on both host and guest) |
| 20 | scp - Uses scp for reliable transfers (always available) |
| 21 | |
| 22 | Not to be confused with 'limactl clone'. |
| 23 | ` |
| 24 | |
| 25 | const copyExample = ` |
| 26 | # Copy file from guest to host (auto backend) |
| 27 | limactl copy default:/etc/os-release . |
| 28 | |
| 29 | # Copy file from host to guest with verbose output |
| 30 | limactl copy -v myfile.txt default:/tmp/ |
| 31 | |
| 32 | # Copy directory recursively using rsync backend |
| 33 | limactl copy --backend=rsync -r ./mydir default:/tmp/ |
| 34 | |
| 35 | # Copy using scp backend specifically |
| 36 | limactl copy --backend=scp default:/var/log/app.log ./logs/ |
| 37 | |
| 38 | # Copy multiple files |
| 39 | limactl copy file1.txt file2.txt default:/tmp/ |
| 40 | ` |
| 41 | |
| 42 | func newCopyCommand() *cobra.Command { |
| 43 | copyCommand := &cobra.Command{ |
| 44 | Use: "copy SOURCE ... TARGET", |
| 45 | Aliases: []string{"cp"}, |
| 46 | Short: "Copy files between host and guest", |
| 47 | Long: copyHelp, |
| 48 | Example: copyExample, |
| 49 | Args: WrapArgsError(cobra.MinimumNArgs(2)), |
| 50 | RunE: copyAction, |
| 51 | GroupID: advancedCommand, |
| 52 | } |
| 53 | |
| 54 | copyCommand.Flags().BoolP("recursive", "r", false, "Copy directories recursively") |
| 55 | copyCommand.Flags().BoolP("verbose", "v", false, "Enable verbose output") |
| 56 | copyCommand.Flags().String("backend", "auto", "Copy backend (scp|rsync|auto)") |
| 57 | |
| 58 | return copyCommand |
| 59 | } |
| 60 | |
| 61 | func copyAction(cmd *cobra.Command, args []string) error { |
| 62 | ctx := cmd.Context() |
| 63 | recursive, err := cmd.Flags().GetBool("recursive") |
| 64 | if err != nil { |
| 65 | return err |
| 66 | } |
| 67 | |
| 68 | verbose, err := cmd.Flags().GetBool("verbose") |
| 69 | if err != nil { |
| 70 | return err |
| 71 | } |
| 72 | |
| 73 | debug, err := cmd.Flags().GetBool("debug") |
| 74 | if err != nil { |
| 75 | return err |
| 76 | } |
| 77 | |
| 78 | if debug { |
| 79 | verbose = true |
| 80 | } |
| 81 | |
| 82 | backend, err := cmd.Flags().GetString("backend") |
| 83 | if err != nil { |
| 84 | return err |
| 85 | } |
| 86 | |
| 87 | cpTool, err := copytool.New(ctx, backend, args, ©tool.Options{ |
| 88 | Recursive: recursive, |
| 89 | Verbose: verbose, |
| 90 | }) |
| 91 | if err != nil { |
| 92 | return err |
| 93 | } |
| 94 | logrus.Debugf("using copy tool %#q", cpTool.Name()) |
| 95 | |
| 96 | copyCmd, err := cpTool.Command(ctx, args, nil) |
| 97 | if err != nil { |
| 98 | return err |
| 99 | } |
| 100 | |
| 101 | copyCmd.Stdin = cmd.InOrStdin() |
| 102 | copyCmd.Stdout = cmd.OutOrStdout() |
| 103 | copyCmd.Stderr = cmd.ErrOrStderr() |
| 104 | logrus.Debugf("executing %v (may take a long time)", copyCmd) |
| 105 | |
| 106 | TODO: |
| 107 | return copyCmd.Run() |
| 108 | } |