-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd_session.go
More file actions
185 lines (152 loc) 路 4.19 KB
/
Copy pathcmd_session.go
File metadata and controls
185 lines (152 loc) 路 4.19 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package sip
import (
"context"
"fmt"
"io"
"os"
"sync"
"time"
)
// cmdSession implements session handling for spawned commands (CLI mode).
// Unlike webSession which runs Bubble Tea in-process, cmdSession spawns
// an external command in a PTY and bridges its I/O to the browser.
type cmdSession struct {
id string
platform *cmdPlatformPty
cols int
rows int
cancelFunc context.CancelFunc
ctx context.Context
mu sync.Mutex
closed bool
startTime time.Time
windowChanges chan WindowSize
}
func (s *cmdSession) Pty() Pty {
s.mu.Lock()
defer s.mu.Unlock()
return Pty{Width: s.cols, Height: s.rows}
}
func (s *cmdSession) Context() context.Context {
return s.ctx
}
func (s *cmdSession) Read(p []byte) (n int, err error) {
return s.platform.OutputReader().Read(p)
}
func (s *cmdSession) Write(p []byte) (n int, err error) {
return s.platform.InputWriter().Write(p)
}
func (s *cmdSession) WindowChanges() <-chan WindowSize {
return s.windowChanges
}
func (s *cmdSession) Fd() uintptr {
return 0 // Not used for command sessions
}
func (s *cmdSession) PtySlave() *os.File {
return nil // Not used for command sessions
}
func (s *cmdSession) Done() <-chan struct{} {
return s.ctx.Done()
}
func (s *cmdSession) Resize(cols, rows int) {
s.mu.Lock()
s.cols = cols
s.rows = rows
s.mu.Unlock()
if s.platform != nil {
_ = s.platform.Resize(cols, rows)
}
select {
case s.windowChanges <- WindowSize{Width: cols, Height: rows}:
default:
}
}
// OutputReader returns the reader for terminal output (for handlers).
func (s *cmdSession) OutputReader() io.Reader {
return s.platform.OutputReader()
}
// InputWriter returns the writer for terminal input (for handlers).
func (s *cmdSession) InputWriter() io.Writer {
return s.platform.InputWriter()
}
// CommandHandler creates command sessions for each browser connection.
type CommandHandler struct {
name string
args []string
dir string
}
// ServeCommand starts the server and runs the specified command for each connection.
// This is used by the CLI to wrap arbitrary commands and expose them via the browser.
func (s *Server) ServeCommand(ctx context.Context, name string, args []string, dir string) error {
cmdHandler := &CommandHandler{
name: name,
args: args,
dir: dir,
}
s.server = newCmdHTTPServer(s.config, cmdHandler)
return s.server.start(ctx)
}
// newCmdHTTPServer creates an HTTP server for command sessions.
func newCmdHTTPServer(config Config, handler *CommandHandler) *httpServer {
srv := &httpServer{
config: config,
cmdHandler: handler,
}
return srv
}
func (srv *httpServer) createCmdSession(ctx context.Context, initialCols, initialRows int) (*cmdSession, error) {
if srv.cmdHandler == nil {
return nil, fmt.Errorf("no command handler configured")
}
cols, rows := initialCols, initialRows
if cols <= 0 {
cols = 80
}
if rows <= 0 {
rows = 24
}
logger.Debug("creating command session", "cols", cols, "rows", rows, "cmd", srv.cmdHandler.name)
platform, err := newCmdPlatformPty(srv.cmdHandler.name, srv.cmdHandler.args, srv.cmdHandler.dir, cols, rows)
if err != nil {
return nil, fmt.Errorf("failed to create command PTY: %w", err)
}
sessionCtx, cancel := context.WithCancel(ctx)
windowChanges := make(chan WindowSize, 1)
session := &cmdSession{
id: fmt.Sprintf("%d", time.Now().UnixNano()),
platform: platform,
cols: cols,
rows: rows,
cancelFunc: cancel,
ctx: sessionCtx,
startTime: time.Now(),
windowChanges: windowChanges,
}
// Monitor process exit
go func() {
_ = platform.Wait()
cancel()
}()
srv.sessions.Store(session.id, session)
logger.Debug("command session created", "session", session.id)
return session, nil
}
func (srv *httpServer) closeCmdSession(session *cmdSession) {
session.mu.Lock()
if session.closed {
session.mu.Unlock()
return
}
session.closed = true
session.mu.Unlock()
duration := time.Since(session.startTime)
session.cancelFunc()
if session.platform != nil {
_ = session.platform.Close()
}
srv.sessions.Delete(session.id)
logger.Debug("command session closed",
"session", session.id,
"duration", duration.Round(time.Millisecond),
)
}