You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
2.1 KiB

  1. # go-billy [![GoDoc](https://godoc.org/gopkg.in/src-d/go-billy.v4?status.svg)](https://godoc.org/gopkg.in/src-d/go-billy.v4) [![Build Status](https://travis-ci.org/src-d/go-billy.svg)](https://travis-ci.org/src-d/go-billy) [![Build status](https://ci.appveyor.com/api/projects/status/vx2qn6vlakbi724t?svg=true)](https://ci.appveyor.com/project/mcuadros/go-billy) [![codecov](https://codecov.io/gh/src-d/go-billy/branch/master/graph/badge.svg)](https://codecov.io/gh/src-d/go-billy)
  2. The missing interface filesystem abstraction for Go.
  3. Billy implements an interface based on the `os` standard library, allowing to develop applications without dependency on the underlying storage. Makes it virtually free to implement mocks and testing over filesystem operations.
  4. Billy was born as part of [src-d/go-git](https://github.com/src-d/go-git) project.
  5. ## Installation
  6. ```go
  7. go get -u gopkg.in/src-d/go-billy.v4/...
  8. ```
  9. ## Usage
  10. Billy exposes filesystems using the
  11. [`Filesystem` interface](https://godoc.org/github.com/src-d/go-billy#Filesystem).
  12. Each filesystem implementation gives you a `New` method, whose arguments depend on
  13. the implementation itself, that returns a new `Filesystem`.
  14. The following example caches in memory all readable files in a directory from any
  15. billy's filesystem implementation.
  16. ```go
  17. func LoadToMemory(origin billy.Filesystem, path string) (*memory.Memory, error) {
  18. memory := memory.New()
  19. files, err := origin.ReadDir("/")
  20. if err != nil {
  21. return nil, err
  22. }
  23. for _, file := range files {
  24. if file.IsDir() {
  25. continue
  26. }
  27. src, err := origin.Open(file.Name())
  28. if err != nil {
  29. return nil, err
  30. }
  31. dst, err := memory.Create(file.Name())
  32. if err != nil {
  33. return nil, err
  34. }
  35. if _, err = io.Copy(dst, src); err != nil {
  36. return nil, err
  37. }
  38. if err := dst.Close(); err != nil {
  39. return nil, err
  40. }
  41. if err := src.Close(); err != nil {
  42. return nil, err
  43. }
  44. }
  45. return memory, nil
  46. }
  47. ```
  48. ## Why billy?
  49. The library billy deals with storage systems and Billy is the name of a well-known, IKEA
  50. bookcase. That's it.
  51. ## License
  52. Apache License Version 2.0, see [LICENSE](LICENSE)