client.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package client
  2. import (
  3. "../future"
  4. "../rcli"
  5. "io"
  6. "log"
  7. "os"
  8. )
  9. // Run docker in "simple mode": run a single command and return.
  10. func SimpleMode(args []string) error {
  11. var oldState *State
  12. var err error
  13. if IsTerminal(0) && os.Getenv("NORAW") == "" {
  14. oldState, err = MakeRaw(0)
  15. if err != nil {
  16. return err
  17. }
  18. defer Restore(0, oldState)
  19. }
  20. // FIXME: we want to use unix sockets here, but net.UnixConn doesn't expose
  21. // CloseWrite(), which we need to cleanly signal that stdin is closed without
  22. // closing the connection.
  23. // See http://code.google.com/p/go/issues/detail?id=3345
  24. conn, err := rcli.Call("tcp", "127.0.0.1:4242", args...)
  25. if err != nil {
  26. return err
  27. }
  28. receive_stdout := future.Go(func() error {
  29. _, err := io.Copy(os.Stdout, conn)
  30. return err
  31. })
  32. send_stdin := future.Go(func() error {
  33. _, err := io.Copy(conn, os.Stdin)
  34. if err := conn.CloseWrite(); err != nil {
  35. log.Printf("Couldn't send EOF: " + err.Error())
  36. }
  37. return err
  38. })
  39. if err := <-receive_stdout; err != nil {
  40. return err
  41. }
  42. if oldState != nil {
  43. Restore(0, oldState)
  44. }
  45. if !IsTerminal(0) {
  46. if err := <-send_stdin; err != nil {
  47. return err
  48. }
  49. }
  50. return nil
  51. }