/
file.go
60 lines (48 loc) · 1.6 KB
/
file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package ssh
import (
"context"
"fmt"
"github.com/neuspaces/terraform-provider-system/internal/cmd"
"github.com/neuspaces/terraform-provider-system/internal/system"
"io"
"io/fs"
)
// readFile implements fs.File for a known fs.FileInfo and an io.ReadCloser
type readFile struct {
fileInfo fs.FileInfo
readCloser io.ReadCloser
}
var _ fs.File = &readFile{}
func (f *readFile) Stat() (fs.FileInfo, error) {
return f.fileInfo, nil
}
func (f *readFile) Read(b []byte) (int, error) {
return f.readCloser.Read(b)
}
func (f *readFile) Close() error {
return f.readCloser.Close()
}
// newCatFileReader returns an io.ReadCloser which reads a file from a System using the `cat` command
func newCatFileReader(ctx context.Context, s system.System, name string) io.ReadCloser {
// Create pipe: pipe reader is returned to the caller; pipe writer captures stdout
pipeReader, pipeWriter := io.Pipe()
catCmd := cmd.NewCommand(fmt.Sprintf(`cat '%s'`, name), cmd.Stdout(pipeWriter))
go func() {
res, err := s.Execute(ctx, catCmd)
if err != nil {
// Error will be returned by Read of the PipeReader
_ = pipeWriter.CloseWithError(err)
return
}
if rc := res.ExitCode(); rc != 0 {
// Error will be returned by Read of the PipeReader
_ = pipeWriter.CloseWithError(fmt.Errorf("non-zero exit code: %d", rc))
return
}
// Read completed
_ = pipeWriter.Close()
}()
// TODO implement a lazy reader; i.e. start reading on first read call; cat reader
// TODO close if context is cancelled in separate goroutine; actually this should be the responsibility of Execute to close the Stdout
return pipeReader
}