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
28
README.md
28
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,7 +87,7 @@ 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
|
||||||
|
@ -100,9 +100,9 @@ File System Methods Available:
|
||||||
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.Closer
|
||||||
io.Reader
|
io.Reader
|
||||||
io.ReaderAt
|
io.ReaderAt
|
||||||
|
@ -117,7 +117,7 @@ File Interfaces and Methods Available:
|
||||||
Sync() : error
|
Sync() : error
|
||||||
Truncate(size int64) : error
|
Truncate(size int64) : error
|
||||||
WriteString(s string) : ret int, err 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,7 +146,7 @@ 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) {
|
func TestExist(t *testing.T) {
|
||||||
appFS = &afero.MemMapFs{}
|
appFS = &afero.MemMapFs{}
|
||||||
// create test files and directories
|
// create test files and directories
|
||||||
|
@ -170,7 +170,7 @@ Then in my tests I would initialize a new MemMapFs for each test:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
## 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