github.com/bingoohuang/pkger@v0.0.0-20210127185155-a71b9df4c4c7/pkging/wrap.go (about) 1 package pkging 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 8 "github.com/bingoohuang/pkger/here" 9 ) 10 11 func Wrap(parent, with Pkger) Pkger { 12 return withPkger{ 13 base: with, 14 parent: parent, 15 } 16 } 17 18 type withPkger struct { 19 base Pkger 20 parent Pkger 21 } 22 23 func (w withPkger) String() string { 24 if w.parent == nil { 25 return fmt.Sprintf("%T", w.base) 26 } 27 return fmt.Sprintf("%T > %T", w.base, w.parent) 28 } 29 30 func (w withPkger) Parse(p string) (here.Path, error) { 31 pt, err := w.base.Parse(p) 32 if err != nil { 33 if w.parent != nil { 34 return w.parent.Parse(p) 35 } 36 return pt, err 37 } 38 return pt, nil 39 } 40 41 func (w withPkger) Current() (here.Info, error) { 42 pt, err := w.base.Current() 43 if err != nil { 44 if w.parent != nil { 45 return w.parent.Current() 46 } 47 return pt, err 48 } 49 return pt, nil 50 } 51 52 func (w withPkger) Info(p string) (here.Info, error) { 53 pt, err := w.base.Info(p) 54 if err != nil { 55 if w.parent != nil { 56 return w.parent.Info(p) 57 } 58 return pt, err 59 } 60 return pt, nil 61 } 62 63 // 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. 64 func (w withPkger) Create(p string) (File, error) { 65 pt, err := w.base.Create(p) 66 if err != nil { 67 if w.parent != nil { 68 return w.parent.Create(p) 69 } 70 return pt, err 71 } 72 return pt, nil 73 } 74 75 // 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. 76 func (w withPkger) MkdirAll(p string, perm os.FileMode) error { 77 err := w.base.MkdirAll(p, perm) 78 if err != nil { 79 return err 80 } 81 if w.parent != nil { 82 return w.parent.MkdirAll(p, perm) 83 } 84 return nil 85 } 86 87 // 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. 88 func (w withPkger) Open(p string) (File, error) { 89 pt, err := w.base.Open(p) 90 if err != nil { 91 if w.parent != nil { 92 return w.parent.Open(p) 93 } 94 return pt, err 95 } 96 return pt, nil 97 } 98 99 // Stat returns a FileInfo describing the named file. 100 func (w withPkger) Stat(p string) (os.FileInfo, error) { 101 pt, err := w.base.Stat(p) 102 if err != nil { 103 if w.parent != nil { 104 return w.parent.Stat(p) 105 } 106 return pt, err 107 } 108 return pt, nil 109 } 110 111 // 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. 112 func (w withPkger) Walk(p string, wf filepath.WalkFunc) error { 113 err := w.base.Walk(p, wf) 114 if err != nil { 115 return err 116 } 117 if w.parent != nil { 118 return w.parent.Walk(p, wf) 119 } 120 return nil 121 } 122 123 // Remove removes the named file or (empty) directory. 124 func (w withPkger) Remove(p string) error { 125 err := w.base.Remove(p) 126 if err != nil { 127 return err 128 } 129 if w.parent != nil { 130 return w.parent.Remove(p) 131 } 132 return nil 133 } 134 135 // 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). 136 func (w withPkger) RemoveAll(p string) error { 137 err := w.base.RemoveAll(p) 138 if err != nil { 139 return err 140 } 141 if w.parent != nil { 142 return w.parent.RemoveAll(p) 143 } 144 return nil 145 }