to bring you my love

This commit is contained in:
Mark Bates 2019-10-24 16:22:15 -04:00
parent 545b970a33
commit 1154513c57
18 changed files with 268 additions and 376 deletions

View File

@ -121,13 +121,12 @@ func run() error {
```bash ```bash
# compile the go binary as usual and run the app: # compile the go binary as usual and run the app:
$ go build -v; ./app $ go build -v; ./app
/public | 128 | drwxr-xr-x | 2019-10-17T15:02:57-04:00 |
/public/images | 192 | drwxr-xr-x | 2019-10-17T15:02:57-04:00 | public | 128 | drwxr-xr-x | 2019-10-18T16:24:55-04:00 |
/public/images/mark-small.png | 649549 | -rw-r--r-- | 2019-10-17T15:02:56-04:00 | images | 128 | drwxr-xr-x | 2019-10-18T16:24:55-04:00 |
/public/images/img1.png | 50401191 | -rw-r--r-- | 2019-10-17T15:02:57-04:00 | img1.png | 27718 | -rw-r--r-- | 2019-10-18T16:24:55-04:00 |
/public/images/mark_250px.png | 27718 | -rw-r--r-- | 2019-10-17T15:02:57-04:00 | img2.png | 27718 | -rw-r--r-- | 2019-10-18T16:24:55-04:00 |
/public/images/mark_400px.png | 63543 | -rw-r--r-- | 2019-10-17T15:02:57-04:00 | index.html | 257 | -rw-r--r-- | 2019-10-18T16:24:55-04:00 |
/public/index.html | 257 | -rw-r--r-- | 2019-10-17T15:02:57-04:00 |
``` ```
### Output With Packing ### Output With Packing
@ -138,11 +137,10 @@ $ pkger
# compile the go binary as usual and run the app: # compile the go binary as usual and run the app:
$ go build -v; ./app $ go build -v; ./app
/ | 128 | drwxr-xr-x | 2019-10-17T15:02:57-04:00 |
/images | 192 | drwxr-xr-x | 2019-10-17T15:02:57-04:00 | public | 128 | drwxr-xr-x | 2019-10-18T16:24:55-04:00 |
/images/mark-small.png | 649549 | -rw-r--r-- | 2019-10-17T15:02:56-04:00 | images | 128 | drwxr-xr-x | 2019-10-18T16:24:55-04:00 |
/images/img1.png | 50401191 | -rw-r--r-- | 2019-10-17T15:02:57-04:00 | img1.png | 27718 | -rw-r--r-- | 2019-10-18T16:24:55-04:00 |
/images/mark_250px.png | 27718 | -rw-r--r-- | 2019-10-17T15:02:57-04:00 | img2.png | 27718 | -rw-r--r-- | 2019-10-18T16:24:55-04:00 |
/images/mark_400px.png | 63543 | -rw-r--r-- | 2019-10-17T15:02:57-04:00 | index.html | 257 | -rw-r--r-- | 2019-10-18T16:24:55-04:00 |
/index.html | 257 | -rw-r--r-- | 2019-10-17T15:02:57-04:00 |
``` ```

View File

@ -37,6 +37,7 @@ func Test_Info_Parse(t *testing.T) {
{in: ip2 + ":" + name, exp: here.Path{Pkg: ip2, Name: name}}, {in: ip2 + ":" + name, exp: here.Path{Pkg: ip2, Name: name}},
{in: ip2, exp: here.Path{Pkg: ip2, Name: "/"}}, {in: ip2, exp: here.Path{Pkg: ip2, Name: "/"}},
{in: ip2 + ":", exp: here.Path{Pkg: ip2, Name: "/"}}, {in: ip2 + ":", exp: here.Path{Pkg: ip2, Name: "/"}},
{in: filepath.Join(app.Info.Dir, "public"), exp: here.Path{Pkg: ip, Name: "/public"}},
} }
for _, tt := range table { for _, tt := range table {

View File

@ -66,7 +66,7 @@ func Test_Create(t *testing.T) {
defer RemoveAll("/tmp") defer RemoveAll("/tmp")
f, err := Create("/tmp/test.create") f, err := Create("/tmp/test.create")
r.NoError(err) r.NoError(err)
r.Equal("/tmp/test.create", f.Name()) r.Equal("github.com/markbates/pkger:/tmp/test.create", f.Name())
r.NoError(f.Close()) r.NoError(f.Close())
} }
@ -80,7 +80,7 @@ func Test_MkdirAll(t *testing.T) {
f, err := Open("/tmp") f, err := Open("/tmp")
r.NoError(err) r.NoError(err)
r.Equal("/tmp", f.Name()) r.Equal("github.com/markbates/pkger:/tmp", f.Name())
r.NoError(f.Close()) r.NoError(f.Close())
} }
@ -89,7 +89,7 @@ func Test_Stat(t *testing.T) {
info, err := Stat("/go.mod") info, err := Stat("/go.mod")
r.NoError(err) r.NoError(err)
r.Equal("/go.mod", info.Name()) r.Equal("go.mod", info.Name())
} }
func Test_Walk(t *testing.T) { func Test_Walk(t *testing.T) {
@ -115,7 +115,6 @@ func Test_Remove(t *testing.T) {
defer RemoveAll("/tmp") defer RemoveAll("/tmp")
f, err := Create("/tmp/test.create") f, err := Create("/tmp/test.create")
r.NoError(err) r.NoError(err)
r.Equal("/tmp/test.create", f.Name())
r.NoError(f.Close()) r.NoError(f.Close())
r.NoError(Remove("/tmp/test.create")) r.NoError(Remove("/tmp/test.create"))

View File

@ -1,5 +1,7 @@
package pkging package pkging
import "os"
type Adder interface { type Adder interface {
Add(files ...File) error Add(files ...*os.File) error
} }

View File

@ -3,7 +3,6 @@ package pkging
import ( import (
"encoding/json" "encoding/json"
"os" "os"
"strings"
"time" "time"
) )
@ -15,6 +14,7 @@ type Details struct {
IsDir bool `json:"is_dir"` IsDir bool `json:"is_dir"`
Sys interface{} `json:"sys"` Sys interface{} `json:"sys"`
} }
type FileInfo struct { type FileInfo struct {
Details `json:"details"` Details `json:"details"`
} }
@ -53,7 +53,7 @@ var _ os.FileInfo = &FileInfo{}
func NewFileInfo(info os.FileInfo) *FileInfo { func NewFileInfo(info os.FileInfo) *FileInfo {
fi := &FileInfo{ fi := &FileInfo{
Details: Details{ Details: Details{
Name: cleanName(info.Name()), Name: info.Name(),
Size: info.Size(), Size: info.Size(),
Mode: info.Mode(), Mode: info.Mode(),
ModTime: ModTime(info.ModTime()), ModTime: ModTime(info.ModTime()),
@ -63,29 +63,3 @@ func NewFileInfo(info os.FileInfo) *FileInfo {
} }
return fi return fi
} }
func WithName(name string, info os.FileInfo) *FileInfo {
fo := NewFileInfo(info)
fo.Details.Name = cleanName(name)
return fo
}
func WithRelName(name string, info os.FileInfo) *FileInfo {
fo := NewFileInfo(info)
s := cleanName(name)
s = strings.TrimPrefix(s, "/")
fo.Details.Name = s
return fo
}
func cleanName(s string) string {
if strings.Contains(s, "\\") {
s = strings.Replace(s, "\\", "/", -1)
}
if !strings.HasPrefix(s, "/") {
s = "/" + s
}
return s
}

31
pkging/file_info_test.go Normal file
View File

@ -0,0 +1,31 @@
package pkging_test
import (
"os"
"path/filepath"
"testing"
"time"
"github.com/markbates/pkger/pkging"
"github.com/markbates/pkger/pkging/pkgtest"
"github.com/stretchr/testify/require"
)
func Test_NewFileInfo(t *testing.T) {
r := require.New(t)
app, err := pkgtest.App()
r.NoError(err)
exp, err := os.Stat(filepath.Join(app.Info.Dir, "go.mod"))
r.NoError(err)
act := pkging.NewFileInfo(exp)
r.Equal(exp.Name(), act.Name())
r.Equal(exp.Size(), act.Size())
r.Equal(exp.Mode(), act.Mode())
r.Equal(exp.IsDir(), act.IsDir())
r.Equal(exp.ModTime().Format(time.RFC3339), act.ModTime().Format(time.RFC3339))
}

View File

@ -1,82 +0,0 @@
package pkging
import (
"os"
"testing"
"github.com/stretchr/testify/require"
)
func Test_NewFileInfo(t *testing.T) {
in := []string{
"/public/images/img1.png",
"public/images/img1.png",
"/public\\images/img1.png",
"public/images\\img1.png",
"\\public\\images\\img1.png",
"public\\images\\img1.png",
"\\public/images\\img1.png",
"public\\images/img1.png",
"\\public\\images\\img1.png",
}
const exp = "/public/images/img1.png"
for _, n := range in {
t.Run(n, func(st *testing.T) {
r := require.New(st)
f1 := &FileInfo{
Details: Details{
Name: n,
Size: 42,
Mode: os.FileMode(0644),
IsDir: true,
},
}
f2 := NewFileInfo(f1)
r.Equal(exp, f2.Name())
r.Equal(f1.Size(), f2.Size())
r.Equal(f1.Mode(), f2.Mode())
r.Equal(f1.IsDir(), f2.IsDir())
})
}
}
func Test_WithName(t *testing.T) {
f1 := &FileInfo{
Details: Details{
Name: "/walls/crumbling",
Size: 42,
Mode: os.FileMode(0644),
IsDir: true,
},
}
const exp = "/public/images/img1.png"
in := []string{
"/public/images/img1.png",
"public/images/img1.png",
"/public\\images/img1.png",
"public/images\\img1.png",
"\\public\\images\\img1.png",
"public\\images\\img1.png",
"\\public/images\\img1.png",
"public\\images/img1.png",
"\\public\\images\\img1.png",
}
for _, n := range in {
t.Run(n, func(st *testing.T) {
r := require.New(st)
f2 := WithName(n, f1)
r.Equal(exp, f2.Name())
})
}
}

View File

@ -3,32 +3,84 @@ package mem
import ( import (
"bytes" "bytes"
"io" "io"
"os"
"path/filepath" "path/filepath"
"github.com/markbates/pkger/here"
"github.com/markbates/pkger/pkging" "github.com/markbates/pkger/pkging"
) )
var _ pkging.Adder = &Pkger{}
// Add copies the pkging.File into the *Pkger // Add copies the pkging.File into the *Pkger
func (fx *Pkger) Add(files ...pkging.File) error { func (fx *Pkger) Add(files ...*os.File) error {
for _, f := range files { for _, f := range files {
info, err := f.Stat() info, err := f.Stat()
if err != nil { if err != nil {
return err return err
} }
if f.Path().Pkg == fx.Here.ImportPath { pt, err := fx.Parse(f.Name())
dir := filepath.Dir(f.Name()) if err != nil {
if dir != "/" { return err
if err := fx.MkdirAll(dir, 0755); err != nil { }
her, err := here.Package(pt.Pkg)
if err != nil {
return err
}
if info.IsDir() {
err = filepath.Walk(f.Name(), func(path string, info os.FileInfo, err error) error {
if err != nil {
return err return err
} }
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
pt, err := fx.Parse(path)
if err != nil {
return err
}
her, err := here.Package(pt.Pkg)
if err != nil {
return err
}
mf := &File{
Here: her,
info: pkging.NewFileInfo(info),
path: pt,
pkging: fx,
}
if !info.IsDir() {
bb := &bytes.Buffer{}
_, err = io.Copy(bb, f)
if err != nil {
return err
}
mf.data = bb.Bytes()
}
fx.files.Store(mf.Path(), mf)
return nil
})
if err != nil {
return err
} }
continue
} }
mf := &File{ mf := &File{
Here: f.Info(), Here: her,
info: pkging.NewFileInfo(info), info: pkging.NewFileInfo(info),
path: f.Path(), path: pt,
pkging: fx, pkging: fx,
} }

73
pkging/mem/add_test.go Normal file
View File

@ -0,0 +1,73 @@
package mem_test
import (
"os"
"path/filepath"
"sort"
"testing"
"github.com/markbates/pkger/pkging/mem"
"github.com/markbates/pkger/pkging/pkgtest"
"github.com/stretchr/testify/require"
)
func Test_Pkger_Add(t *testing.T) {
r := require.New(t)
app, err := pkgtest.App()
r.NoError(err)
pkg, err := mem.New(app.Info)
r.NoError(err)
root := app.Info.Dir
var exp []os.FileInfo
err = filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
exp = append(exp, info)
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
return pkg.Add(f)
})
r.NoError(err)
sort.Slice(exp, func(i, j int) bool {
return exp[i].Name() < exp[j].Name()
})
var act []os.FileInfo
err = pkg.Walk("/", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
act = append(act, info)
return nil
})
r.NoError(err)
sort.Slice(act, func(i, j int) bool {
return act[i].Name() < act[j].Name()
})
r.Len(act, len(exp))
for i, e := range exp {
a := act[i]
r.Equal(e.Name(), a.Name())
r.Equal(e.Size(), a.Size())
r.Equal(e.Mode(), a.Mode())
r.Equal(e.IsDir(), a.IsDir())
}
}

View File

@ -3,13 +3,13 @@ package mem_test
import ( import (
"bytes" "bytes"
"os" "os"
"path/filepath"
"testing" "testing"
"github.com/markbates/pkger/parser" "github.com/markbates/pkger/parser"
"github.com/markbates/pkger/pkging/mem" "github.com/markbates/pkger/pkging/mem"
"github.com/markbates/pkger/pkging/pkgtest" "github.com/markbates/pkger/pkging/pkgtest"
"github.com/markbates/pkger/pkging/pkgutil" "github.com/markbates/pkger/pkging/pkgutil"
"github.com/markbates/pkger/pkging/stdos"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -35,22 +35,16 @@ func Test_Pkger_Embedding(t *testing.T) {
base, err := mem.New(app.Info) base, err := mem.New(app.Info)
r.NoError(err) r.NoError(err)
disk, err := stdos.New(app.Info) err = filepath.Walk(app.Info.Dir, func(path string, info os.FileInfo, err error) error {
r.NoError(err)
err = disk.Walk("/", func(path string, info os.FileInfo, err error) error {
if err != nil { if err != nil {
return err return err
} }
if info.IsDir() { f, err := os.Open(path)
return nil
}
f, err := disk.Open(path)
if err != nil { if err != nil {
return err return err
} }
defer f.Close()
return base.Add(f) return base.Add(f)
}) })

View File

@ -8,7 +8,6 @@ import (
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"strings"
"time" "time"
"github.com/markbates/pkger/here" "github.com/markbates/pkger/here"
@ -100,9 +99,9 @@ func (f File) Stat() (os.FileInfo, error) {
return f.info, nil return f.info, nil
} }
// Name retuns the name of the file // Name retuns the name of the file in pkger format
func (f File) Name() string { func (f File) Name() string {
return f.info.Name() return f.path.String()
} }
// Abs returns an absolute representation of the file. 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 returns an absolute representation of the file. 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.
@ -148,7 +147,6 @@ func (f *File) Readdir(count int) ([]os.FileInfo, error) {
return nil return nil
} }
info = pkging.WithRelName(strings.TrimPrefix(info.Name(), f.parent.Name), info)
infos = append(infos, info) infos = append(infos, info)
if info.IsDir() && path != root { if info.IsDir() && path != root {
return filepath.SkipDir return filepath.SkipDir

View File

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"os" "os"
"path"
"path/filepath" "path/filepath"
"strings" "strings"
"time" "time"
@ -193,7 +194,7 @@ func (fx *Pkger) Create(name string) (pkging.File, error) {
path: pt, path: pt,
info: &pkging.FileInfo{ info: &pkging.FileInfo{
Details: pkging.Details{ Details: pkging.Details{
Name: pt.Name, Name: filepath.Base(name),
Mode: 0644, Mode: 0644,
ModTime: pkging.ModTime(time.Now()), ModTime: pkging.ModTime(time.Now()),
}, },
@ -208,53 +209,39 @@ func (fx *Pkger) Create(name string) (pkging.File, error) {
// 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 (fx *Pkger) MkdirAll(p string, perm os.FileMode) error { func (fx *Pkger) MkdirAll(p string, perm os.FileMode) error {
path, err := fx.Parse(p) pt, err := fx.Parse(p)
if err != nil { if err != nil {
return err return err
} }
root := path.Name dir, name := path.Split(pt.Name)
cur, err := fx.Current() if dir != "/" {
if err != nil { if err := fx.MkdirAll(dir, perm); err != nil {
return err return err
}
} }
for root != "" {
pt := here.Path{ if dir == "/" && name == "" {
Pkg: path.Pkg, dir = filepath.Base(fx.Here.Dir)
Name: root, }
}
if _, ok := fx.files.Load(pt); ok { f := &File{
root = filepath.Dir(root) Here: fx.Here,
if root == "/" || root == "\\" { pkging: fx,
break path: pt,
} info: &pkging.FileInfo{
continue Details: pkging.Details{
} IsDir: true,
f := &File{ Name: name,
Here: cur, Mode: perm,
pkging: fx, ModTime: pkging.ModTime(time.Now()),
path: pt,
info: &pkging.FileInfo{
Details: pkging.Details{
Name: pt.Name,
Mode: perm,
ModTime: pkging.ModTime(time.Now()),
},
}, },
} },
if err != nil {
return err
}
f.info.Details.IsDir = true
f.info.Details.Mode = perm
if err := f.Close(); err != nil {
return err
}
fx.files.Store(pt, f)
root = filepath.Dir(root)
} }
if err := f.Close(); err != nil {
return err
}
fx.files.Store(pt, f)
return nil return nil
} }
@ -280,7 +267,7 @@ func (fx *Pkger) Open(name string) (pkging.File, error) {
} }
nf := &File{ nf := &File{
pkging: fx, pkging: fx,
info: pkging.WithName(f.info.Name(), f.info), info: pkging.NewFileInfo(f.info),
path: f.path, path: f.path,
data: f.data, data: f.data,
Here: f.Here, Here: f.Here,
@ -333,7 +320,7 @@ func (f *Pkger) Walk(p string, wf filepath.WalkFunc) error {
return err return err
} }
fi = pkging.WithName(strings.TrimPrefix(k.Name, pt.Name), fi) fi = pkging.NewFileInfo(fi)
err = wf(k.String(), fi, nil) err = wf(k.String(), fi, nil)
if err == filepath.SkipDir { if err == filepath.SkipDir {

View File

@ -1,54 +1,12 @@
package pkgtest package pkgtest
import ( import (
"path/filepath"
"sort" "sort"
"testing" "testing"
"github.com/markbates/pkger/pkging/pkgutil"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
func (s Suite) Test_File_Info(t *testing.T) {
r := require.New(t)
pkg, err := s.Make()
r.NoError(err)
cur, err := pkg.Current()
r.NoError(err)
ip := cur.ImportPath
table := []struct {
in string
}{
{in: mould},
{in: ":" + mould},
{in: ip + ":" + mould},
}
for _, tt := range table {
s.Run(t, tt.in, func(st *testing.T) {
r := require.New(st)
r.NoError(pkg.RemoveAll(mould))
r.NoError(pkg.MkdirAll(filepath.Dir(tt.in), 0755))
err := pkgutil.WriteFile(pkg, tt.in, []byte(mould), 0644)
r.NoError(err)
f, err := pkg.Open(tt.in)
r.NoError(err)
r.Equal(mould, f.Name())
r.Equal(cur.ImportPath, f.Info().ImportPath)
r.NoError(f.Close())
})
}
}
// func (s Suite) Test_File_Read(t *testing.T) {
// panic("not implemented")
// }
//
func (s Suite) Test_File_Readdir(t *testing.T) { func (s Suite) Test_File_Readdir(t *testing.T) {
r := require.New(t) r := require.New(t)
@ -104,16 +62,3 @@ func (s Suite) Test_File_Readdir(t *testing.T) {
}) })
} }
} }
//
// func (s Suite) Test_File_Seek(t *testing.T) {
// panic("not implemented")
// }
//
// func (s Suite) Test_File_Stat(t *testing.T) {
// panic("not implemented")
// }
//
// func (s Suite) Test_File_Write(t *testing.T) {
// panic("not implemented")
// }

View File

@ -64,16 +64,16 @@ func (s Suite) LoadFolder(pkg pkging.Pkger) error {
if err != nil { if err != nil {
return err return err
} }
if info.IsDir() {
return nil
}
of, err := os.Open(path) of, err := os.Open(path)
if err != nil { if err != nil {
return err return err
} }
defer of.Close() defer of.Close()
if a, ok := pkg.(pkging.Adder); ok {
return a.Add(of)
}
path = strings.TrimPrefix(path, app.Dir) path = strings.TrimPrefix(path, app.Dir)
pt, err := pkg.Parse(path) pt, err := pkg.Parse(path)
@ -85,6 +85,9 @@ func (s Suite) LoadFolder(pkg pkging.Pkger) error {
return err return err
} }
if info.IsDir() {
return nil
}
f, err := pkg.Create(pt.String()) f, err := pkg.Create(pt.String())
if err != nil { if err != nil {
return err return err

View File

@ -2,7 +2,6 @@ package pkgtest
import ( import (
"fmt" "fmt"
"io"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
@ -102,13 +101,13 @@ func (s Suite) Test_Create(t *testing.T) {
f, err := pkg.Create(pt.Name) f, err := pkg.Create(pt.Name)
r.NoError(err) r.NoError(err)
r.Equal(pt.Name, f.Name()) r.Equal(pt.String(), f.Name())
fi, err := f.Stat() fi, err := f.Stat()
r.NoError(err) r.NoError(err)
r.NoError(f.Close()) r.NoError(f.Close())
r.Equal(pt.Name, fi.Name()) r.Equal(filepath.Base(pt.Name), fi.Name())
r.NotZero(fi.ModTime()) r.NotZero(fi.ModTime())
r.NoError(pkg.RemoveAll(pt.String())) r.NoError(pkg.RemoveAll(pt.String()))
}) })
@ -218,7 +217,7 @@ func (s Suite) Test_MkdirAll(t *testing.T) {
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
dir = strings.Replace(dir, "\\", "/", -1) dir = strings.Replace(dir, "\\", "/", -1)
} }
r.Equal(dir, fi.Name()) r.Equal(filepath.Base(dir), fi.Name())
r.NotZero(fi.ModTime()) r.NotZero(fi.ModTime())
r.NoError(pkg.RemoveAll(pt.String())) r.NoError(pkg.RemoveAll(pt.String()))
}) })
@ -354,87 +353,51 @@ func (s Suite) Test_Stat_Error(t *testing.T) {
func (s Suite) Test_Stat_Dir(t *testing.T) { func (s Suite) Test_Stat_Dir(t *testing.T) {
r := require.New(t) r := require.New(t)
app, err := App()
r.NoError(err)
exp, err := os.Stat(filepath.Join(app.Info.Dir, "public"))
r.NoError(err)
pkg, err := s.Make() pkg, err := s.Make()
r.NoError(err) r.NoError(err)
cur, err := pkg.Current() r.NoError(s.LoadFolder(pkg))
act, err := pkg.Stat("/public")
r.NoError(err) r.NoError(err)
dir := filepath.Dir(mould) r.Equal(exp.Name(), act.Name())
ip := cur.ImportPath r.Equal(exp.Size(), act.Size())
r.Equal(exp.Mode(), act.Mode())
r.Equal(exp.IsDir(), act.IsDir())
r.NotZero(act.ModTime())
table := []struct {
in string
}{
{in: ip},
{in: dir},
{in: ":" + dir},
{in: ip + ":" + dir},
}
for _, tt := range table {
s.Run(t, tt.in, func(st *testing.T) {
r := require.New(st)
pt, err := pkg.Parse(tt.in)
r.NoError(err)
r.NoError(pkg.RemoveAll(pt.String()))
r.NoError(pkg.MkdirAll(pt.Name, 0755))
info, err := pkg.Stat(tt.in)
r.NoError(err)
r.Equal(pt.Name, info.Name())
})
}
} }
func (s Suite) Test_Stat_File(t *testing.T) { func (s Suite) Test_Stat_File(t *testing.T) {
r := require.New(t) r := require.New(t)
app, err := App()
r.NoError(err)
exp, err := os.Stat(filepath.Join(app.Info.Dir, "go.mod"))
r.NoError(err)
pkg, err := s.Make() pkg, err := s.Make()
r.NoError(err) r.NoError(err)
cur, err := pkg.Current() r.NoError(s.LoadFolder(pkg))
act, err := pkg.Stat("/go.mod")
r.NoError(err) r.NoError(err)
ip := cur.ImportPath r.Equal(exp.Name(), act.Name())
table := []struct { r.Equal(exp.Size(), act.Size())
in string r.Equal(exp.Mode(), act.Mode())
}{ r.Equal(exp.IsDir(), act.IsDir())
{in: mould}, r.NotZero(act.ModTime())
{in: ":" + mould},
{in: ip + ":" + mould},
{in: hart},
}
for _, tt := range table {
s.Run(t, tt.in, func(st *testing.T) {
r := require.New(st)
pkg, err := s.Make()
r.NoError(err)
pt, err := pkg.Parse(tt.in)
r.NoError(err)
r.NoError(pkg.RemoveAll(pt.String()))
r.NoError(pkg.MkdirAll(filepath.Dir(pt.Name), 0755))
f, err := pkg.Create(tt.in)
r.NoError(err)
_, err = io.Copy(f, strings.NewReader("!"+pt.String()))
r.NoError(err)
r.NoError(f.Close())
info, err := pkg.Stat(tt.in)
r.NoError(err)
r.Equal(pt.Name, info.Name())
})
}
} }
func (s Suite) Test_Walk(t *testing.T) { func (s Suite) Test_Walk(t *testing.T) {

View File

@ -7,15 +7,9 @@ import (
"github.com/markbates/pkger/here" "github.com/markbates/pkger/here"
"github.com/markbates/pkger/parser" "github.com/markbates/pkger/parser"
"github.com/markbates/pkger/pkging/mem" "github.com/markbates/pkger/pkging/mem"
"github.com/markbates/pkger/pkging/stdos"
) )
func Stuff(w io.Writer, c here.Info, decls parser.Decls) error { func Stuff(w io.Writer, c here.Info, decls parser.Decls) error {
disk, err := stdos.New(c)
if err != nil {
return err
}
pkg, err := mem.New(c) pkg, err := mem.New(c)
if err != nil { if err != nil {
return err return err
@ -28,47 +22,16 @@ func Stuff(w io.Writer, c here.Info, decls parser.Decls) error {
for _, pf := range files { for _, pf := range files {
err = func() error { err = func() error {
df, err := disk.Open(pf.Path.String()) df, err := os.Open(pf.Abs)
if err != nil { if err != nil {
return err return err
} }
defer df.Close() defer df.Close()
info, err := df.Stat()
if err != nil {
return err
}
if err := pkg.Add(df); err != nil { if err := pkg.Add(df); err != nil {
return err return err
} }
if !info.IsDir() {
return nil
}
err = disk.Walk(df.Path().String(), func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
f, err := disk.Open(path)
if err != nil {
return err
}
defer f.Close()
if err := pkg.Add(f); err != nil {
return err
}
return nil
})
return err return err
}() }()

View File

@ -34,9 +34,9 @@ func (f *File) Info() here.Info {
return f.her return f.her
} }
// Name retuns the name of the file // Name retuns the name of the file in pkger format
func (f *File) Name() string { func (f File) Name() string {
return f.info.Name() return f.path.String()
} }
// Readdir reads the contents of the directory associated with file and returns a slice of up to n FileInfo values, as would be returned by Lstat, in directory order. Subsequent calls on the same file will yield further FileInfos. // Readdir reads the contents of the directory associated with file and returns a slice of up to n FileInfo values, as would be returned by Lstat, in directory order. Subsequent calls on the same file will yield further FileInfos.
@ -45,16 +45,7 @@ func (f *File) Name() string {
// //
// If n <= 0, Readdir returns all the FileInfo from the directory in a single slice. In this case, if Readdir succeeds (reads all the way to the end of the directory), it returns the slice and a nil error. If it encounters an error before the end of the directory, Readdir returns the FileInfo read until that point and a non-nil error. // If n <= 0, Readdir returns all the FileInfo from the directory in a single slice. In this case, if Readdir succeeds (reads all the way to the end of the directory), it returns the slice and a nil error. If it encounters an error before the end of the directory, Readdir returns the FileInfo read until that point and a non-nil error.
func (f *File) Readdir(count int) ([]os.FileInfo, error) { func (f *File) Readdir(count int) ([]os.FileInfo, error) {
osinfos, err := f.File.Readdir(count) return f.File.Readdir(count)
if err != nil {
return nil, err
}
infos := make([]os.FileInfo, len(osinfos))
for i, info := range osinfos {
infos[i] = pkging.WithRelName(info.Name(), info)
}
return infos, err
} }
// Open implements the http.FileSystem interface. A FileSystem implements access to a collection of named files. The elements in a file path are separated by slash ('/', U+002F) characters, regardless of host operating system convention. // Open implements the http.FileSystem interface. A FileSystem implements access to a collection of named files. The elements in a file path are separated by slash ('/', U+002F) characters, regardless of host operating system convention.
@ -82,6 +73,6 @@ func (f *File) Stat() (os.FileInfo, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
f.info = pkging.WithName(f.path.Name, info) f.info = pkging.NewFileInfo(info)
return f.info, nil return f.info, nil
} }

View File

@ -80,7 +80,7 @@ func (fx *Pkger) Create(name string) (pkging.File, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
nf.info = pkging.WithName(pt.Name, info) nf.info = pkging.NewFileInfo(info)
return nf, nil return nf, nil
} }
@ -145,7 +145,7 @@ func (fx *Pkger) Open(name string) (pkging.File, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
nf.info = pkging.WithName(pt.Name, info) nf.info = pkging.NewFileInfo(info)
return nf, nil return nf, nil
} }
@ -172,7 +172,7 @@ func (f *Pkger) Stat(name string) (os.FileInfo, error) {
return nil, err return nil, err
} }
info = pkging.WithName(pt.Name, pkging.NewFileInfo(info)) info = pkging.NewFileInfo(info)
return info, nil return info, nil
} }
@ -203,7 +203,7 @@ func (f *Pkger) Walk(p string, wf filepath.WalkFunc) error {
path = strings.TrimPrefix(path, info.Dir) path = strings.TrimPrefix(path, info.Dir)
path = strings.ReplaceAll(path, "\\", "/") path = strings.ReplaceAll(path, "\\", "/")
pt.Name = path pt.Name = path
return wf(pt.String(), pkging.WithName(path, pkging.NewFileInfo(fi)), nil) return wf(pt.String(), pkging.NewFileInfo(fi), nil)
}) })
return err return err