Skip to content

Commit b4df671

Browse files
committed
doc: move examples to godoc
1 parent 5ab2ef2 commit b4df671

11 files changed

+170
-94
lines changed

README.md

+30-17
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,40 @@ import github.com/blang/vfs
1616

1717
```go
1818
// Create a vfs accessing the filesystem of the underlying OS
19-
fs := vfs.OS()
20-
fs.Mkdir("/tmp", 0777)
19+
var osfs vfs.Filesystem = vfs.OS()
20+
osfs.Mkdir("/tmp", 0777)
2121

2222
// Make the filesystem read-only:
23-
fs = vfs.ReadOnly(fs) // Simply wrap filesystems to change its behaviour
23+
osfs = vfs.ReadOnly(osfs) // Simply wrap filesystems to change its behaviour
2424

2525
// os.O_CREATE will fail and return vfs.ErrReadOnly
2626
// os.O_RDWR is supported but Write(..) on the file is disabled
27-
f, _ := fs.OpenFile("/tmp/example.txt", os.O_RDWR, 0)
27+
f, _ := osfs.OpenFile("/tmp/example.txt", os.O_RDWR, 0)
2828

2929
// Return vfs.ErrReadOnly
3030
_, err := f.Write([]byte("Write on readonly fs?"))
31+
if err != nil {
32+
fmt.Errorf("Filesystem is read only!\n")
33+
}
3134

3235
// Create a fully writable filesystem in memory
33-
fs := memfs.Create()
34-
fs.Mkdir("/root")
36+
mfs := memfs.Create()
37+
mfs.Mkdir("/root", 0777)
38+
39+
// Create a vfs supporting mounts
40+
// The root fs is accessing the filesystem of the underlying OS
41+
fs := mountfs.Create(osfs)
42+
43+
// Mount a memfs inside /memfs
44+
// /memfs may not exist
45+
fs.Mount(mfs, "/memfs")
46+
47+
// This will create /testdir inside the memfs
48+
fs.Mkdir("/memfs/testdir", 0777)
49+
50+
// This would create /tmp/testdir inside your OS fs
51+
// But the rootfs `osfs` is read-only
52+
fs.Mkdir("/tmp/testdir", 0777)
3553
```
3654

3755
Check detailed examples below. Also check the [GoDocs](http://godoc.org/github.com/blang/vfs).
@@ -46,19 +64,14 @@ Why should I use this lib?
4664
- Compose/Wrap Filesystems `ReadOnly(OS())` and write simple Wrappers
4765
- Many features, see [GoDocs](http://godoc.org/github.com/blang/vfs) and examples below
4866

49-
Example
50-
-----
51-
52-
Have a look at full examples in [examples/](examples/)
53-
54-
Features
67+
Features and Examples
5568
-----
5669

57-
- OS Filesystem support
58-
- ReadOnly Wrapper
59-
- DummyFS for quick mocking
60-
- MemFS full in-memory filesystem
61-
- MountFS - support mounts across filesystems
70+
- [OS Filesystem support](http://godoc.org/github.com/blang/vfs#example-OsFS)
71+
- [ReadOnly Wrapper](http://godoc.org/github.com/blang/vfs#example-RoFS)
72+
- [DummyFS for quick mocking](http://godoc.org/github.com/blang/vfs#example-DummyFS)
73+
- [MemFS - full in-memory filesystem](http://godoc.org/github.com/blang/vfs/memfs#example-MemFS)
74+
- [MountFS - support mounts across filesystems](http://godoc.org/github.com/blang/vfs/mountfs#example-MountFS)
6275

6376
Current state: ALPHA
6477
-----

example_dummy_test.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package vfs_test
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"os"
7+
8+
"github.com/blang/vfs"
9+
)
10+
11+
type myFS struct {
12+
vfs.Filesystem // Embed the Filesystem interface and fill it with vfs.Dummy on creation
13+
}
14+
15+
func MyFS() *myFS {
16+
return &myFS{
17+
vfs.Dummy(errors.New("Not implemented yet!")),
18+
}
19+
}
20+
21+
func (fs myFS) Mkdir(name string, perm os.FileMode) error {
22+
// Create a directory
23+
// ...
24+
return nil
25+
}
26+
27+
func ExampleDummyFS() {
28+
// Simply bootstrap your filesystem
29+
var fs vfs.Filesystem = MyFS()
30+
31+
// Your mkdir implementation
32+
fs.Mkdir("/tmp", 0777)
33+
34+
// All necessary methods like OpenFile (therefor Create) are stubbed
35+
// and return the dummys error
36+
_, err := vfs.Create(fs, "/tmp/vfs/example.txt")
37+
if err != nil {
38+
fmt.Printf("Error will be: Not implemented yet!\n")
39+
}
40+
41+
}

example_os_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package vfs_test
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/blang/vfs"
7+
)
8+
9+
func ExampleOsFS() {
10+
11+
// Create a vfs accessing the filesystem of the underlying OS
12+
osFS := vfs.OS()
13+
err := osFS.Mkdir("/tmp/vfs_example", 0777)
14+
if err != nil {
15+
fmt.Printf("Error creating directory: %s\n", err)
16+
}
17+
18+
// Convenience method
19+
f, err := vfs.Create(osFS, "/tmp/vfs_example/example.txt")
20+
// f, err := osFS.OpenFile("/tmp/vfs/example.txt", os.O_CREATE|os.O_RDWR, 0666)
21+
if err != nil {
22+
fmt.Printf("Could not create file: %s\n", err)
23+
}
24+
defer f.Close()
25+
if _, err := f.Write([]byte("VFS working on your filesystem")); err != nil {
26+
fmt.Printf("Error writing to file: %s\n", err)
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,39 @@
1-
package examples
1+
package vfs_test
22

33
import (
4+
"fmt"
45
"os"
56

67
"github.com/blang/vfs"
78
)
89

910
// Every vfs.Filesystem could be easily wrapped
10-
func ExampleReadOnlyOS() {
11+
func ExampleRoFS() {
1112
// Create a readonly vfs accessing the filesystem of the underlying OS
1213
roFS := vfs.ReadOnly(vfs.OS())
1314

1415
// Mkdir is disabled on ReadOnly vfs, will return vfs.ErrReadOnly
1516
// See vfs.ReadOnly for all disabled operations
16-
err := roFS.Mkdir("/tmp/vfs", 0777)
17+
err := roFS.Mkdir("/tmp/vfs_example", 0777)
1718
if err != nil {
18-
fatal("Error creating directory: %s\n", err)
19+
fmt.Printf("Error creating directory: %s\n", err)
20+
return
1921
}
2022

2123
// OpenFile is controlled to support read-only functionality. os.O_CREATE or os.O_APPEND will fail.
2224
// Flags like os.O_RDWR are supported but the returned file is protected e.g. from Write(..).
23-
f, err := roFS.OpenFile("/tmp/vfs/example.txt", os.O_RDWR, 0)
25+
f, err := roFS.OpenFile("/tmp/vfs_example/example.txt", os.O_RDWR, 0)
2426
if err != nil {
25-
fatal("Could not create file: %s\n", err)
27+
fmt.Printf("Could not create file: %s\n", err)
28+
return
29+
2630
}
2731
defer f.Close()
2832

2933
// Will fail and return vfs.ErrReadOnly
3034
_, err = f.Write([]byte("VFS working on your filesystem"))
3135
if err != nil {
32-
fatal("Could not write file on read only filesystem: %s", err)
36+
fmt.Printf("Could not write file on read only filesystem: %s", err)
37+
return
3338
}
3439
}

example_test.go

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package vfs_test
2+
3+
import (
4+
"fmt"
5+
"github.com/blang/vfs"
6+
"github.com/blang/vfs/memfs"
7+
"github.com/blang/vfs/mountfs"
8+
"os"
9+
)
10+
11+
func Example() {
12+
// Create a vfs accessing the filesystem of the underlying OS
13+
var osfs vfs.Filesystem = vfs.OS()
14+
osfs.Mkdir("/tmp", 0777)
15+
16+
// Make the filesystem read-only:
17+
osfs = vfs.ReadOnly(osfs) // Simply wrap filesystems to change its behaviour
18+
19+
// os.O_CREATE will fail and return vfs.ErrReadOnly
20+
// os.O_RDWR is supported but Write(..) on the file is disabled
21+
f, _ := osfs.OpenFile("/tmp/example.txt", os.O_RDWR, 0)
22+
23+
// Return vfs.ErrReadOnly
24+
_, err := f.Write([]byte("Write on readonly fs?"))
25+
if err != nil {
26+
fmt.Errorf("Filesystem is read only!\n")
27+
}
28+
29+
// Create a fully writable filesystem in memory
30+
mfs := memfs.Create()
31+
mfs.Mkdir("/root", 0777)
32+
33+
// Create a vfs supporting mounts
34+
// The root fs is accessing the filesystem of the underlying OS
35+
fs := mountfs.Create(osfs)
36+
37+
// Mount a memfs inside /memfs
38+
// /memfs may not exist
39+
fs.Mount(mfs, "/memfs")
40+
41+
// This will create /testdir inside the memfs
42+
fs.Mkdir("/memfs/testdir", 0777)
43+
44+
// This would create /tmp/testdir inside your OS fs
45+
// But the rootfs `osfs` is read-only
46+
fs.Mkdir("/tmp/testdir", 0777)
47+
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
package examples
1+
package vfs_test
22

33
import (
44
"errors"
5+
"fmt"
56
"os"
67

78
"github.com/blang/vfs"
@@ -11,7 +12,7 @@ type noNewDirs struct {
1112
vfs.Filesystem
1213
}
1314

14-
func NoNewDirs(fs vfs.Filesystem) vfs.Filesystem {
15+
func NoNewDirs(fs vfs.Filesystem) *noNewDirs {
1516
return &noNewDirs{fs}
1617
}
1718

@@ -20,13 +21,13 @@ func (fs *noNewDirs) Mkdir(name string, perm os.FileMode) error {
2021
return errors.New("Mkdir disabled!")
2122
}
2223

23-
func ExampleMyWrapper() {
24+
func ExampleOsFS_myWrapper() {
2425

2526
// Disable Mkdirs on the OS Filesystem
2627
var fs vfs.Filesystem = NoNewDirs(vfs.OS())
2728

2829
err := fs.Mkdir("/tmp", 0777)
2930
if err != nil {
30-
fatal("Mkdir disabled!")
31+
fmt.Printf("Mkdir disabled!\n")
3132
}
3233
}

examples/example_myfs.go

-27
This file was deleted.

examples/example_os.go

-24
This file was deleted.

examples/util.go

-11
This file was deleted.

examples/example_memfs.go memfs/example_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
package examples
1+
package memfs_test
22

3-
import "github.com/blang/vfs/memfs"
3+
import (
4+
"github.com/blang/vfs/memfs"
5+
)
46

57
func ExampleMemFS() {
68
// Create a fully writable filesystem in memory

examples/example_mountfs.go mountfs/example_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package examples
1+
package mountfs_test
22

33
import (
44
"github.com/blang/vfs"
@@ -11,7 +11,8 @@ func ExampleMountFS() {
1111
// The root fs is accessing the filesystem of the underlying OS
1212
fs := mountfs.Create(vfs.OS())
1313

14-
// Mount a memfs inside
14+
// Mount a memfs inside /memfs
15+
// /memfs may not exist
1516
fs.Mount(memfs.Create(), "/memfs")
1617

1718
// This will create /testdir inside the memfs

0 commit comments

Comments
 (0)