forked from docker/machine
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ssh.go
51 lines (40 loc) · 975 Bytes
/
ssh.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
package commands
import (
"fmt"
"github.com/docker/machine/libmachine"
"github.com/docker/machine/libmachine/state"
)
type errStateInvalidForSSH struct {
HostName string
}
func (e errStateInvalidForSSH) Error() string {
return fmt.Sprintf("Error: Cannot run SSH command: Host %q is not running", e.HostName)
}
func cmdSSH(c CommandLine, api libmachine.API) error {
// Check for help flag -- Needed due to SkipFlagParsing
firstArg := c.Args().First()
if firstArg == "-help" || firstArg == "--help" || firstArg == "-h" {
c.ShowHelp()
return nil
}
target, err := targetHost(c, api)
if err != nil {
return err
}
host, err := api.Load(target)
if err != nil {
return err
}
currentState, err := host.Driver.GetState()
if err != nil {
return err
}
if currentState != state.Running {
return errStateInvalidForSSH{host.Name}
}
client, err := host.CreateSSHClient()
if err != nil {
return err
}
return client.Shell(c.Args().Tail()...)
}