/
custom_noninteractive.go
36 lines (31 loc) · 1.06 KB
/
custom_noninteractive.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
package ssh
import (
boshdir "github.com/cloudfoundry/bosh-cli/director"
boshssh "github.com/cloudfoundry/bosh-cli/ssh"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshsys "github.com/cloudfoundry/bosh-utils/system"
"os"
)
type CustomNonInteractiveRunner struct {
customRunner CustomRunner
}
func NewCustomNonInteractiveRunner(customRunner CustomRunner) CustomNonInteractiveRunner {
return CustomNonInteractiveRunner{customRunner}
}
func (r CustomNonInteractiveRunner) Run(connOpts boshssh.ConnectionOpts, result boshdir.SSHResult, rawCmd []string) error {
if len(result.Hosts) == 0 {
return bosherr.Errorf("Non-interactive SSH expects at least one host")
}
if len(rawCmd) == 0 {
return bosherr.Errorf("Non-interactive SSH expects non-empty command")
}
cmdFactory := func(host boshdir.Host) boshsys.Command {
return boshsys.Command{
Name: "ssh",
Args: append([]string{host.Host, "-l", host.Username}, rawCmd...),
KeepAttached: true,
Stdin: os.Stdin,
}
}
return r.customRunner.Run(connOpts, result, cmdFactory)
}