hnakamur / go-scp

[Unmaintained] A scp client library written in Go
MIT License
41 stars 23 forks source link

Support reading files via `io.Reader` #27

Open dominik-lekse opened 2 years ago

dominik-lekse commented 2 years ago

Provide a function which allows to open files from the remote server and read from them via an io.Reader.

Description

The proposed function ReceiveOpen returns a FileInfo to the caller similar to Receive. The difference to Receive is, that writing to an io.Writer provided by the caller, the caller gets an io.Reader.

The main advantage is that it provides the consumer more control over reading process. This would allow better integration of go-scp with other golang modules. For example, the io.Reader can be wrapped in high level Reader implementations provided by the io package.

Proposed changes

Add the function ReceiveOpen:

// ReceiveOpen returns a FileInfo and a ReadCloser
func (*SCP) ReceiveOpen(srcFile string) (*FileInfo, io.ReadCloser, error)

Example usage


// Open file on remote server
reader, fileInfo, err := scp.NewSCP(c).ReceiveOpen(remotePath)
defer reader.Close()
if err != nil {
    panic(err)
}

// Read file data into slice
data, err := ioutil.ReadAll(reader)
if err != nil {
    panic(err)
}