This repository has been archived by the owner on Aug 12, 2021. It is now read-only.
forked from machine-drivers/docker-machine-driver-hyperkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add cp command to transfer file in and out of a machine
Uses scp syntax; a ':' prefix will be replaced by 'user@host:'. Signed-off-by: Jan Dubois <jan.dubois@suse.com>
- Loading branch information
Showing
1 changed file
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/docker/machine/libmachine/state" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func init() { | ||
rootCmd.AddCommand(cpCmd) | ||
} | ||
|
||
var cpCmd = &cobra.Command{ | ||
Use: "cp [SOURCE] [DEST]", | ||
Short: "Copy files into or out of a machine.", | ||
Long: `Copy files into or out of a machine.`, | ||
Args: cobra.ExactArgs(2), | ||
RunE: cpCommand, | ||
} | ||
|
||
func cpCommand(cmd *cobra.Command, args []string) error { | ||
api := newAPI() | ||
defer api.Close() | ||
|
||
host, err := api.Load(machineName) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
currentState, err := host.Driver.GetState() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if currentState != state.Running { | ||
return fmt.Errorf("cannot run cp command: Host %q is not running", host.Name) | ||
} | ||
|
||
port, err := host.Driver.GetSSHPort() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
scpArgs := []string{ | ||
"-P", strconv.Itoa(port), | ||
"-i", host.Driver.GetSSHKeyPath(), | ||
"-o", "IdentitiesOnly=yes", | ||
"-o", "LogLevel=quiet", | ||
"-o", "PasswordAuthentication=no", | ||
"-o", "StrictHostKeyChecking=no", | ||
"-o", "UserKnownHostsFile=/dev/null", | ||
} | ||
|
||
sshUserame := host.Driver.GetSSHUsername() | ||
sshHostname, err := host.Driver.GetSSHHostname() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, arg := range args { | ||
if strings.HasPrefix(arg, ":") { | ||
scpArgs = append(scpArgs, fmt.Sprintf("%s@%s%s", sshUserame, sshHostname, arg)) | ||
} else { | ||
scpArgs = append(scpArgs, arg) | ||
} | ||
} | ||
|
||
command := exec.Command("scp", scpArgs...) | ||
command.Stdout = os.Stdout | ||
command.Stderr = os.Stderr | ||
return command.Run() | ||
} |