Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some code moves to fs2 package #13

Merged
merged 1 commit into from
Nov 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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