mirror of https://github.com/spf13/afero.git
Format Readme.md code blocks as Go code
This commit is contained in:
parent
3a224e9827
commit
55789de153
138
README.md
138
README.md
|
@ -51,20 +51,20 @@ First use go get to install the latest version of the library.
|
||||||
$ go get github.com/spf13/afero
|
$ go get github.com/spf13/afero
|
||||||
|
|
||||||
Next include Afero in your application.
|
Next include Afero in your application.
|
||||||
|
```go
|
||||||
import "github.com/spf13/afero"
|
import "github.com/spf13/afero"
|
||||||
|
```
|
||||||
|
|
||||||
## Step 2: Declare a backend
|
## Step 2: Declare a backend
|
||||||
|
|
||||||
First define a package variable and set it to a pointer to a filesystem.
|
First define a package variable and set it to a pointer to a filesystem.
|
||||||
|
```go
|
||||||
|
var AppFs afero.Fs = &afero.MemMapFs{}
|
||||||
|
|
||||||
var AppFs afero.Fs = &afero.MemMapFs{}
|
or
|
||||||
|
|
||||||
or
|
|
||||||
|
|
||||||
var AppFs afero.Fs = &afero.OsFs{}
|
|
||||||
|
|
||||||
|
var AppFs afero.Fs = &afero.OsFs{}
|
||||||
|
```
|
||||||
It is important to note that if you repeat the composite literal you
|
It is important to note that if you repeat the composite literal you
|
||||||
will be using a completely new and isolated filesystem. In the case of
|
will be using a completely new and isolated filesystem. In the case of
|
||||||
OsFs it will still use the same underlying filesystem but will reduce
|
OsFs it will still use the same underlying filesystem but will reduce
|
||||||
|
@ -76,9 +76,9 @@ Throughout your application use any function and method like you normally
|
||||||
would.
|
would.
|
||||||
|
|
||||||
So if my application before had:
|
So if my application before had:
|
||||||
|
```go
|
||||||
os.Open('/tmp/foo')
|
os.Open('/tmp/foo')
|
||||||
|
```
|
||||||
We would replace it with a call to `AppFs.Open('/tmp/foo')`.
|
We would replace it with a call to `AppFs.Open('/tmp/foo')`.
|
||||||
|
|
||||||
`AppFs` being the variable we defined above.
|
`AppFs` being the variable we defined above.
|
||||||
|
@ -87,37 +87,37 @@ We would replace it with a call to `AppFs.Open('/tmp/foo')`.
|
||||||
## List of all available functions
|
## List of all available functions
|
||||||
|
|
||||||
File System Methods Available:
|
File System Methods Available:
|
||||||
|
```go
|
||||||
Chmod(name string, mode os.FileMode) : error
|
Chmod(name string, mode os.FileMode) : error
|
||||||
Chtimes(name string, atime time.Time, mtime time.Time) : error
|
Chtimes(name string, atime time.Time, mtime time.Time) : error
|
||||||
Create(name string) : File, error
|
Create(name string) : File, error
|
||||||
Mkdir(name string, perm os.FileMode) : error
|
Mkdir(name string, perm os.FileMode) : error
|
||||||
MkdirAll(path string, perm os.FileMode) : error
|
MkdirAll(path string, perm os.FileMode) : error
|
||||||
Name() : string
|
Name() : string
|
||||||
Open(name string) : File, error
|
Open(name string) : File, error
|
||||||
OpenFile(name string, flag int, perm os.FileMode) : File, error
|
OpenFile(name string, flag int, perm os.FileMode) : File, error
|
||||||
Remove(name string) : error
|
Remove(name string) : error
|
||||||
RemoveAll(path string) : error
|
RemoveAll(path string) : error
|
||||||
Rename(oldname, newname string) : error
|
Rename(oldname, newname string) : error
|
||||||
Stat(name string) : os.FileInfo, error
|
Stat(name string) : os.FileInfo, error
|
||||||
|
```
|
||||||
File Interfaces and Methods Available:
|
File Interfaces and Methods Available:
|
||||||
|
```go
|
||||||
|
io.Closer
|
||||||
|
io.Reader
|
||||||
|
io.ReaderAt
|
||||||
|
io.Seeker
|
||||||
|
io.Writer
|
||||||
|
io.WriterAt
|
||||||
|
|
||||||
io.Closer
|
Name() : string
|
||||||
io.Reader
|
Readdir(count int) : []os.FileInfo, error
|
||||||
io.ReaderAt
|
Readdirnames(n int) : []string, error
|
||||||
io.Seeker
|
Stat() : os.FileInfo, error
|
||||||
io.Writer
|
Sync() : error
|
||||||
io.WriterAt
|
Truncate(size int64) : error
|
||||||
|
WriteString(s string) : ret int, err error
|
||||||
Name() : string
|
```
|
||||||
Readdir(count int) : []os.FileInfo, error
|
|
||||||
Readdirnames(n int) : []string, error
|
|
||||||
Stat() : os.FileInfo, error
|
|
||||||
Sync() : error
|
|
||||||
Truncate(size int64) : error
|
|
||||||
WriteString(s string) : ret int, err error
|
|
||||||
|
|
||||||
In some applications it may make sense to define a new package that
|
In some applications it may make sense to define a new package that
|
||||||
simply exports the file system variable for easy access from anywhere.
|
simply exports the file system variable for easy access from anywhere.
|
||||||
|
|
||||||
|
@ -146,31 +146,31 @@ appropriate in my application code. This approach ensures that Tests are order
|
||||||
independent, with no test relying on the state left by an earlier test.
|
independent, with no test relying on the state left by an earlier test.
|
||||||
|
|
||||||
Then in my tests I would initialize a new MemMapFs for each test:
|
Then in my tests I would initialize a new MemMapFs for each test:
|
||||||
|
```go
|
||||||
|
func TestExist(t *testing.T) {
|
||||||
|
appFS = &afero.MemMapFs{}
|
||||||
|
// create test files and directories
|
||||||
|
appFS.MkdirAll("src/a", 0755))
|
||||||
|
appFS.WriteFile("src/a/b", []byte("file b"), 0644)
|
||||||
|
appFS.WriteFile("src/c", []byte("file c"), 0644)
|
||||||
|
testExistence("src/c", true, t)
|
||||||
|
}
|
||||||
|
|
||||||
func TestExist(t *testing.T) {
|
func testExistence(name string, e bool, t *testing.T) {
|
||||||
appFS = &afero.MemMapFs{}
|
_, err := appFS.Stat(name)
|
||||||
// create test files and directories
|
if os.IsNotExist(err) {
|
||||||
appFS.MkdirAll("src/a", 0755))
|
if e {
|
||||||
appFS.WriteFile("src/a/b", []byte("file b"), 0644)
|
t.Errorf("file \"%s\" does not exist.\n", name)
|
||||||
appFS.WriteFile("src/c", []byte("file c"), 0644)
|
}
|
||||||
testExistence("src/c", true, t)
|
} else if err != nil {
|
||||||
}
|
panic(err)
|
||||||
|
} else {
|
||||||
func testExistence(name string, e bool, t *testing.T) {
|
if !e {
|
||||||
_, err := appFS.Stat(name)
|
t.Errorf("file \"%s\" exists.\n", name)
|
||||||
if os.IsNotExist(err) {
|
}
|
||||||
if e {
|
}
|
||||||
t.Errorf("file \"%s\" does not exist.\n", name)
|
}
|
||||||
}
|
```
|
||||||
} else if err != nil {
|
|
||||||
panic(err)
|
|
||||||
} else {
|
|
||||||
if !e {
|
|
||||||
t.Errorf("file \"%s\" exists.\n", name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
## Using Afero with Http
|
## Using Afero with Http
|
||||||
|
|
||||||
|
@ -182,11 +182,11 @@ returns an http.File type.
|
||||||
|
|
||||||
Afero provides an httpFs file system which satisfies this requirement.
|
Afero provides an httpFs file system which satisfies this requirement.
|
||||||
Any Afero FileSystem can be used as an httpFs.
|
Any Afero FileSystem can be used as an httpFs.
|
||||||
|
```go
|
||||||
httpFs := &afero.HttpFs{SourceFs: <ExistingFS>}
|
httpFs := &afero.HttpFs{SourceFs: <ExistingFS>}
|
||||||
fileserver := http.FileServer(httpFs.Dir(<PATH>)))
|
fileserver := http.FileServer(httpFs.Dir(<PATH>)))
|
||||||
http.Handle("/", fileserver)
|
http.Handle("/", fileserver)
|
||||||
|
```
|
||||||
# Available Backends
|
# Available Backends
|
||||||
|
|
||||||
## OsFs
|
## OsFs
|
||||||
|
|
Loading…
Reference in New Issue