| 1 | |
| 2 | |
| 3 | |
| 4 | package copytool |
| 5 | |
| 6 | import ( |
| 7 | "context" |
| 8 | "errors" |
| 9 | "fmt" |
| 10 | "os/exec" |
| 11 | |
| 12 | "github.com/coreos/go-semver/semver" |
| 13 | |
| 14 | "github.com/lima-vm/lima/v2/pkg/limatype" |
| 15 | "github.com/lima-vm/lima/v2/pkg/sshutil" |
| 16 | ) |
| 17 | |
| 18 | type scpTool struct { |
| 19 | toolPath string |
| 20 | Options *Options |
| 21 | } |
| 22 | |
| 23 | func newSCPTool(opts *Options) (*scpTool, error) { |
| 24 | path, err := exec.LookPath("scp") |
| 25 | if err != nil { |
| 26 | return nil, fmt.Errorf("scp not found on host: %w", err) |
| 27 | } |
| 28 | return &scpTool{toolPath: path, Options: opts}, nil |
| 29 | } |
| 30 | |
| 31 | func (t *scpTool) Name() string { |
| 32 | return t.toolPath |
| 33 | } |
| 34 | |
| 35 | func (t *scpTool) IsAvailableOnGuest(_ context.Context, _ []string) bool { |
| 36 | |
| 37 | return true |
| 38 | } |
| 39 | |
| 40 | func (t *scpTool) Command(ctx context.Context, paths []string, opts *Options) (*exec.Cmd, error) { |
| 41 | copyPaths, err := parseCopyPaths(ctx, paths) |
| 42 | if err != nil { |
| 43 | return nil, err |
| 44 | } |
| 45 | |
| 46 | effectiveOpts := t.Options |
| 47 | if opts != nil { |
| 48 | effectiveOpts = opts |
| 49 | } |
| 50 | |
| 51 | instances := make(map[string]*limatype.Instance) |
| 52 | scpFlags := []string{} |
| 53 | scpArgs := []string{} |
| 54 | |
| 55 | if effectiveOpts.Verbose { |
| 56 | scpFlags = append(scpFlags, "-v") |
| 57 | } else { |
| 58 | scpFlags = append(scpFlags, "-q") |
| 59 | } |
| 60 | |
| 61 | if effectiveOpts.Recursive { |
| 62 | scpFlags = append(scpFlags, "-r") |
| 63 | } |
| 64 | |
| 65 | if effectiveOpts.AdditionalArgs != nil { |
| 66 | scpFlags = append(scpFlags, effectiveOpts.AdditionalArgs...) |
| 67 | } |
| 68 | |
| 69 | |
| 70 | sshExeForVersion, err := sshutil.NewSSHExe() |
| 71 | if err != nil { |
| 72 | return nil, err |
| 73 | } |
| 74 | legacySSH := sshutil.DetectOpenSSHVersion(ctx, sshExeForVersion).LessThan(*semver.New("8.0.0")) |
| 75 | |
| 76 | for _, cp := range copyPaths { |
| 77 | if cp.IsRemote { |
| 78 | if legacySSH { |
| 79 | scpFlags = append(scpFlags, "-P", fmt.Sprintf("%d", cp.Instance.SSHLocalPort)) |
| 80 | scpArgs = append(scpArgs, fmt.Sprintf("%s:%s", *cp.Instance.Config.User.Name+"@"+cp.Instance.SSHAddress, cp.Path)) |
| 81 | } else { |
| 82 | scpArgs = append(scpArgs, fmt.Sprintf("scp://%s:%d/%s", *cp.Instance.Config.User.Name+"@"+cp.Instance.SSHAddress, cp.Instance.SSHLocalPort, cp.Path)) |
| 83 | } |
| 84 | instances[cp.InstanceName] = cp.Instance |
| 85 | } else { |
| 86 | scpArgs = append(scpArgs, cp.Path) |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | if legacySSH && len(instances) > 1 { |
| 91 | return nil, errors.New("more than one (instance) host is involved in this command, this is only supported for openSSH v8.0 or higher") |
| 92 | } |
| 93 | |
| 94 | scpFlags = append(scpFlags, "-3", "--") |
| 95 | scpArgs = append(scpFlags, scpArgs...) |
| 96 | |
| 97 | var sshOpts []string |
| 98 | if len(instances) == 1 { |
| 99 | |
| 100 | |
| 101 | |
| 102 | for _, inst := range instances { |
| 103 | sshExe, err := sshutil.NewSSHExe() |
| 104 | if err != nil { |
| 105 | return nil, err |
| 106 | } |
| 107 | sshOpts, err = sshutil.SSHOpts(ctx, sshExe, inst.Dir, *inst.Config.User.Name, false, false, false, false) |
| 108 | if err != nil { |
| 109 | return nil, err |
| 110 | } |
| 111 | } |
| 112 | } else { |
| 113 | |
| 114 | sshExe, err := sshutil.NewSSHExe() |
| 115 | if err != nil { |
| 116 | return nil, err |
| 117 | } |
| 118 | sshOpts, err = sshutil.CommonOpts(ctx, sshExe, false) |
| 119 | if err != nil { |
| 120 | return nil, err |
| 121 | } |
| 122 | } |
| 123 | sshArgs := sshutil.SSHArgsFromOpts(sshOpts) |
| 124 | |
| 125 | return exec.CommandContext(ctx, t.toolPath, append(sshArgs, scpArgs...)...), nil |
| 126 | } |