ssh.Session should implement io.ReaderWriter

227 views
Skip to first unread message

Brian McCallister

unread,
Feb 8, 2013, 5:42:56 PM2/8/13
to golan...@googlegroups.com
I'd like to propose that ssh.Session (1) to implement io.Reader and io.Writer against the stdout and stdin pipes. This would let the session be used in all the nice places that sockets, files, and so on can now.

minux

unread,
Feb 8, 2013, 6:23:57 PM2/8/13
to Brian McCallister, golan...@googlegroups.com

On Sat, Feb 9, 2013 at 6:42 AM, Brian McCallister <bri...@skife.org> wrote:
I'd like to propose that ssh.Session (1) to implement io.Reader and io.Writer against the stdout and stdin pipes. This would let the session be used in all the nice places that sockets, files, and so on can now.
because there are two io.Writers in Session, making Session a io.ReadWriter is ambiguous
as to which io.Writer the writes should go?

however, you can achieve your goal with something like this:
var _ io.ReadWriter = struct {
io.Reader
io.Writer
}{sess.Stdin, sess.Stdout}

Brian McCallister

unread,
Feb 8, 2013, 6:31:57 PM2/8/13
to minux, golan...@googlegroups.com
On Fri, Feb 8, 2013 at 4:23 PM, minux <minu...@gmail.com> wrote:

On Sat, Feb 9, 2013 at 6:42 AM, Brian McCallister <bri...@skife.org> wrote:
I'd like to propose that ssh.Session (1) to implement io.Reader and io.Writer against the stdout and stdin pipes. This would let the session be used in all the nice places that sockets, files, and so on can now.
because there are two io.Writers in Session, making Session a io.ReadWriter is ambiguous
as to which io.Writer the writes should go?

Writes should always go to Stdin for the session, reads should come from Stdout. Stderr is for errors, not normal communication.
 
however, you can achieve your goal with something like this:
var _ io.ReadWriter = struct {
io.Reader
io.Writer
}{sess.Stdin, sess.Stdout}

Which is (almost exactly) what I did :-)

Dave Cheney

unread,
Feb 8, 2013, 6:34:18 PM2/8/13
to Brian McCallister, golan...@googlegroups.com
One my the goals for the ssh.Session type was to emulate the exec.Cmd
so that, with some limitations, remote commands and local commands
would behave the same. As minux points out exec.Cmd has two outputs
and one input, so there is an ambiguity that needs to be resolved.
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Brian McCallister

unread,
Feb 8, 2013, 6:40:01 PM2/8/13
to Dave Cheney, golan...@googlegroups.com
On Fri, Feb 8, 2013 at 4:34 PM, Dave Cheney <da...@cheney.net> wrote:
One my the goals for the ssh.Session type was to emulate the exec.Cmd
so that, with some limitations, remote commands and local commands
would behave the same. As minux points out exec.Cmd has two outputs
and one input, so there is an ambiguity that needs to be resolved.

I am not proposing getting rid of Stdin, Stdout, or Stderr, just exposing the Session as a ReaderWriterCloser (it already has Closer) for the common case (Stdin and Stdout).

-Brian

ps: I would make the same proposal for exec.Cmd, actually.

Dave Cheney

unread,
Feb 8, 2013, 6:47:00 PM2/8/13
to Brian McCallister, golan...@googlegroups.com
> I am not proposing getting rid of Stdin, Stdout, or Stderr, just exposing
> the Session as a ReaderWriterCloser (it already has Closer) for the common
> case (Stdin and Stdout).

I'm hesitant to do that because what might be the common case for you,
may be uncommon for others. I believe that scp uses the stderr channel
for signalling, and probably other examples that I'm not aware of. If
you want to propose a CL to implement that then lets see what people
think.

> -Brian
>
> ps: I would make the same proposal for exec.Cmd, actually.

I can't speak for the Authors, but I would imagine this having a low
chance of being merged. Go 1.1 feature window is closed, and this is
*almost* a change to the Go 1 API contract.

A counter proposal would be a new method in io/ioutil, func
ReadWriteCloser(io.Reader, io.Writer, io.Closer) io.ReadWriteCloser,
but it's almost as many lines as just writing

s, _ := conn.NewSession()
rwc := struct{ io.ReadCloser, io.Writer } { s, s } // implements
io.ReadWriteCloser
Reply all
Reply to author
Forward
0 new messages