-
Notifications
You must be signed in to change notification settings - Fork 20
/
scp.go
138 lines (112 loc) · 3.1 KB
/
scp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package cmd
import (
"fmt"
"os"
"os/exec"
"strings"
"github.com/kballard/go-shellquote"
"github.com/spf13/cobra"
)
type scpInfo struct {
sshInfo
recursive bool
replStr string
}
var scpCmd = &cobra.Command{
Use: "scp <instance name | ID> <source> <target>",
Short: "SCP files to/from a Compute instance",
Long: `This command executes the "scp" command to send or receive files to/from the
specified Compute instance. The target (or source, depending on the direction
of the transfer) must contain the "{}" marker which will be interpolated at
run time with the actual Compute instance IP address, similar to xargs(1).
This marker can be replaced by another string via the --replace-str|-i flag.
Example:
exo scp my-instance hello-world.txt {}:
exo scp -i%% my-instance %%:/etc/motd .
`,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 3 {
return cmd.Usage()
}
ipv6, err := cmd.Flags().GetBool("ipv6")
if err != nil {
return err
}
printCmd, err := cmd.Flags().GetBool("print")
if err != nil {
return err
}
recursive, err := cmd.Flags().GetBool("recursive")
if err != nil {
return err
}
replStr, err := cmd.Flags().GetString("replace-str")
if err != nil {
return err
}
sshOpts, err := cmd.Flags().GetString("ssh-options")
if err != nil {
return err
}
sshInfo, err := getSSHInfo(args[0], ipv6)
if err != nil {
return err
}
sshInfo.opts = sshOpts
scpCmd := buildSCPCommand(&scpInfo{
sshInfo: *sshInfo,
recursive: recursive,
replStr: replStr,
}, args[1:])
if printCmd {
fmt.Println(strings.Join(scpCmd, " "))
return nil
}
return runSCP(scpCmd[1:])
},
}
func buildSCPCommand(info *scpInfo, args []string) []string {
cmd := []string{"scp"}
if _, err := os.Stat(info.sshKeys); err == nil {
cmd = append(cmd, "-i", info.sshKeys)
}
if info.recursive {
cmd = append(cmd, "-r")
}
if info.opts != "" {
opts, err := shellquote.Split(info.opts)
if err == nil {
cmd = append(cmd, opts...)
}
}
// Parse arguments to find the replacement string to interpolate with <[username@]target instance IP address>
for i, a := range args {
if strings.Contains(a, info.replStr) {
remote := info.ip.String()
if info.username != "" {
remote = fmt.Sprintf("%s@%s", info.username, remote)
}
args[i] = strings.Replace(args[i], info.replStr, remote, 1)
break
}
}
cmd = append(cmd, args...)
return cmd
}
func runSCP(args []string) error {
cmd := exec.Command("scp", args...)
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
return cmd.Run()
}
func init() {
scpCmd.Flags().StringP("replace-str", "i", "{}",
"String to replace with the actual Compute instance information (i.e. username@<IP address>)")
scpCmd.Flags().StringP("ssh-options", "o", "",
"Additional options to pass to the `scp` command (e.g. -o \"-l my-user -p 2222\"`)")
scpCmd.Flags().BoolP("print", "p", false, "Print SCP command")
scpCmd.Flags().BoolP("recursive", "r", false, "Recursively copy entire directories")
scpCmd.Flags().BoolP("ipv6", "6", false, "Use IPv6")
RootCmd.AddCommand(scpCmd)
}