Skip to content

Commit

Permalink
Merge pull request #13 from jarxorg/some-codes-moves-to-fs2
Browse files Browse the repository at this point in the history
Some code moves to fs2 package
  • Loading branch information
mojatter authored Nov 18, 2021
2 parents ada8eab + 63f053f commit 0534c40
Show file tree
Hide file tree
Showing 16 changed files with 8 additions and 2,982 deletions.
78 changes: 8 additions & 70 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,9 @@
[![Report Card](https://goreportcard.com/badge/github.com/jarxorg/io2)](https://goreportcard.com/report/github.com/jarxorg/io2)
[![Coverage Status](https://coveralls.io/repos/github/jarxorg/io2/badge.svg?branch=main)](https://coveralls.io/github/jarxorg/io2?branch=main)

Go "io" and "io/fs" package utilities.
Go "io" package utilities.

## Writable io/fs.FS implementations

- [osfs](https://github.com/jarxorg/io2/tree/main/osfs)
- [memfs](https://github.com/jarxorg/io2/tree/main/memfs)
- [s3fs](https://github.com/jarxorg/s3fs)

```go
package main

import (
"fmt"
"io/fs"
"log"

"github.com/jarxorg/io2"
"github.com/jarxorg/io2/memfs"
"github.com/jarxorg/io2/osfs"
)

func main() {
osFsys := osfs.DirFS(".")
memFsys := memfs.New()

err := io2.CopyFS(memFsys, osFsys, "osfs/testdata")
if err != nil {
log.Fatal(err)
}

names, err := fs.Glob(memFsys, "osfs/testdata/dir0/*.txt")
if err != nil {
log.Fatal(err)
}

fmt.Printf("%v\n", names)

// Output: [osfs/testdata/dir0/file01.txt osfs/testdata/dir0/file02.txt]
}
```
NOTE: some codes moves to [fs2](https://github.com/jarxorg/fs2).

## Delegator

Expand Down Expand Up @@ -81,6 +44,12 @@ func main() {
### No-op Closer using Delegator

```go
// NopReadCloser returns a ReadCloser with a no-op Close method wrapping the provided interface.
// This function like io.NopCloser(io.Reader).
func NopReadCloser(r io.Reader) io.ReadCloser {
return DelegateReader(r)
}

// NopReadWriteCloser returns a ReadWriteCloser with a no-op Close method wrapping the provided interface.
func NopReadWriteCloser(rw io.ReadWriter) io.ReadWriteCloser {
return DelegateReadWriter(rw)
Expand All @@ -97,37 +66,6 @@ func NopWriteCloser(w io.Writer) io.WriteCloser {
}
```

## FSDelegator and FileDelegator

FSDelegator implements FS, ReadDirFS, ReadFileFS, StatFS, SubFS of [io/fs](https://github.com/golang/go/tree/master/src/io/fs) package.
FSDelegator can override the FS functions that is useful for unit tests.

```go
package main

import (
"errors"
"fmt"
"io/fs"
"os"

"github.com/jarxorg/io2"
)

func main() {
fsys := io2.DelegateFS(os.DirFS("."))
fsys.ReadDirFunc = func(name string) ([]fs.DirEntry, error) {
return nil, errors.New("custom")
}

var err error
_, err = fs.ReadDir(fsys, ".")
fmt.Printf("Error: %v\n", err)

// Output: Error: custom
}
```

## WriteSeekBuffer

WriteSeekBuffer implements io.Writer, io.Seeker and io.Closer.
Expand Down
61 changes: 0 additions & 61 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,11 @@ import (
"errors"
"fmt"
"io"
"io/fs"
"io/ioutil"
"log"
"os"

"github.com/jarxorg/io2"
"github.com/jarxorg/io2/osfs"
)

func ExampleWriteFile() {
tmpDir, err := ioutil.TempDir("", "example")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(tmpDir)

name := "example.txt"
content := []byte(`Hello`)

fsys := osfs.DirFS(tmpDir)
_, err = io2.WriteFile(fsys, name, content, fs.ModePerm)
if err != nil {
log.Fatal(err)
}

wrote, err := ioutil.ReadFile(tmpDir + "/" + name)
if err != nil {
log.Fatal(err)
}

fmt.Printf("%s\n", string(wrote))

// Output: Hello
}

func ExampleDelegateReader() {
org := bytes.NewReader([]byte(`original`))

Expand All @@ -55,37 +25,6 @@ func ExampleDelegateReader() {
// Output: Error: custom
}

func ExampleDelegateFS() {
fsys := io2.DelegateFS(os.DirFS("."))
fsys.ReadDirFunc = func(name string) ([]fs.DirEntry, error) {
return nil, errors.New("custom")
}

var err error
_, err = fs.ReadDir(fsys, ".")
fmt.Printf("Error: %v\n", err)

// Output: Error: custom
}

func ExampleDelegateFile() {
fsys := io2.DelegateFS(os.DirFS("."))
fsys.OpenFunc = func(name string) (fs.File, error) {
return &io2.FileDelegator{
StatFunc: func() (fs.FileInfo, error) {
return nil, errors.New("custom")
},
}, nil
}

file, _ := fsys.Open("anyfile")
var err error
_, err = file.Stat()
fmt.Printf("Error: %v\n", err)

// Output: Error: custom
}

func ExampleNewWriteSeekerBuffer() {
o := io2.NewWriteSeekBuffer(0)
o.Write([]byte(`Hello!`))
Expand Down
99 changes: 0 additions & 99 deletions fs.go

This file was deleted.

Loading

0 comments on commit 0534c40

Please sign in to comment.