MCPcopy
hub / github.com/ContainerSSH/ContainerSSH / read

Function read

internal/sshserver/Server_test.go:468–494  ·  view source on GitHub ↗
(stdout io.Reader, stdin io.WriteCloser, session *ssh.Session)

Source from the content-addressed store, hash-verified

466}
467
468func read(stdout io.Reader, stdin io.WriteCloser, session *ssh.Session) (
469 []byte,
470 int,
471 error,
472) {
473 var exitStatus int
474 data := make([]byte, 4096)
475 n, err := stdout.Read(data)
476 if err != nil && !errors.Is(err, io.EOF) {
477 return nil, -1, fmt.Errorf("failed to read from stdout (%w)", err)
478 }
479 if err := stdin.Close(); err != nil && !errors.Is(err, io.EOF) {
480 return data[:n], -1, fmt.Errorf("failed to close stdin (%w)", err)
481 }
482 if err := session.Wait(); err != nil {
483 exitError := &ssh.ExitError{}
484 if errors.As(err, &exitError) {
485 exitStatus = exitError.ExitStatus()
486 } else {
487 return data[:n], -1, fmt.Errorf("failed to wait for exit (%w)", err)
488 }
489 }
490 if err := session.Close(); err != nil && !errors.Is(err, io.EOF) {
491 return data[:n], -1, fmt.Errorf("failed to close session (%w)", err)
492 }
493 return data[:n], exitStatus, nil
494}
495
496func createPipe(session *ssh.Session) (io.WriteCloser, io.Reader, error) {
497 stdin, err := session.StdinPipe()

Callers 1

shellRequestReplyFunction · 0.85

Calls 5

ReadMethod · 0.65
ErrorfMethod · 0.65
CloseMethod · 0.65
WaitMethod · 0.65
ExitStatusMethod · 0.65

Tested by

no test coverage detected