Skip to content

Commit

Permalink
Introduce FileSystem.
Browse files Browse the repository at this point in the history
  • Loading branch information
adyatlov committed Jun 9, 2018
1 parent 62c4d71 commit d2e0804
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 14 deletions.
4 changes: 3 additions & 1 deletion bun/cmd/check.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"context"
"fmt"
"os"
"sort"
Expand Down Expand Up @@ -36,7 +37,8 @@ func preRun(cmd *cobra.Command, args []string) {
fmt.Printf("Error while detecting a working directory: %v\n", err.Error())
os.Exit(1)
}
b, err := bun.NewBundle(path)
ctx := context.WithValue(context.Background(), "fs", bun.OSFS{})
b, err := bun.NewBundle(ctx, path)
if err != nil {
fmt.Printf("Error while identifying basic bundle parameters: %v\n", err.Error())
os.Exit(1)
Expand Down
13 changes: 7 additions & 6 deletions bundle.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package bun

import (
"io"
"io/ioutil"
"context"
"log"
"path/filepath"
"regexp"
Expand All @@ -13,15 +12,16 @@ type Bundle struct {
Hosts map[string]Host // IP to Host map
}

func NewBundle(path string) (Bundle, error) {
func NewBundle(ctx context.Context, path string) (Bundle, error) {
b := Bundle{Hosts: make(map[string]Host)}
var err error
b.Path, err = filepath.Abs(path)
if err != nil {
log.Printf("Error occurred while detecting absolute path: %v", err)
return b, err
}
infos, err := ioutil.ReadDir(b.Path)
fs := ctx.Value("fs").(FileSystem)
infos, err := fs.ReadDir(b.Path)
if err != nil {
return b, err
}
Expand Down Expand Up @@ -53,7 +53,8 @@ func NewBundle(path string) (Bundle, error) {
return b, nil
}

// OpenFile opens bundle file. Caller is responsible for closing the file.
func (b Bundle) OpenFile(fileType string) (io.ReadCloser, error) {
// OpenFile opens a file in a root directory of the bundle. The caller is
// responsible for closing the file.
func (b Bundle) OpenFile(fileType string) (File, error) {
return OpenFile(b.Path, fileType)
}
15 changes: 8 additions & 7 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,26 @@ func GetFileType(typeName string) (FileType, error) {
}

// OpenFile opens bundle file. Caller is responsible for closing the file.
func OpenFile(basePath string, typeName string) (rc io.ReadCloser, err error) {
func OpenFile(basePath string, typeName string) (file File, err error) {
fileType, err := GetFileType(typeName)
if err != nil {
return
}
filePath := path.Join(basePath, fileType.Path)
rc, err = os.Open(filePath)
file, err = os.Open(filePath)
if os.IsNotExist(err) {
var gzrc io.ReadCloser
if gzrc, err = os.Open(filePath + ".gz"); err != nil {
var gzfile File
if gzfile, err = os.Open(filePath + ".gz"); err != nil {
return
}
if rc, err = gzip.NewReader(gzrc); err != nil {
if file, err = gzip.NewReader(gzfile); err != nil {
return
}
rc = struct {
file = struct {
io.Reader
io.Closer
}{io.Reader(rc), bulkCloser{rc, gzrc}}
}{io.Reader(file),
bulkCloser{file, gzfile}}
}
return
}
Expand Down
45 changes: 45 additions & 0 deletions file_system.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package bun

import (
"io"
"io/ioutil"
"os"
)

type FileSystem interface {
// ReadDir reads the directory named by dirname and returns
// a list of directory entries sorted by filename.
// It's mocking the io/ioutil.ReadDir.
ReadDir(string) ([]os.FileInfo, error)

// Open opens the named file for reading. If successful, methods on
// the returned file can be used for reading.
// It's partially mocking the os.Open function.
Open(string) (File, error)

// Getwd returns a rooted path name corresponding to the
// current directory. If the current directory can be
// reached via multiple paths (due to symbolic links),
// Getwd may return any one of them.
// It's mocking os.Getwd.
Getwd() (string, error)
}

type File interface {
io.ReadCloser
}

// osFS implements FileSystem
type OSFS struct {
}

func (osfs OSFS) ReadDir(dirname string) ([]os.FileInfo, error) {
return ioutil.ReadDir(dirname)
}
func (osfs OSFS) Open(name string) (File, error) {
file, err := os.Open(name)
return File(file), err
}
func (osfs OSFS) Getwd() (string, error) {
return os.Getwd()
}
18 changes: 18 additions & 0 deletions test/file_system.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package test

import (
"os"

"github.com/adyatlov/bun/fs"
)

type FileSystem struct {
}

func (fs *FileSystem) ReadDir(name string) ([]os.FileInfo, error) {
return nil, nil
}

func (fs *FileSystem) Open(name string) (fs.File, error) {
return nil, nil
}

0 comments on commit d2e0804

Please sign in to comment.