Created
September 10, 2018 16:01
-
-
Save ashcrow/bd1f3df68262d6a07c5db57304c2d6e3 to your computer and use it in GitHub Desktop.
File System Client Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package daemon | |
import ( | |
"os" | |
) | |
// FileSystemClient abstracts file/directory manipulation operations | |
type FileSystemClient interface { | |
Create(string) (os.File, error) | |
Remove(string) error | |
MkdirAll(string, os.FileMode) error | |
Stat(string) (os.FileInfo, error) | |
Symlink(string, string) error | |
Chmod(os.FileMode) error | |
Chown(int, int) error | |
} | |
// fsClient is internally used to hang the FileSystemClient functions on. It | |
// can also be used to hang mocks for testing. | |
type fsClient struct { | |
Create func(string) (os.File, error) | |
Remove func(string) error | |
MkdirAll func(string, os.FileMode) error | |
Stat func(string) (os.FileInfo, error) | |
Symlink func(string, string) error | |
Chmod func(os.FileMode) error | |
Chown func(int, int) error | |
} | |
// NewFileSystemClient creates a new file system client using the default | |
// implementations provided by the os package. | |
func NewFileSystemClient() *fsClient { | |
d := fsClient{ | |
d.Create: os.Create, | |
d.Remove: os.Remove, | |
d.MkdirAll: os.MkdirAll, | |
d.Stat: os.Stat, | |
d.Symlink: os.Symlink, | |
d.Chmod: os.Chmod, | |
d.Chown: os.Chown, | |
} | |
return &d | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment