@@ -673,24 +673,76 @@ func (client *HyperClient) Attach(opts AttachToContainerOptions) error {
673673 return fmt .Errorf ("No Such Container %s" , opts .Container )
674674 }
675675
676- tag := client .GetTag ()
677- v := url.Values {}
678- v .Set (KEY_TYPE , TYPE_CONTAINER )
679- v .Set (KEY_VALUE , opts .Container )
680- v .Set (KEY_TAG , tag )
681- path := "/attach?" + v .Encode ()
682- err := client .hijack ("POST" , path , hijackOptions {
683- in : opts .InputStream ,
684- stdout : opts .OutputStream ,
685- stderr : opts .ErrorStream ,
686- tty : opts .TTY ,
687- })
676+ stream , err := client .client .Attach (context .Background ())
677+ if err != nil {
678+ return err
679+ }
680+
681+ request := grpctypes.AttachMessage {
682+ ContainerID : opts .Container ,
683+ }
684+ if err := stream .Send (& request ); err != nil {
685+ return nil
686+ }
687+
688+ var recvStdoutError chan error
689+ if opts .OutputStream != nil {
690+ recvStdoutError = getReturnValue (func () error {
691+ for {
692+ res , err := stream .Recv ()
693+ if err != nil {
694+ if err == io .EOF {
695+ return nil
696+ }
697+ return err
698+ }
699+ n , err := opts .OutputStream .Write (res .Data )
700+ if err != nil {
701+ return err
702+ }
703+ if n != len (res .Data ) {
704+ return io .ErrShortWrite
705+ }
706+ }
707+ })
708+ }
709+
710+ var reqStdinError chan error
711+ if opts .InputStream != nil {
712+ reqStdinError = getReturnValue (func () error {
713+ for {
714+ req := make ([]byte , 512 )
715+ n , err := opts .InputStream .Read (req )
716+ if err := stream .Send (& grpctypes.AttachMessage {Data : req [:n ]}); err != nil {
717+ return err
718+ }
719+ if err == io .EOF {
720+ return nil
721+ }
722+ if err != nil {
723+ return err
724+ }
725+ }
726+ })
727+ }
728+
729+ if opts .OutputStream != nil && opts .InputStream != nil {
730+ select {
731+ case err = <- recvStdoutError :
732+ case err = <- reqStdinError :
733+ }
734+ } else if opts .OutputStream != nil {
735+ err = <- recvStdoutError
736+ } else if opts .InputStream != nil {
737+ err = <- reqStdinError
738+ }
688739
689740 if err != nil {
690741 return err
691742 }
692743
693- return client .GetExitCode (opts .Container , tag )
744+ //TODO: GetExitCode
745+ return nil
694746}
695747
696748func (c * HyperClient ) Exec (opts ExecInContainerOptions ) error {
0 commit comments