Skip to content
This repository has been archived by the owner on Aug 22, 2022. It is now read-only.

Commit

Permalink
docs
Browse files Browse the repository at this point in the history
  • Loading branch information
markbates committed Oct 16, 2019
1 parent e5215ae commit 4e9c2f8
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 28 deletions.
6 changes: 6 additions & 0 deletions apply.go
Expand Up @@ -6,7 +6,13 @@ import (
"github.com/markbates/pkger/pkging"
)

// Apply will wrap the current implementation
// of pkger.Pkger with the new pkg. This allows
// for layering of pkging.Pkger implementations.
func Apply(pkg pkging.Pkger, err error) error {
if err != nil {
return err
}
gil.Lock()
defer gil.Unlock()
current = pkging.Wrap(current, pkg)
Expand Down
3 changes: 3 additions & 0 deletions apply_debug.go
Expand Up @@ -12,6 +12,9 @@ import (
func Apply(pkg pkging.Pkger, err error) error {
gil.Lock()
defer gil.Unlock()
if err != nil {
return err
}
if err := pkgutil.Dump(os.Stdout, pkg); err != nil {
return err
}
Expand Down
21 changes: 0 additions & 21 deletions doc.go

This file was deleted.

5 changes: 5 additions & 0 deletions pkger.go
Expand Up @@ -35,22 +35,27 @@ func impl() pkging.Pkger {
return current
}

// Parse the string in here.Path format.
func Parse(p string) (here.Path, error) {
return impl().Parse(p)
}

// Abs returns an absolute representation of path. If the path is not absolute it will be joined with the current working directory to turn it into an absolute path. The absolute path name for a given file is not guaranteed to be unique. Abs calls Clean on the result.
func Abs(p string) (string, error) {
return impl().Abs(p)
}

// AbsPath returns an absolute representation of here.Path. If the path is not absolute it will be joined with the current working directory to turn it into an absolute path. The absolute path name for a given file is not guaranteed to be unique. AbsPath calls Clean on the result.
func AbsPath(p here.Path) (string, error) {
return impl().AbsPath(p)
}

// Current returns the here.Info representing the current Pkger implementation.
func Current() (here.Info, error) {
return impl().Current()
}

// Info returns the here.Info of the here.Path
func Info(p string) (here.Info, error) {
return impl().Info(p)
}
Expand Down
22 changes: 16 additions & 6 deletions pkging/mem/mem.go
Expand Up @@ -17,12 +17,7 @@ import (

var _ pkging.Pkger = &Pkger{}

func WithInfo(fx *Pkger, infos ...here.Info) {
for _, info := range infos {
fx.infos.Store(info.ImportPath, info)
}
}

// New returns *Pkger for the provided here.Info
func New(info here.Info) (*Pkger, error) {
f := &Pkger{
infos: &maps.Infos{},
Expand All @@ -46,6 +41,7 @@ type jay struct {
Current here.Info `json:"current"`
}

// MarshalJSON creates a fully re-hydratable JSON representation of *Pkger
func (p *Pkger) MarshalJSON() ([]byte, error) {
files := map[string]*File{}

Expand All @@ -65,6 +61,7 @@ func (p *Pkger) MarshalJSON() ([]byte, error) {
})
}

// UnmarshalJSON re-hydrates the *Pkger
func (p *Pkger) UnmarshalJSON(b []byte) error {
y := jay{}

Expand All @@ -87,6 +84,7 @@ func (p *Pkger) UnmarshalJSON(b []byte) error {
return nil
}

// Abs returns an absolute representation of path. If the path is not absolute it will be joined with the current working directory to turn it into an absolute path. The absolute path name for a given file is not guaranteed to be unique. Abs calls Clean on the result.
func (f *Pkger) Abs(p string) (string, error) {
pt, err := f.Parse(p)
if err != nil {
Expand All @@ -95,14 +93,17 @@ func (f *Pkger) Abs(p string) (string, error) {
return f.AbsPath(pt)
}

// AbsPath returns an absolute representation of here.Path. If the path is not absolute it will be joined with the current working directory to turn it into an absolute path. The absolute path name for a given file is not guaranteed to be unique. AbsPath calls Clean on the result.
func (f *Pkger) AbsPath(pt here.Path) (string, error) {
return pt.String(), nil
}

// Current returns the here.Info representing the current Pkger implementation.
func (f *Pkger) Current() (here.Info, error) {
return f.current, nil
}

// Info returns the here.Info of the here.Path
func (f *Pkger) Info(p string) (here.Info, error) {
info, ok := f.infos.Load(p)
if !ok {
Expand All @@ -112,10 +113,12 @@ func (f *Pkger) Info(p string) (here.Info, error) {
return info, nil
}

// Parse the string in here.Path format.
func (f *Pkger) Parse(p string) (here.Path, error) {
return f.current.Parse(p)
}

// Remove removes the named file or (empty) directory.
func (fx *Pkger) Remove(name string) error {
pt, err := fx.Parse(name)
if err != nil {
Expand All @@ -130,6 +133,7 @@ func (fx *Pkger) Remove(name string) error {
return nil
}

// 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 (fx *Pkger) RemoveAll(name string) error {
pt, err := fx.Parse(name)
if err != nil {
Expand All @@ -146,6 +150,7 @@ func (fx *Pkger) RemoveAll(name string) error {
return nil
}

// Add copies the pkging.File into the *Pkger
func (fx *Pkger) Add(f pkging.File) error {
fx.MkdirAll("/", 0755)
info, err := f.Stat()
Expand Down Expand Up @@ -180,6 +185,7 @@ func (fx *Pkger) Add(f pkging.File) error {
return nil
}

// 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 (fx *Pkger) Create(name string) (pkging.File, error) {
fx.MkdirAll("/", 0755)
pt, err := fx.Parse(name)
Expand Down Expand Up @@ -215,6 +221,7 @@ func (fx *Pkger) Create(name string) (pkging.File, error) {
return f, 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 (fx *Pkger) MkdirAll(p string, perm os.FileMode) error {
path, err := fx.Parse(p)
if err != nil {
Expand Down Expand Up @@ -267,6 +274,7 @@ func (fx *Pkger) MkdirAll(p string, perm os.FileMode) error {

}

// 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 (fx *Pkger) Open(name string) (pkging.File, error) {
pt, err := fx.Parse(name)
if err != nil {
Expand Down Expand Up @@ -296,6 +304,7 @@ func (fx *Pkger) Open(name string) (pkging.File, error) {
return nf, nil
}

// Stat returns a FileInfo describing the named file.
func (fx *Pkger) Stat(name string) (os.FileInfo, error) {
pt, err := fx.Parse(name)
if err != nil {
Expand All @@ -308,6 +317,7 @@ func (fx *Pkger) Stat(name string) (os.FileInfo, error) {
return nil, fmt.Errorf("could not stat %s", pt)
}

// 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 (f *Pkger) Walk(p string, wf filepath.WalkFunc) error {
keys := f.files.Keys()

Expand Down
1 change: 0 additions & 1 deletion pkging/mem/stuffing.go
Expand Up @@ -30,7 +30,6 @@ func Stuff(w io.Writer, cur here.Info, paths []here.Path) error {
}

return nil
// WithInfo(ng, og)
}()
if err != nil {
return err
Expand Down
8 changes: 8 additions & 0 deletions pkging/pkger.go
Expand Up @@ -8,11 +8,19 @@ import (
)

type Pkger interface {
// Parse the string in here.Path format.
Parse(p string) (here.Path, error)

// Abs returns an absolute representation of path. If the path is not absolute it will be joined with the current working directory to turn it into an absolute path. The absolute path name for a given file is not guaranteed to be unique. Abs calls Clean on the result.
Abs(p string) (string, error)

// AbsPath returns an absolute representation of here.Path. If the path is not absolute it will be joined with the current working directory to turn it into an absolute path. The absolute path name for a given file is not guaranteed to be unique. AbsPath calls Clean on the result.
AbsPath(here.Path) (string, error)

// Current returns the here.Info representing the current Pkger implementation.
Current() (here.Info, error)

// Info returns the here.Info of the here.Path
Info(p string) (here.Info, error)

// 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.
Expand Down
13 changes: 13 additions & 0 deletions pkging/stdos/stdos.go
Expand Up @@ -18,6 +18,7 @@ type Pkger struct {
infos *maps.Infos
}

// Abs returns an absolute representation of path. If the path is not absolute it will be joined with the current working directory to turn it into an absolute path. The absolute path name for a given file is not guaranteed to be unique. Abs calls Clean on the result.
func (f *Pkger) Abs(p string) (string, error) {
pt, err := f.Parse(p)
if err != nil {
Expand All @@ -26,6 +27,7 @@ func (f *Pkger) Abs(p string) (string, error) {
return f.AbsPath(pt)
}

// AbsPath returns an absolute representation of here.Path. If the path is not absolute it will be joined with the current working directory to turn it into an absolute path. The absolute path name for a given file is not guaranteed to be unique. AbsPath calls Clean on the result.
func (f *Pkger) AbsPath(pt here.Path) (string, error) {
if pt.Pkg == f.Here.ImportPath {
return filepath.Join(f.Here.Dir, pt.Name), nil
Expand All @@ -37,6 +39,7 @@ func (f *Pkger) AbsPath(pt here.Path) (string, error) {
return filepath.Join(info.Dir, pt.Name), nil
}

// New returns *Pkger for the provided here.Info
func New(her here.Info) (*Pkger, error) {
p := &Pkger{
infos: &maps.Infos{},
Expand All @@ -46,6 +49,7 @@ func New(her here.Info) (*Pkger, error) {
return p, nil
}

// 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 (fx *Pkger) Create(name string) (pkging.File, error) {
name, err := fx.Abs(name)
if err != nil {
Expand Down Expand Up @@ -80,10 +84,12 @@ func (fx *Pkger) Create(name string) (pkging.File, error) {
return nf, nil
}

// Current returns the here.Info representing the current Pkger implementation.
func (f *Pkger) Current() (here.Info, error) {
return f.Here, nil
}

// Info returns the here.Info of the here.Path
func (f *Pkger) Info(p string) (here.Info, error) {
info, ok := f.infos.Load(p)
if ok {
Expand All @@ -98,6 +104,7 @@ func (f *Pkger) Info(p string) (here.Info, error) {
return info, 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 (f *Pkger) MkdirAll(p string, perm os.FileMode) error {
p, err := f.Abs(p)
if err != nil {
Expand All @@ -106,6 +113,7 @@ func (f *Pkger) MkdirAll(p string, perm os.FileMode) error {
return os.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.
func (fx *Pkger) Open(name string) (pkging.File, error) {
pt, err := fx.Parse(name)
if err != nil {
Expand Down Expand Up @@ -142,10 +150,12 @@ func (fx *Pkger) Open(name string) (pkging.File, error) {
return nf, nil
}

// Parse the string in here.Path format.
func (f *Pkger) Parse(p string) (here.Path, error) {
return f.Here.Parse(p)
}

// Stat returns a FileInfo describing the named file.
func (f *Pkger) Stat(name string) (os.FileInfo, error) {
pt, err := f.Parse(name)
if err != nil {
Expand All @@ -167,6 +177,7 @@ func (f *Pkger) Stat(name string) (os.FileInfo, error) {
return info, nil
}

// 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 (f *Pkger) Walk(p string, wf filepath.WalkFunc) error {
fp, err := f.Abs(p)
if err != nil {
Expand Down Expand Up @@ -198,6 +209,7 @@ func (f *Pkger) Walk(p string, wf filepath.WalkFunc) error {
return err
}

// Remove removes the named file or (empty) directory.
func (fx *Pkger) Remove(name string) error {
name, err := fx.Abs(name)
if err != nil {
Expand All @@ -206,6 +218,7 @@ func (fx *Pkger) Remove(name string) error {
return os.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).
func (fx *Pkger) RemoveAll(name string) error {
name, err := fx.Abs(name)
if err != nil {
Expand Down

0 comments on commit 4e9c2f8

Please sign in to comment.