-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs.go
55 lines (50 loc) · 1.14 KB
/
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
50
51
52
53
54
55
package fssh
import (
"fmt"
"io/fs"
"os"
"path"
"github.com/jarxorg/gcsfs"
"github.com/jarxorg/s3fs"
"github.com/jarxorg/wfs"
"github.com/jarxorg/wfs/memfs"
"github.com/jarxorg/wfs/osfs"
)
// FS is writable FS.
type FS wfs.WriteFileFS
// NewFS parses nameUrl and creates a new FS according to the protocol.
func NewFS(filenameUrl string) (fsys FS, protocol string, host string, filename string, err error) {
protocol, host, filename, err = ParseURI(filenameUrl)
if err != nil {
return
}
switch protocol {
case "s3://":
fsys = s3fs.New(host)
case "gs://":
fsys = gcsfs.New(host)
case "mem://":
fsys = memfs.New()
err = fsys.MkdirAll(path.Join(host, filename), os.ModePerm)
default:
fsys = osfs.New(host)
}
return
}
// NewDirFS parses dirUrl and creates a new FS according to the protocol.
func NewDirFS(dirUrl string) (fsys FS, protocol string, host string, dir string, err error) {
fsys, protocol, host, dir, err = NewFS(dirUrl)
if err != nil {
return
}
var info fs.FileInfo
info, err = fs.Stat(fsys, dir)
if err != nil {
return
}
if !info.IsDir() {
err = fmt.Errorf("not directory: %s", dir)
return
}
return
}