55 "encoding/json"
66 "errors"
77 "fmt"
8+ "io"
89 "io/ioutil"
910 "net/http"
1011 "os"
@@ -817,14 +818,27 @@ func HandleKubernetesRuntime(
817818
818819 fmt .Printf ("\n " )
819820 //note: blocks until done streaming or failure...
820- err = attach .StreamWithContext (
821- ctx ,
822- remotecommand.StreamOptions {
823- Stdin : os .Stdin ,
824- Stdout : os .Stdout ,
825- Stderr : os .Stderr ,
826- Tty : doTTY ,
827- })
821+ if commandParams .TUI {
822+ // TODO - move KubeComm off of command params
823+ reader := & KubeReader {inputChan : commandParams .KubeComm .InputChan }
824+ err = attach .StreamWithContext (
825+ ctx ,
826+ remotecommand.StreamOptions {
827+ Stdin : reader ,
828+ Stdout : os .Stdout ,
829+ Stderr : os .Stderr ,
830+ Tty : true , // Later on we may parse this in TUI mode.
831+ })
832+ } else {
833+ err = attach .StreamWithContext (
834+ ctx ,
835+ remotecommand.StreamOptions {
836+ Stdin : os .Stdin ,
837+ Stdout : os .Stdout ,
838+ Stderr : os .Stderr ,
839+ Tty : doTTY ,
840+ })
841+ }
828842
829843 if err != nil {
830844 if apierrors .IsNotFound (err ) {
@@ -844,6 +858,49 @@ func HandleKubernetesRuntime(
844858 }
845859}
846860
861+ // NOTE -> this input channel reader will be genericized
862+ // as per the comment in `debug/tui.go`.
863+ // An InputReader usable by Docker, Podman, Kubernetes, and Containerd
864+ // will be added to this directory.
865+ type KubeReader struct {
866+ inputChan chan InputKey
867+ }
868+
869+ func (kr * KubeReader ) Read (p []byte ) (n int , err error ) {
870+ inputKey , ok := <- kr .inputChan
871+ if ! ok {
872+ return 0 , io .EOF
873+ }
874+ log .Debugf ("KubeReader received inputKey %v" , inputKey )
875+ switch inputKey .Special {
876+ case NotSpecial :
877+ p [0 ] = byte (inputKey .Rune )
878+ return 1 , nil
879+ case Enter :
880+ p [0 ] = '\n'
881+ return 1 , nil
882+ case Backspace :
883+ p [0 ] = 127
884+ return 1 , nil
885+ case Up :
886+ copy (p , []byte {27 , 91 , 65 }) // ESC [ A
887+ return 3 , nil
888+ case Down :
889+ copy (p , []byte {27 , 91 , 66 }) // ESC [ B
890+ return 3 , nil
891+ case Left :
892+ copy (p , []byte {27 , 91 , 68 }) // ESC [ D
893+ return 3 , nil
894+ case Right :
895+ copy (p , []byte {27 , 91 , 67 }) // ESC [ C
896+ return 3 , nil
897+ default :
898+ log .Debugf ("Unhandled inputKey %v" , inputKey )
899+ // Handle other special keys or return an error
900+ return 0 , fmt .Errorf ("unsupported special key" )
901+ }
902+ }
903+
847904func listNamespaces (ctx context.Context , api * kubernetes.Clientset ) ([]string , error ) {
848905 namespaces , err := api .CoreV1 ().Namespaces ().List (ctx , metav1.ListOptions {})
849906 if err != nil {
0 commit comments