MCPcopy
hub / github.com/wavetermdev/waveterm / testShellWithCommand

Function testShellWithCommand

cmd/test-conn/testutil.go:142–190  ·  view source on GitHub ↗
(connName string, cmd string, timeout time.Duration)

Source from the content-addressed store, hash-verified

140}
141
142func testShellWithCommand(connName string, cmd string, timeout time.Duration) error {
143 opts, err := remote.ParseOpts(connName)
144 if err != nil {
145 return fmt.Errorf("failed to parse connection string: %w", err)
146 }
147
148 log.Printf("Connecting to %s...", opts.String())
149
150 conn := conncontroller.GetConn(opts)
151 ctx, cancel := context.WithTimeout(context.Background(), timeout)
152 defer cancel()
153
154 err = conn.Connect(ctx, &wconfig.ConnKeywords{})
155 if err != nil {
156 return fmt.Errorf("connection failed: %w", err)
157 }
158
159 log.Printf("✓ Connected! Starting shell...")
160
161 termSize := waveobj.TermSize{Rows: 24, Cols: 80}
162 shellProc, err := shellexec.StartRemoteShellProcNoWsh(ctx, termSize, "", shellexec.CommandOptsType{}, conn)
163 if err != nil {
164 return fmt.Errorf("failed to start shell: %w", err)
165 }
166 defer shellProc.Close()
167
168 log.Printf("✓ Shell started! Executing: %s", cmd)
169
170 _, err = shellProc.Cmd.Write([]byte(cmd + "\n"))
171 if err != nil {
172 return fmt.Errorf("failed to write command: %w", err)
173 }
174
175 time.Sleep(500 * time.Millisecond)
176
177 buf := make([]byte, 8192)
178 n, err := shellProc.Cmd.Read(buf)
179 if err != nil {
180 log.Printf("Warning: read error (may be expected): %v", err)
181 }
182
183 if n > 0 {
184 log.Printf("\n--- Output ---\n%s\n--- End Output ---", string(buf[:n]))
185 } else {
186 log.Printf("No output received (timeout or no data)")
187 }
188
189 return nil
190}
191
192func testWshExec(connName string, cmd string, timeout time.Duration) error {
193 opts, err := remote.ParseOpts(connName)

Callers 1

mainFunction · 0.85

Calls 8

ParseOptsFunction · 0.92
GetConnFunction · 0.92
CloseMethod · 0.65
WriteMethod · 0.65
StringMethod · 0.45
ConnectMethod · 0.45
ReadMethod · 0.45

Tested by

no test coverage detected