github.com/bingoohuang/pkger@v0.0.0-20210127185155-a71b9df4c4c7/pkger.go (about)

     1  package pkger
     2  
     3  import (
     4  	"io/ioutil"
     5  	"net/http"
     6  	"os"
     7  	"path/filepath"
     8  	"sync"
     9  
    10  	"github.com/bingoohuang/pkger/here"
    11  	"github.com/bingoohuang/pkger/pkging"
    12  	"github.com/bingoohuang/pkger/pkging/stdos"
    13  )
    14  
    15  var current pkging.Pkger
    16  var gil = &sync.RWMutex{}
    17  
    18  var disk = func() pkging.Pkger {
    19  	her, _ := here.Current()
    20  	n, _ := stdos.New(her)
    21  	return n
    22  }()
    23  
    24  func impl() pkging.Pkger {
    25  	gil.RLock()
    26  	defer gil.RUnlock()
    27  	if current == nil {
    28  		return disk
    29  	}
    30  	return current
    31  }
    32  
    33  type Dir string
    34  
    35  func (d Dir) Open(name string) (http.File, error) {
    36  	f, err := impl().Open(string(d))
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  	return f.Open(name)
    41  }
    42  
    43  // Parse the string in here.Path format.
    44  func Parse(p string) (here.Path, error) {
    45  	return impl().Parse(p)
    46  }
    47  
    48  // Current returns the here.Info representing the current Pkger implementation.
    49  func Current() (here.Info, error) {
    50  	return impl().Current()
    51  }
    52  
    53  // Info returns the here.Info of the here.Path
    54  func Info(p string) (here.Info, error) {
    55  	return impl().Info(p)
    56  }
    57  
    58  // Create creates the named file with mode 0666 (before umask) - It's actually 0644, truncating it if it already exists. If successful, methods on the returned File can be used for I/O; the associated file descriptor has mode O_RDWR.
    59  func Create(p string) (pkging.File, error) {
    60  	return impl().Create(p)
    61  }
    62  
    63  // MkdirAll creates a directory named path, along with any necessary parents, and returns nil, or else returns an error. The permission bits perm (before umask) are used for all directories that MkdirAll creates. If path is already a directory, MkdirAll does nothing and returns nil.
    64  func MkdirAll(p string, perm os.FileMode) error {
    65  	return impl().MkdirAll(p, perm)
    66  }
    67  
    68  // Open opens the named file for reading. If successful, methods on the returned file can be used for reading; the associated file descriptor has mode O_RDONLY.
    69  func Open(p string) (pkging.File, error) {
    70  	return impl().Open(p)
    71  }
    72  
    73  // ReadStr read string from named file.
    74  func ReadStr(p string) (string, error) {
    75  	r, err := Read(p)
    76  	return string(r), err
    77  }
    78  
    79  // MustReadStr read string from named file.
    80  func MustReadStr(p string) string {
    81  	return string(MustRead(p))
    82  }
    83  
    84  // MustRead read bytes slice from named file.
    85  func MustRead(p string) []byte {
    86  	if d, err := Read(p); err != nil {
    87  		panic(err)
    88  	} else {
    89  		return d
    90  	}
    91  }
    92  
    93  // Read read bytes slice from named file.
    94  func Read(p string) ([]byte, error) {
    95  	f, err := Open(p)
    96  	if err != nil {
    97  		return nil, err
    98  	}
    99  
   100  	defer f.Close()
   101  
   102  	return ioutil.ReadAll(f)
   103  }
   104  
   105  // Stat returns a FileInfo describing the named file.
   106  func Stat(name string) (os.FileInfo, error) {
   107  	return impl().Stat(name)
   108  }
   109  
   110  // Walk walks the file tree rooted at root, calling walkFn for each file or directory in the tree, including root. All errors that arise visiting files and directories are filtered by walkFn. The files are walked in lexical order, which makes the output deterministic but means that for very large directories Walk can be inefficient. Walk does not follow symbolic links. - That is from the standard library. I know. Their grammar teachers can not be happy with them right now.
   111  func Walk(p string, wf filepath.WalkFunc) error {
   112  	return impl().Walk(p, wf)
   113  }
   114  
   115  // Remove removes the named file or (empty) directory.
   116  func Remove(name string) error {
   117  	return impl().Remove(name)
   118  }
   119  
   120  // RemoveAll removes path and any children it contains. It removes everything it can but returns the first error it encounters. If the path does not exist, RemoveAll returns nil (no error).
   121  func RemoveAll(name string) error {
   122  	return impl().RemoveAll(name)
   123  }
   124  
   125  // Include is a no-op that directs the pkger tool to include the desired file or folder.
   126  func Include(name string) string {
   127  	return name
   128  }