pkger/pkger.go

96 lines
3.0 KiB
Go
Raw Permalink Normal View History

2019-07-31 00:21:26 +03:00
package pkger
2019-08-02 07:22:17 +03:00
import (
"net/http"
2019-09-21 19:51:29 +03:00
"os"
"path/filepath"
2019-09-22 23:47:40 +03:00
"sync"
2019-08-09 22:43:58 +03:00
2019-09-21 19:51:29 +03:00
"github.com/markbates/pkger/here"
2019-09-20 18:16:57 +03:00
"github.com/markbates/pkger/pkging"
"github.com/markbates/pkger/pkging/stdos"
2019-08-02 07:22:17 +03:00
)
2019-09-22 23:47:40 +03:00
var current pkging.Pkger
var gil = &sync.RWMutex{}
var disk = func() pkging.Pkger {
2019-11-20 16:50:48 +03:00
her, _ := here.Current()
n, _ := stdos.New(her)
2019-09-22 23:47:40 +03:00
return n
2019-09-20 18:16:57 +03:00
}()
2019-09-22 23:47:40 +03:00
func impl() pkging.Pkger {
gil.RLock()
defer gil.RUnlock()
if current == nil {
return disk
}
return current
}
2019-10-25 16:30:31 +03:00
type Dir string
func (d Dir) Open(name string) (http.File, error) {
2019-11-01 00:20:15 +03:00
f, err := impl().Open(string(d))
2019-10-25 16:30:31 +03:00
if err != nil {
return nil, err
}
return f.Open(name)
}
2019-10-16 18:07:57 +03:00
// Parse the string in here.Path format.
2019-10-09 20:21:54 +03:00
func Parse(p string) (here.Path, error) {
2019-09-22 23:47:40 +03:00
return impl().Parse(p)
2019-09-21 19:51:29 +03:00
}
2019-10-16 18:07:57 +03:00
// Current returns the here.Info representing the current Pkger implementation.
2019-09-21 19:51:29 +03:00
func Current() (here.Info, error) {
2019-09-22 23:47:40 +03:00
return impl().Current()
2019-09-21 19:51:29 +03:00
}
2019-10-16 18:07:57 +03:00
// Info returns the here.Info of the here.Path
2019-09-21 19:51:29 +03:00
func Info(p string) (here.Info, error) {
2019-09-22 23:47:40 +03:00
return impl().Info(p)
2019-09-21 19:51:29 +03:00
}
// 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.
func Create(p string) (pkging.File, error) {
2019-09-22 23:47:40 +03:00
return impl().Create(p)
2019-09-21 19:51:29 +03:00
}
// 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.
func MkdirAll(p string, perm os.FileMode) error {
2019-09-22 23:47:40 +03:00
return impl().MkdirAll(p, perm)
2019-09-21 19:51:29 +03:00
}
// 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.
func Open(p string) (pkging.File, error) {
2019-09-22 23:47:40 +03:00
return impl().Open(p)
2019-09-21 19:51:29 +03:00
}
// Stat returns a FileInfo describing the named file.
func Stat(name string) (os.FileInfo, error) {
2019-09-22 23:47:40 +03:00
return impl().Stat(name)
2019-09-21 19:51:29 +03:00
}
// 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.
func Walk(p string, wf filepath.WalkFunc) error {
2019-09-22 23:47:40 +03:00
return impl().Walk(p, wf)
2019-09-21 19:51:29 +03:00
}
// Remove removes the named file or (empty) directory.
func Remove(name string) error {
2019-09-22 23:47:40 +03:00
return impl().Remove(name)
2019-09-21 19:51:29 +03:00
}
// 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).
func RemoveAll(name string) error {
2019-09-22 23:47:40 +03:00
return impl().RemoveAll(name)
2019-09-21 19:51:29 +03:00
}
// Include is a no-op that directs the pkger tool to include the desired file or folder.
2019-11-17 21:10:11 +03:00
func Include(name string) string {
return name
}