Browse code

Move the terminal setup to execdriver, instead of ./pkg/term

It is independent of any particular driver, but likely used by
multiple execdrivers. Also, pkg/... is not to have any links to
docker, which this terminal setup does.

Docker-DCO-1.1-Signed-off-by: Vincent Batts <vbatts@redhat.com> (github: vbatts)

Vincent Batts authored on 2014/02/25 05:28:45
Showing 2 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,125 @@
0
+package execdriver
1
+
2
+import (
3
+	"github.com/dotcloud/docker/pkg/term"
4
+	"github.com/kr/pty"
5
+	"io"
6
+	"os"
7
+)
8
+
9
+func SetTerminal(command *Command, pipes *Pipes) error {
10
+	var (
11
+		term Terminal
12
+		err  error
13
+	)
14
+	if command.Tty {
15
+		term, err = NewTtyConsole(command, pipes)
16
+	} else {
17
+		term, err = NewStdConsole(command, pipes)
18
+	}
19
+	if err != nil {
20
+		return err
21
+	}
22
+	command.Terminal = term
23
+	return nil
24
+}
25
+
26
+type TtyConsole struct {
27
+	master *os.File
28
+	slave  *os.File
29
+}
30
+
31
+func NewTtyConsole(command *Command, pipes *Pipes) (*TtyConsole, error) {
32
+	ptyMaster, ptySlave, err := pty.Open()
33
+	if err != nil {
34
+		return nil, err
35
+	}
36
+	tty := &TtyConsole{
37
+		master: ptyMaster,
38
+		slave:  ptySlave,
39
+	}
40
+	if err := tty.attach(command, pipes); err != nil {
41
+		tty.Close()
42
+		return nil, err
43
+	}
44
+	return tty, nil
45
+}
46
+
47
+func (t *TtyConsole) Master() *os.File {
48
+	return t.master
49
+}
50
+
51
+func (t *TtyConsole) Resize(h, w int) error {
52
+	return term.SetWinsize(t.master.Fd(), &term.Winsize{Height: uint16(h), Width: uint16(w)})
53
+}
54
+
55
+func (t *TtyConsole) attach(command *Command, pipes *Pipes) error {
56
+	command.Stdout = t.slave
57
+	command.Stderr = t.slave
58
+	command.Console = t.slave.Name()
59
+
60
+	go func() {
61
+		if wb, ok := pipes.Stdout.(interface {
62
+			CloseWriters() error
63
+		}); ok {
64
+			defer wb.CloseWriters()
65
+		}
66
+		io.Copy(pipes.Stdout, t.master)
67
+	}()
68
+
69
+	if pipes.Stdin != nil {
70
+		command.Stdin = t.slave
71
+		command.SysProcAttr.Setctty = true
72
+
73
+		go func() {
74
+			defer pipes.Stdin.Close()
75
+			io.Copy(t.master, pipes.Stdin)
76
+		}()
77
+	}
78
+	return nil
79
+}
80
+
81
+func (t *TtyConsole) Close() error {
82
+	t.slave.Close()
83
+	return t.master.Close()
84
+}
85
+
86
+type StdConsole struct {
87
+}
88
+
89
+func NewStdConsole(command *Command, pipes *Pipes) (*StdConsole, error) {
90
+	std := &StdConsole{}
91
+
92
+	if err := std.attach(command, pipes); err != nil {
93
+		return nil, err
94
+	}
95
+	return std, nil
96
+}
97
+
98
+func (s *StdConsole) attach(command *Command, pipes *Pipes) error {
99
+	command.Stdout = pipes.Stdout
100
+	command.Stderr = pipes.Stderr
101
+
102
+	if pipes.Stdin != nil {
103
+		stdin, err := command.StdinPipe()
104
+		if err != nil {
105
+			return err
106
+		}
107
+
108
+		go func() {
109
+			defer stdin.Close()
110
+			io.Copy(stdin, pipes.Stdin)
111
+		}()
112
+	}
113
+	return nil
114
+}
115
+
116
+func (s *StdConsole) Resize(h, w int) error {
117
+	// we do not need to reside a non tty
118
+	return nil
119
+}
120
+
121
+func (s *StdConsole) Close() error {
122
+	// nothing to close here
123
+	return nil
124
+}
0 125
deleted file mode 100644
... ...
@@ -1,125 +0,0 @@
1
-package term
2
-
3
-import (
4
-	"github.com/dotcloud/docker/execdriver"
5
-	"github.com/kr/pty"
6
-	"io"
7
-	"os"
8
-)
9
-
10
-func SetTerminal(command *execdriver.Command, pipes *execdriver.Pipes) error {
11
-	var (
12
-		term execdriver.Terminal
13
-		err  error
14
-	)
15
-	if command.Tty {
16
-		term, err = NewTtyConsole(command, pipes)
17
-	} else {
18
-		term, err = NewStdConsole(command, pipes)
19
-	}
20
-	if err != nil {
21
-		return err
22
-	}
23
-	command.Terminal = term
24
-	return nil
25
-}
26
-
27
-type TtyConsole struct {
28
-	master *os.File
29
-	slave  *os.File
30
-}
31
-
32
-func NewTtyConsole(command *execdriver.Command, pipes *execdriver.Pipes) (*TtyConsole, error) {
33
-	ptyMaster, ptySlave, err := pty.Open()
34
-	if err != nil {
35
-		return nil, err
36
-	}
37
-	tty := &TtyConsole{
38
-		master: ptyMaster,
39
-		slave:  ptySlave,
40
-	}
41
-	if err := tty.attach(command, pipes); err != nil {
42
-		tty.Close()
43
-		return nil, err
44
-	}
45
-	return tty, nil
46
-}
47
-
48
-func (t *TtyConsole) Master() *os.File {
49
-	return t.master
50
-}
51
-
52
-func (t *TtyConsole) Resize(h, w int) error {
53
-	return SetWinsize(t.master.Fd(), &Winsize{Height: uint16(h), Width: uint16(w)})
54
-}
55
-
56
-func (t *TtyConsole) attach(command *execdriver.Command, pipes *execdriver.Pipes) error {
57
-	command.Stdout = t.slave
58
-	command.Stderr = t.slave
59
-	command.Console = t.slave.Name()
60
-
61
-	go func() {
62
-		if wb, ok := pipes.Stdout.(interface {
63
-			CloseWriters() error
64
-		}); ok {
65
-			defer wb.CloseWriters()
66
-		}
67
-		io.Copy(pipes.Stdout, t.master)
68
-	}()
69
-
70
-	if pipes.Stdin != nil {
71
-		command.Stdin = t.slave
72
-		command.SysProcAttr.Setctty = true
73
-
74
-		go func() {
75
-			defer pipes.Stdin.Close()
76
-			io.Copy(t.master, pipes.Stdin)
77
-		}()
78
-	}
79
-	return nil
80
-}
81
-
82
-func (t *TtyConsole) Close() error {
83
-	t.slave.Close()
84
-	return t.master.Close()
85
-}
86
-
87
-type StdConsole struct {
88
-}
89
-
90
-func NewStdConsole(command *execdriver.Command, pipes *execdriver.Pipes) (*StdConsole, error) {
91
-	std := &StdConsole{}
92
-
93
-	if err := std.attach(command, pipes); err != nil {
94
-		return nil, err
95
-	}
96
-	return std, nil
97
-}
98
-
99
-func (s *StdConsole) attach(command *execdriver.Command, pipes *execdriver.Pipes) error {
100
-	command.Stdout = pipes.Stdout
101
-	command.Stderr = pipes.Stderr
102
-
103
-	if pipes.Stdin != nil {
104
-		stdin, err := command.StdinPipe()
105
-		if err != nil {
106
-			return err
107
-		}
108
-
109
-		go func() {
110
-			defer stdin.Close()
111
-			io.Copy(stdin, pipes.Stdin)
112
-		}()
113
-	}
114
-	return nil
115
-}
116
-
117
-func (s *StdConsole) Resize(h, w int) error {
118
-	// we do not need to reside a non tty
119
-	return nil
120
-}
121
-
122
-func (s *StdConsole) Close() error {
123
-	// nothing to close here
124
-	return nil
125
-}