Skip to content

Commit

Permalink
feat: piping input via stdin
Browse files Browse the repository at this point in the history
  • Loading branch information
jpts committed Jun 22, 2023
1 parent 6711e61 commit 4ea47c8
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions cmd/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io/ioutil"
"net"
"net/http"
"os"
"strconv"
"sync"

Expand All @@ -21,6 +22,7 @@ type WebsocketRoundTripper struct {
Dialer *websocket.Dialer
TermState *TerminalState
SendBuffer bytes.Buffer
OneShot bool
}

type ApiServerError struct {
Expand Down Expand Up @@ -90,6 +92,33 @@ func (d *WebsocketRoundTripper) concurrentSend(wg *sync.WaitGroup, ws *websocket
buf := make([]byte, 1025)
stdIn, _, _ := term.StdStreams()

stdInFile, ok := stdIn.(*os.File)
if !ok {
errChan <- errors.New("Error determining input type")
return
}

stat, _ := stdInFile.Stat()
if (stat.Mode() & os.ModeCharDevice) == 0 {
d.OneShot = true

bytes, err := io.ReadAll(stdIn)
if err != nil {
errChan <- err
return
}

stdin := append([]byte{streamStdIn}, bytes...)
klog.V(4).Infof("got stdin %s", string(stdin))

err = ws.WriteMessage(websocket.BinaryMessage, stdin)
if err != nil {
errChan <- err
return
}
return
}

for {
n, err := stdIn.Read(buf[1:])
if err != nil {
Expand Down Expand Up @@ -149,6 +178,10 @@ func (d *WebsocketRoundTripper) concurrentRecv(wg *sync.WaitGroup, ws *websocket
errChan <- err
return
}

if d.OneShot {
break
}
}
d.SendBuffer.Reset()
}
Expand Down

0 comments on commit 4ea47c8

Please sign in to comment.