Skip to content

Commit

Permalink
add rsync flag option to copy files using rsync
Browse files Browse the repository at this point in the history
Signed-off-by: olalekan odukoya <[email protected]>
  • Loading branch information
olamilekan000 committed Jan 23, 2025
1 parent b2121e5 commit 4b57bc6
Showing 1 changed file with 41 additions and 7 deletions.
48 changes: 41 additions & 7 deletions cmd/limactl/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func newCopyCommand() *cobra.Command {

copyCommand.Flags().BoolP("recursive", "r", false, "copy directories recursively")
copyCommand.Flags().BoolP("verbose", "v", false, "enable verbose output")
copyCommand.Flags().BoolP("rsync", "", false, "use rsync for copying instead of scp")

return copyCommand
}
Expand All @@ -49,10 +50,24 @@ func copyAction(cmd *cobra.Command, args []string) error {
return err
}

arg0, err := exec.LookPath("scp")
useRsync, err := cmd.Flags().GetBool("rsync")
if err != nil {
return err
}

var arg0 string
if useRsync {
arg0, err = exec.LookPath("rsync")
if err != nil {
return err
}
} else {
arg0, err = exec.LookPath("scp")
if err != nil {
return err
}
}

instances := make(map[string]*store.Instance)
scpFlags := []string{}
scpArgs := []string{}
Expand All @@ -67,6 +82,9 @@ func copyAction(cmd *cobra.Command, args []string) error {

if verbose {
scpFlags = append(scpFlags, "-v")
if useRsync {
scpFlags = append(scpFlags, "--progress")
}
} else {
scpFlags = append(scpFlags, "-q")
}
Expand All @@ -93,11 +111,15 @@ func copyAction(cmd *cobra.Command, args []string) error {
if inst.Status == store.StatusStopped {
return fmt.Errorf("instance %q is stopped, run `limactl start %s` to start the instance", instName, instName)
}
if legacySSH {
scpFlags = append(scpFlags, "-P", fmt.Sprintf("%d", inst.SSHLocalPort))
if useRsync {
scpArgs = append(scpArgs, fmt.Sprintf("%[email protected]:%s", *inst.Config.User.Name, path[1]))
} else {
scpArgs = append(scpArgs, fmt.Sprintf("scp://%[email protected]:%d/%s", *inst.Config.User.Name, inst.SSHLocalPort, path[1]))
if legacySSH {
scpFlags = append(scpFlags, "-P", fmt.Sprintf("%d", inst.SSHLocalPort))
scpArgs = append(scpArgs, fmt.Sprintf("%[email protected]:%s", *inst.Config.User.Name, path[1]))
} else {
scpArgs = append(scpArgs, fmt.Sprintf("scp://%[email protected]:%d/%s", *inst.Config.User.Name, inst.SSHLocalPort, path[1]))
}
}
instances[instName] = inst
default:
Expand All @@ -107,7 +129,9 @@ func copyAction(cmd *cobra.Command, args []string) error {
if legacySSH && len(instances) > 1 {
return errors.New("more than one (instance) host is involved in this command, this is only supported for openSSH v8.0 or higher")
}
scpFlags = append(scpFlags, "-3", "--")
if !useRsync {
scpFlags = append(scpFlags, "-3", "--")
}
scpArgs = append(scpFlags, scpArgs...)

var sshOpts []string
Expand All @@ -128,13 +152,23 @@ func copyAction(cmd *cobra.Command, args []string) error {
return err
}
}

var cmdArgs []string
sshArgs := sshutil.SSHArgsFromOpts(sshOpts)
if useRsync {
// for rsync, add the -e flag with SSH options
scpFlags = append(scpFlags, "-e", fmt.Sprintf("ssh %s", strings.Join(sshArgs, " ")))
cmdArgs = append(cmdArgs, append(scpFlags, scpArgs...)...)
} else {
// for scp, append SSH options and scpArgs
cmdArgs = append(cmdArgs, append(sshArgs, scpArgs...)...)
}

sshCmd := exec.Command(arg0, append(sshArgs, scpArgs...)...)
sshCmd := exec.Command(arg0, cmdArgs...)
sshCmd.Stdin = cmd.InOrStdin()
sshCmd.Stdout = cmd.OutOrStdout()
sshCmd.Stderr = cmd.ErrOrStderr()
logrus.Debugf("executing scp (may take a long time): %+v", sshCmd.Args)
logrus.Debugf("executing %s (may take a long time): %+v", arg0, sshCmd.Args)

// TODO: use syscall.Exec directly (results in losing tty?)
return sshCmd.Run()
Expand Down

0 comments on commit 4b57bc6

Please sign in to comment.