From 251353380e8ded7e748e5da11f826d47a2769b94 Mon Sep 17 00:00:00 2001 From: Solomon Hykes Date: Wed, 26 Mar 2014 12:24:28 -0700 Subject: [PATCH] beam/examples/beamsh: utility function 'fileToConn' Docker-DCO-1.1-Signed-off-by: Solomon Hykes (github: shykes) --- beam/examples/beamsh/beamsh.go | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/beam/examples/beamsh/beamsh.go b/beam/examples/beamsh/beamsh.go index 60a13c7..837622d 100644 --- a/beam/examples/beamsh/beamsh.go +++ b/beam/examples/beamsh/beamsh.go @@ -290,16 +290,10 @@ func GetHandler(name string) Handler { beam.Send(out, data.Empty().Set("status", "1").Set("message", err.Error()).Bytes(), nil) return } - var f *os.File - if connWithFile, ok := conn.(interface { File() (*os.File, error) }); !ok { + f, err := connToFile(conn) + if err != nil { conn.Close() continue - } else { - f, err = connWithFile.File() - if err != nil { - conn.Close() - continue - } } beam.Send(out, data.Empty().Set("type", "socket").Set("remoteaddr", conn.RemoteAddr().String()).Bytes(), f) } @@ -369,6 +363,17 @@ func GetHandler(name string) Handler { return nil } +func connToFile(conn net.Conn) (f *os.File, err error) { + if connWithFile, ok := conn.(interface { File() (*os.File, error) }); !ok { + return nil, fmt.Errorf("no file descriptor available") + } else { + f, err = connWithFile.File() + if err != nil { + return nil, err + } + } + return f, err +} // 'status' is a notification of a job's status. //