-
Notifications
You must be signed in to change notification settings - Fork 13
/
fs.go
49 lines (41 loc) · 828 Bytes
/
fs.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
package main
import (
"bytes"
"errors"
"os"
"os/exec"
)
type FS interface {
Open(name string) (File, error)
}
type File interface {
Stat() (os.FileInfo, error)
Read([]byte) (int, error)
Close() error
}
// memfile is an in-memory file
type memfile struct {
*bytes.Buffer
}
func (m memfile) Close() error {
m.Buffer = nil
return nil
}
func (m memfile) Stat() (os.FileInfo, error) {
return nil, errors.New("memfile does not support stat")
}
// gitfs implements the FS interface for files at a specific git revision.
type gitfs struct {
cwd string
rev string
}
func (g *gitfs) Open(name string) (File, error) {
cmd := exec.Command("git", "-C", g.cwd, "show", g.rev+":"+name)
buf, err := cmd.Output()
if err != nil {
return nil, os.ErrNotExist
}
return memfile{
Buffer: bytes.NewBuffer(buf),
}, nil
}