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