pkger/pkger.go

86 lines
2.7 KiB
Go
Raw Normal View History

2019-07-31 00:21:26 +03:00
package pkger
2019-08-02 07:22:17 +03:00
import (
2019-09-22 00:22:59 +03:00
"fmt"
2019-08-02 07:22:17 +03:00
"log"
2019-09-21 19:51:29 +03:00
"os"
"path/filepath"
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-21 19:51:29 +03:00
var disk = func() pkging.Pkger {
2019-09-20 18:16:57 +03:00
n, err := stdos.New()
2019-08-02 07:22:17 +03:00
if err != nil {
2019-09-21 19:51:29 +03:00
log.Println(err)
2019-09-20 18:16:57 +03:00
}
return n
}()
2019-09-22 00:22:59 +03:00
func impl() pkging.Pkger {
2019-09-21 19:51:29 +03:00
gil.RLock()
defer gil.RUnlock()
if current == nil {
2019-09-22 00:22:59 +03:00
return disk
2019-09-21 19:51:29 +03:00
}
2019-09-22 00:22:59 +03:00
fmt.Printf("!> using %s\n", current)
return current
}
func Parse(p string) (pkging.Path, error) {
return impl().Parse(p)
2019-09-21 19:51:29 +03:00
}
func Abs(p string) (string, error) {
2019-09-22 00:22:59 +03:00
return impl().Abs(p)
2019-09-21 19:51:29 +03:00
}
func AbsPath(p pkging.Path) (string, error) {
2019-09-22 00:22:59 +03:00
return impl().AbsPath(p)
2019-09-21 19:51:29 +03:00
}
func Current() (here.Info, error) {
2019-09-22 00:22:59 +03:00
return impl().Current()
2019-09-21 19:51:29 +03:00
}
func Info(p string) (here.Info, error) {
2019-09-22 00:22:59 +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 00:22:59 +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 00:22:59 +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 00:22:59 +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 00:22:59 +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 00:22:59 +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 00:22:59 +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 00:22:59 +03:00
return impl().RemoveAll(name)
2019-09-21 19:51:29 +03:00
}