mirror of https://github.com/markbates/pkger.git
party
This commit is contained in:
parent
cad42281de
commit
b28e301e65
89
pkger.go
89
pkger.go
|
@ -1,6 +1,7 @@
|
||||||
package pkger
|
package pkger
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
@ -18,117 +19,67 @@ var disk = func() pkging.Pkger {
|
||||||
return n
|
return n
|
||||||
}()
|
}()
|
||||||
|
|
||||||
func Parse(p string) (pkging.Path, error) {
|
func impl() pkging.Pkger {
|
||||||
gil.RLock()
|
gil.RLock()
|
||||||
defer gil.RUnlock()
|
defer gil.RUnlock()
|
||||||
if current == nil {
|
if current == nil {
|
||||||
return disk.Parse(p)
|
return disk
|
||||||
}
|
}
|
||||||
return current.Parse(p)
|
fmt.Printf("!> using %s\n", current)
|
||||||
|
return current
|
||||||
|
}
|
||||||
|
|
||||||
|
func Parse(p string) (pkging.Path, error) {
|
||||||
|
return impl().Parse(p)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Abs(p string) (string, error) {
|
func Abs(p string) (string, error) {
|
||||||
gil.RLock()
|
return impl().Abs(p)
|
||||||
defer gil.RUnlock()
|
|
||||||
if current == nil {
|
|
||||||
return disk.Abs(p)
|
|
||||||
}
|
|
||||||
return current.Abs(p)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func AbsPath(p pkging.Path) (string, error) {
|
func AbsPath(p pkging.Path) (string, error) {
|
||||||
gil.RLock()
|
return impl().AbsPath(p)
|
||||||
defer gil.RUnlock()
|
|
||||||
if current == nil {
|
|
||||||
return disk.AbsPath(p)
|
|
||||||
}
|
|
||||||
return current.AbsPath(p)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Current() (here.Info, error) {
|
func Current() (here.Info, error) {
|
||||||
gil.RLock()
|
return impl().Current()
|
||||||
defer gil.RUnlock()
|
|
||||||
if current == nil {
|
|
||||||
return disk.Current()
|
|
||||||
}
|
|
||||||
return current.Current()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Info(p string) (here.Info, error) {
|
func Info(p string) (here.Info, error) {
|
||||||
gil.RLock()
|
return impl().Info(p)
|
||||||
defer gil.RUnlock()
|
|
||||||
if current == nil {
|
|
||||||
return disk.Info(p)
|
|
||||||
}
|
|
||||||
return current.Info(p)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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.
|
// 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) {
|
func Create(p string) (pkging.File, error) {
|
||||||
gil.RLock()
|
return impl().Create(p)
|
||||||
defer gil.RUnlock()
|
|
||||||
if current == nil {
|
|
||||||
return disk.Create(p)
|
|
||||||
}
|
|
||||||
return current.Create(p)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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.
|
// 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 {
|
func MkdirAll(p string, perm os.FileMode) error {
|
||||||
gil.RLock()
|
return impl().MkdirAll(p, perm)
|
||||||
defer gil.RUnlock()
|
|
||||||
if current == nil {
|
|
||||||
return disk.MkdirAll(p, perm)
|
|
||||||
}
|
|
||||||
return current.MkdirAll(p, perm)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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.
|
// 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) {
|
func Open(p string) (pkging.File, error) {
|
||||||
gil.RLock()
|
return impl().Open(p)
|
||||||
defer gil.RUnlock()
|
|
||||||
if current == nil {
|
|
||||||
return disk.Open(p)
|
|
||||||
}
|
|
||||||
return current.Open(p)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stat returns a FileInfo describing the named file.
|
// Stat returns a FileInfo describing the named file.
|
||||||
func Stat(name string) (os.FileInfo, error) {
|
func Stat(name string) (os.FileInfo, error) {
|
||||||
gil.RLock()
|
return impl().Stat(name)
|
||||||
defer gil.RUnlock()
|
|
||||||
if current == nil {
|
|
||||||
return disk.Stat(name)
|
|
||||||
}
|
|
||||||
return current.Stat(name)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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.
|
// 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 {
|
func Walk(p string, wf filepath.WalkFunc) error {
|
||||||
gil.RLock()
|
return impl().Walk(p, wf)
|
||||||
defer gil.RUnlock()
|
|
||||||
if current == nil {
|
|
||||||
return disk.Walk(p, wf)
|
|
||||||
}
|
|
||||||
return current.Walk(p, wf)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove removes the named file or (empty) directory.
|
// Remove removes the named file or (empty) directory.
|
||||||
func Remove(name string) error {
|
func Remove(name string) error {
|
||||||
gil.RLock()
|
return impl().Remove(name)
|
||||||
defer gil.RUnlock()
|
|
||||||
if current == nil {
|
|
||||||
return disk.Remove(name)
|
|
||||||
}
|
|
||||||
return current.Remove(name)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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).
|
// 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 {
|
func RemoveAll(name string) error {
|
||||||
gil.RLock()
|
return impl().RemoveAll(name)
|
||||||
defer gil.RUnlock()
|
|
||||||
if current == nil {
|
|
||||||
return disk.RemoveAll(name)
|
|
||||||
}
|
|
||||||
return current.RemoveAll(name)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package pkging
|
package pkging
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
|
@ -19,6 +20,13 @@ type withPkger struct {
|
||||||
parent Pkger
|
parent Pkger
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (w withPkger) String() string {
|
||||||
|
if w.parent == nil {
|
||||||
|
return fmt.Sprintf("%T", w.base)
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%T > %T", w.base, w.parent)
|
||||||
|
}
|
||||||
|
|
||||||
func (w withPkger) Parse(p string) (Path, error) {
|
func (w withPkger) Parse(p string) (Path, error) {
|
||||||
pt, err := w.base.Parse(p)
|
pt, err := w.base.Parse(p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue