pkger/pkging/stdos/stdos.go

229 lines
5.7 KiB
Go
Raw Normal View History

2019-09-03 18:29:28 +03:00
package stdos
2019-09-01 00:00:24 +03:00
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/markbates/pkger/here"
"github.com/markbates/pkger/internal/maps"
2019-09-02 01:02:45 +03:00
"github.com/markbates/pkger/pkging"
2019-09-01 00:00:24 +03:00
)
2019-09-03 18:29:28 +03:00
var _ pkging.Pkger = &Pkger{}
2019-09-01 00:00:24 +03:00
2019-09-03 18:29:28 +03:00
type Pkger struct {
2019-10-09 20:21:54 +03:00
Here here.Info
infos *maps.Infos
2019-09-01 00:00:24 +03:00
}
2019-10-16 18:07:57 +03:00
// 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.
2019-09-03 18:29:28 +03:00
func (f *Pkger) Abs(p string) (string, error) {
2019-09-01 23:18:39 +03:00
pt, err := f.Parse(p)
if err != nil {
return "", err
}
return f.AbsPath(pt)
}
2019-10-16 18:07:57 +03:00
// 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.
2019-10-09 20:21:54 +03:00
func (f *Pkger) AbsPath(pt here.Path) (string, error) {
if pt.Pkg == f.Here.ImportPath {
return filepath.Join(f.Here.Dir, pt.Name), nil
2019-09-01 23:18:39 +03:00
}
info, err := f.Info(pt.Pkg)
if err != nil {
return "", err
}
return filepath.Join(info.Dir, pt.Name), nil
}
2019-10-16 18:07:57 +03:00
// New returns *Pkger for the provided here.Info
2019-10-09 20:21:54 +03:00
func New(her here.Info) (*Pkger, error) {
2019-09-20 17:56:26 +03:00
p := &Pkger{
2019-09-01 06:29:25 +03:00
infos: &maps.Infos{},
2019-10-09 20:21:54 +03:00
Here: her,
2019-09-20 17:56:26 +03:00
}
2019-10-09 20:21:54 +03:00
p.infos.Store(her.ImportPath, her)
2019-09-20 17:56:26 +03:00
return p, nil
2019-09-01 00:00:24 +03:00
}
2019-10-16 18:07:57 +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.
2019-09-03 18:29:28 +03:00
func (fx *Pkger) Create(name string) (pkging.File, error) {
2019-09-01 23:18:39 +03:00
name, err := fx.Abs(name)
2019-09-01 00:00:24 +03:00
if err != nil {
return nil, err
}
f, err := os.Create(name)
if err != nil {
return nil, err
}
2019-10-15 23:40:45 +03:00
pt, err := fx.Parse(name)
if err != nil {
return nil, err
}
her, err := fx.Info(pt.Pkg)
if err != nil {
return nil, err
}
nf := &File{
File: f,
her: her,
path: pt,
pkging: fx,
}
info, err := f.Stat()
if err != nil {
return nil, err
}
nf.info = pkging.WithName(pt.Name, info)
return nf, nil
2019-09-01 00:00:24 +03:00
}
2019-10-16 18:07:57 +03:00
// Current returns the here.Info representing the current Pkger implementation.
2019-09-03 18:29:28 +03:00
func (f *Pkger) Current() (here.Info, error) {
2019-10-09 20:21:54 +03:00
return f.Here, nil
2019-09-01 00:00:24 +03:00
}
2019-10-16 18:07:57 +03:00
// Info returns the here.Info of the here.Path
2019-09-03 18:29:28 +03:00
func (f *Pkger) Info(p string) (here.Info, error) {
2019-09-01 00:00:24 +03:00
info, ok := f.infos.Load(p)
if ok {
return info, nil
}
info, err := here.Package(p)
if err != nil {
return info, err
}
f.infos.Store(p, info)
return info, nil
}
2019-10-16 18:07:57 +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.
2019-09-03 18:29:28 +03:00
func (f *Pkger) MkdirAll(p string, perm os.FileMode) error {
2019-09-01 23:18:39 +03:00
p, err := f.Abs(p)
2019-09-01 00:00:24 +03:00
if err != nil {
return err
}
return os.MkdirAll(p, perm)
}
2019-10-16 18:07:57 +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.
2019-09-03 18:29:28 +03:00
func (fx *Pkger) Open(name string) (pkging.File, error) {
2019-10-15 23:40:45 +03:00
pt, err := fx.Parse(name)
if err != nil {
return nil, err
}
name, err = fx.Abs(name)
2019-09-01 00:00:24 +03:00
if err != nil {
return nil, err
}
f, err := os.Open(name)
if err != nil {
return nil, err
}
2019-10-15 23:40:45 +03:00
her, err := fx.Info(pt.Pkg)
if err != nil {
return nil, err
}
nf := &File{
File: f,
her: her,
path: pt,
pkging: fx,
}
info, err := f.Stat()
if err != nil {
return nil, err
}
nf.info = pkging.WithName(pt.Name, info)
return nf, nil
2019-09-01 00:00:24 +03:00
}
2019-10-16 18:07:57 +03:00
// Parse the string in here.Path format.
2019-10-09 20:21:54 +03:00
func (f *Pkger) Parse(p string) (here.Path, error) {
return f.Here.Parse(p)
2019-09-01 00:00:24 +03:00
}
2019-10-16 18:07:57 +03:00
// Stat returns a FileInfo describing the named file.
2019-09-03 18:29:28 +03:00
func (f *Pkger) Stat(name string) (os.FileInfo, error) {
2019-09-01 23:27:31 +03:00
pt, err := f.Parse(name)
2019-09-01 00:00:24 +03:00
if err != nil {
return nil, err
}
2019-09-01 23:27:31 +03:00
abs, err := f.AbsPath(pt)
if err != nil {
return nil, err
}
info, err := os.Stat(abs)
if err != nil {
return nil, err
}
2019-09-02 01:02:45 +03:00
info = pkging.WithName(pt.Name, pkging.NewFileInfo(info))
2019-09-01 23:27:31 +03:00
return info, nil
2019-09-01 00:00:24 +03:00
}
2019-10-16 18:07:57 +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.
2019-09-03 18:29:28 +03:00
func (f *Pkger) Walk(p string, wf filepath.WalkFunc) error {
2019-09-01 23:18:39 +03:00
fp, err := f.Abs(p)
2019-09-01 00:00:24 +03:00
if err != nil {
return err
}
pt, err := f.Parse(p)
if err != nil {
return err
}
err = filepath.Walk(fp, func(path string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
pt, err := f.Parse(fmt.Sprintf("%s:%s", pt.Pkg, path))
if err != nil {
return err
}
2019-10-16 17:11:54 +03:00
info, err := f.Info(pt.Pkg)
if err != nil {
return err
}
path = strings.TrimPrefix(path, info.Dir)
2019-10-16 17:30:06 +03:00
path = strings.ReplaceAll(path, "\\", "/")
2019-10-16 17:11:54 +03:00
pt.Name = path
2019-09-02 01:02:45 +03:00
return wf(pt.String(), pkging.WithName(path, pkging.NewFileInfo(fi)), nil)
2019-09-01 00:00:24 +03:00
})
return err
}
2019-10-16 18:07:57 +03:00
// Remove removes the named file or (empty) directory.
2019-09-03 18:29:28 +03:00
func (fx *Pkger) Remove(name string) error {
2019-09-01 23:18:39 +03:00
name, err := fx.Abs(name)
2019-09-01 05:45:22 +03:00
if err != nil {
return err
}
return os.Remove(name)
}
2019-10-16 18:07:57 +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).
2019-09-03 18:29:28 +03:00
func (fx *Pkger) RemoveAll(name string) error {
2019-09-01 23:18:39 +03:00
name, err := fx.Abs(name)
2019-09-01 05:45:22 +03:00
if err != nil {
return err
}
return os.RemoveAll(name)
}