/
filesystem.go
96 lines (75 loc) · 2.42 KB
/
filesystem.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright 2020 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package modelcmd
import (
"io"
"os"
"github.com/juju/errors"
)
// ReadSeekCloser is a minimal interface used for os.File.
type ReadSeekCloser interface {
io.ReadCloser
io.Seeker
}
// Filesystem is an interface providing access to the filesystem,
// either delegating to calling os functions or functions which
// always return an error.
type Filesystem interface {
Stat(name string) (os.FileInfo, error)
Open(name string) (ReadSeekCloser, error)
OpenFile(name string, flag int, perm os.FileMode) (*os.File, error)
Create(name string) (*os.File, error)
RemoveAll(path string) error
}
type osFilesystem struct{}
func (osFilesystem) Create(name string) (*os.File, error) {
return os.Create(name)
}
func (osFilesystem) RemoveAll(path string) error {
return os.RemoveAll(path)
}
func (osFilesystem) Open(name string) (ReadSeekCloser, error) {
return os.Open(name)
}
func (osFilesystem) OpenFile(name string, flag int, perm os.FileMode) (*os.File, error) {
return os.OpenFile(name, flag, perm)
}
func (osFilesystem) Stat(name string) (os.FileInfo, error) {
return os.Stat(name)
}
var notSupported = errors.NotSupportedf("access to filesystem")
type restrictedFilesystem struct{}
func (restrictedFilesystem) Create(string) (*os.File, error) {
return nil, notSupported
}
func (restrictedFilesystem) RemoveAll(string) error {
return notSupported
}
func (restrictedFilesystem) Open(string) (ReadSeekCloser, error) {
return nil, notSupported
}
func (restrictedFilesystem) OpenFile(string, int, os.FileMode) (*os.File, error) {
return nil, notSupported
}
func (restrictedFilesystem) Stat(string) (os.FileInfo, error) {
return nil, notSupported
}
// FilesystemCommand is embedded in commands that need access to the filesystem.
type FilesystemCommand struct {
// filesystem provides access to os calls to access files.
// For embedded commands, methods will always return an error.
filesystem Filesystem
}
// SetFilesystem sets the Filesystem instance on the command.
func (c *FilesystemCommand) SetFilesystem(fs Filesystem) {
c.filesystem = fs
}
// Filesystem returns an instance that provides access to
// the filesystem, either delegating to calling os functions
// or functions which always return an error.
func (c *FilesystemCommand) Filesystem() Filesystem {
if c.filesystem == nil {
c.filesystem = osFilesystem{}
}
return c.filesystem
}