-
Notifications
You must be signed in to change notification settings - Fork 162
/
scp_args.go
54 lines (39 loc) · 1.14 KB
/
scp_args.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
package ssh
import (
"fmt"
"strings"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshdir "github.com/cloudfoundry/bosh-cli/director"
)
type SCPArgs struct {
raw []string
recursive bool
}
func NewSCPArgs(rawArgs []string, recursive bool) SCPArgs {
return SCPArgs{raw: rawArgs, recursive: recursive}
}
func (a SCPArgs) AllOrInstanceGroupOrInstanceSlug() (boshdir.AllOrInstanceGroupOrInstanceSlug, error) {
for _, rawArg := range a.raw {
pieces := strings.SplitN(rawArg, ":", 2)
if len(pieces) == 2 {
return boshdir.NewAllOrInstanceGroupOrInstanceSlugFromString(pieces[0])
}
}
err := bosherr.Errorf("Missing remote host information in source/destination arguments")
return boshdir.AllOrInstanceGroupOrInstanceSlug{}, err
}
func (a SCPArgs) ForHost(host boshdir.Host) []string {
args := []string{}
if a.recursive {
args = append(args, "-r")
}
for _, rawArg := range a.raw {
pieces := strings.SplitN(rawArg, ":", 2)
if len(pieces) == 2 {
// Resolve named host to actual user@ip
pieces[0] = fmt.Sprintf("%s@%s", host.Username, host.Host)
}
args = append(args, strings.Join(pieces, ":"))
}
return args
}