/
shell.go
69 lines (55 loc) · 1.5 KB
/
shell.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
package internal
import (
"os"
"golang.org/x/crypto/ssh"
"golang.org/x/term"
)
type Shell struct {
Command string
}
func (s *Shell) HandleSession(client *ssh.Client) error {
session, err := client.NewSession()
if err != nil {
return err
}
defer session.Close()
// Redirects local terminal's I/O to remote interactive shell.
session.Stdout = os.Stdout
session.Stderr = os.Stderr
session.Stdin = os.Stdin
inFd := int(os.Stdin.Fd())
outFd := int(os.Stdout.Fd())
if term.IsTerminal(outFd) {
// If starting an interactive shell from a local terminal, first places
// the terminal in raw mode so that characters sent to the terminal are
// sent directly to the SSH process. This allows terminal control
// signals like ^C and ^D, tab completion, and command navigation via
// the arrow keys to work as expected.
prevState, err := term.MakeRaw(inFd)
if err != nil {
return err
}
defer term.Restore(inFd, prevState)
width, height, err := term.GetSize(outFd)
if err != nil {
return err
}
// Enables echoing (prints out characters while typing).
modes := ssh.TerminalModes{ssh.ECHO: 1}
// Starts an interactive terminal with ANSI coloring and the same
// dimensions as the local terminal.
if err := session.RequestPty("xterm-256color", height, width, modes); err != nil {
return err
}
}
if s.Command != "" {
return session.Run(s.Command)
}
if err := session.Shell(); err != nil {
return err
}
if err := session.Wait(); err != nil {
return err
}
return nil
}