diff --git a/bun/cmd/check.go b/bun/cmd/check.go index 4e48959..bb0c072 100644 --- a/bun/cmd/check.go +++ b/bun/cmd/check.go @@ -1,6 +1,7 @@ package cmd import ( + "context" "fmt" "os" "sort" @@ -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) diff --git a/bundle.go b/bundle.go index f89717f..0543a09 100644 --- a/bundle.go +++ b/bundle.go @@ -1,8 +1,7 @@ package bun import ( - "io" - "io/ioutil" + "context" "log" "path/filepath" "regexp" @@ -13,7 +12,7 @@ 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) @@ -21,7 +20,8 @@ func NewBundle(path string) (Bundle, error) { 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 } @@ -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) } diff --git a/file.go b/file.go index 1b99453..de2c0ec 100644 --- a/file.go +++ b/file.go @@ -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 } diff --git a/file_system.go b/file_system.go new file mode 100644 index 0000000..415fb12 --- /dev/null +++ b/file_system.go @@ -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() +} diff --git a/test/file_system.go b/test/file_system.go new file mode 100644 index 0000000..15940cd --- /dev/null +++ b/test/file_system.go @@ -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 +}