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

Commit

Permalink
that's what i get
Browse files Browse the repository at this point in the history
  • Loading branch information
markbates committed Aug 3, 2019
1 parent 34a3aeb commit a4a55a5
Show file tree
Hide file tree
Showing 19 changed files with 215 additions and 125 deletions.
2 changes: 1 addition & 1 deletion cmd/pkger/list.go
Expand Up @@ -8,7 +8,7 @@ import (
)

func list(args []string) error {
info, err := pkger.Current()
info, err := pkger.Stat()
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/pkger/pack.go
Expand Up @@ -12,7 +12,7 @@ import (
const outName = "pkged.go"

func pack(args []string) error {
info, err := pkger.Current()
info, err := pkger.Stat()
if err != nil {
return err
}
Expand Down Expand Up @@ -41,7 +41,7 @@ func Package(out string, paths []pkger.Path) error {
return err
}

c, err := pkger.Current()
c, err := pkger.Stat()
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions current.go
Expand Up @@ -8,6 +8,6 @@ func Info(p string) (here.Info, error) {
return rootIndex.Info(p)
}

func Current() (here.Info, error) {
return rootIndex.Current()
func Stat() (here.Info, error) {
return rootIndex.Stat()
}
7 changes: 7 additions & 0 deletions debug.go
@@ -0,0 +1,7 @@
// +build debug

package pkger

func Debug(format string, a ...interface{}) {
fmt.Println("[PKGER] ", fmt.Sprintf(format, a...)
}
5 changes: 5 additions & 0 deletions debug_shim.go
@@ -0,0 +1,5 @@
// +build !debug

package pkger

func Debug(format string, a ...interface{}) {}
10 changes: 4 additions & 6 deletions file.go
Expand Up @@ -8,6 +8,7 @@ import (
"io/ioutil"
"net/http"
"os"
"path"
"time"

"github.com/gobuffalo/here"
Expand Down Expand Up @@ -62,12 +63,8 @@ func (f *File) Close() error {
}

func (f *File) Read(p []byte) (int, error) {
if len(f.data) > 0 && len(f.data) <= len(p) {
return copy(p, f.data), io.EOF
}

if len(f.data) > 0 {
f.reader = ioutil.NopCloser(bytes.NewReader(f.data))
if len(f.data) > 0 && f.reader == nil {
f.reader = bytes.NewReader(f.data)
}

if f.reader != nil {
Expand Down Expand Up @@ -168,6 +165,7 @@ func (f *File) Open(name string) (http.File, error) {
return f, nil
}

pt.Name = path.Join(f.Path().Name, pt.Name)
return rootIndex.Open(pt)
}

Expand Down
10 changes: 8 additions & 2 deletions files_map.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

153 changes: 90 additions & 63 deletions index.go
@@ -1,7 +1,6 @@
package pkger

import (
"encoding/json"
"fmt"
"os"
"path/filepath"
Expand All @@ -10,17 +9,40 @@ import (
"time"

"github.com/gobuffalo/here"
"github.com/markbates/hepa"
"github.com/markbates/hepa/filters"
)

type index struct {
Files *filesMap
Infos *infosMap
current here.Info
Files *filesMap `json:"files"`
Infos *infosMap `json:"infos"`
Paths *pathsMap `json:"paths"`
Current here.Info `json:"current"`
once sync.Once
}

func (i *index) Parse(p string) (Path, error) {
pt, ok := i.Paths.Load(p)
if ok {
return pt, nil
}
if len(p) == 0 {
return build(p, "", "")
}

res := pathrx.FindAllStringSubmatch(p, -1)
if len(res) == 0 {
return pt, fmt.Errorf("could not parse %q", p)
}

matches := res[0]

if len(matches) != 4 {
return pt, fmt.Errorf("could not parse %q", p)
}

return build(p, matches[1], matches[3])
return rootIndex.Parse(p)
}

func (i *index) Info(p string) (here.Info, error) {
info, ok := i.Infos.Load(p)
if ok {
Expand All @@ -35,14 +57,14 @@ func (i *index) Info(p string) (here.Info, error) {
return info, nil
}

func (i *index) Current() (here.Info, error) {
func (i *index) Stat() (here.Info, error) {
i.once.Do(func() {
i.current, _ = here.Cache("", func(string) (here.Info, error) {
i.Current, _ = here.Cache("", func(string) (here.Info, error) {
return here.Current()
})
})

return i.current, nil
return i.Current, nil
}

func (i *index) Create(pt Path) (*File, error) {
Expand All @@ -60,63 +82,67 @@ func (i *index) Create(pt Path) (*File, error) {
},
}

i.Files.Store(pt, f)
return f, nil
}

func (i *index) MarshalJSON() ([]byte, error) {
m := map[string]interface{}{}

m["files"] = i.Files
m["infos"] = i.Infos
m["current"] = i.current

b, err := json.Marshal(m)
if err != nil {
return nil, err
if i.Files == nil {
i.Files = &filesMap{}
}

hep := hepa.New()
hep = hepa.With(hep, filters.Golang())
hep = hepa.With(hep, filters.Secrets())
return hep.Filter(b)
i.Files.Store(pt, f)
return f, nil
}

func (i *index) UnmarshalJSON(b []byte) error {
m := map[string]json.RawMessage{}

if err := json.Unmarshal(b, &m); err != nil {
return err
}

infos, ok := m["infos"]
if !ok {
return fmt.Errorf("missing infos")
}
i.Infos = &infosMap{}
if err := json.Unmarshal(infos, i.Infos); err != nil {
return err
}

files, ok := m["files"]
if !ok {
return fmt.Errorf("missing files")
}

i.Files = &filesMap{}
if err := json.Unmarshal(files, i.Files); err != nil {
return err
}

current, ok := m["current"]
if !ok {
return fmt.Errorf("missing current")
}
if err := json.Unmarshal(current, &i.current); err != nil {
return err
}
return nil
}
// func (i *index) MarshalJSON() ([]byte, error) {
// m := map[string]interface{}{}
//
// m["files"] = i.Files
// m["infos"] = i.Infos
// m["current"] = i.Current
//
// b, err := json.Marshal(m)
// if err != nil {
// return nil, err
// }
//
// hep := hepa.New()
// hep = hepa.With(hep, filters.Golang())
// hep = hepa.With(hep, filters.Secrets())
// return hep.Filter(b)
// }

// func (i *index) UnmarshalJSON(b []byte) error {
// m := map[string]json.RawMessage{}
//
// if err := json.Unmarshal(b, &m); err != nil {
// return err
// }
//
// infos, ok := m["infos"]
// if !ok {
// return fmt.Errorf("missing infos")
// }
// i.Infos = &infosMap{}
// if err := json.Unmarshal(infos, i.Infos); err != nil {
// return err
// }
//
// files, ok := m["files"]
// if !ok {
// return fmt.Errorf("missing files")
// }
//
// i.Files = &filesMap{}
// if err := json.Unmarshal(files, i.Files); err != nil {
// return err
// }
//
// current, ok := m["current"]
// if !ok {
// return fmt.Errorf("missing current")
// }
// if err := json.Unmarshal(current, &i.Current); err != nil {
// return err
// }
// return nil
// }

func (i index) Walk(pt Path, wf WalkFunc) error {
var err error
Expand All @@ -136,7 +162,7 @@ func (i index) Walk(pt Path, wf WalkFunc) error {

var info here.Info
if pt.Pkg == "." {
info, err = Current()
info, err = Stat()
if err != nil {
return err
}
Expand Down Expand Up @@ -207,6 +233,7 @@ func newIndex() *index {
return &index{
Files: &filesMap{},
Infos: &infosMap{},
Paths: &pathsMap{},
}
}

Expand Down
6 changes: 3 additions & 3 deletions index_test.go
Expand Up @@ -77,7 +77,7 @@ func Test_index_JSON(t *testing.T) {
fmt.Fprint(f, radio)
r.NoError(f.Close())

c, err := i.Current()
c, err := i.Stat()
r.NoError(err)
r.Equal(curPkg, c.ImportPath)

Expand All @@ -86,7 +86,7 @@ func Test_index_JSON(t *testing.T) {

r.Equal(1, len(i.Files.Keys()))
r.Equal(1, len(i.Infos.Keys()))
r.NotZero(i.current)
r.NotZero(i.Current)

jason, err := json.Marshal(i)
r.NoError(err)
Expand All @@ -98,7 +98,7 @@ func Test_index_JSON(t *testing.T) {

r.NotNil(i2.Infos)
r.NotNil(i2.Files)
r.NotZero(i2.current)
r.NotZero(i2.Current)
r.Equal(1, len(i2.Files.Keys()))
r.Equal(1, len(i2.Infos.Keys()))

Expand Down
7 changes: 5 additions & 2 deletions infos_map.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions internal/examples/app/.gitignore
@@ -0,0 +1,2 @@
example
app
7 changes: 7 additions & 0 deletions internal/examples/app/Dockerfile
@@ -0,0 +1,7 @@
FROM alpine

EXPOSE 3000
COPY example /bin/
RUN ls -la

CMD /bin/example
6 changes: 6 additions & 0 deletions internal/examples/app/Makefile
@@ -0,0 +1,6 @@
default:
cd ../../../cmd/pkger && go install -v .
pkger
GOOS=linux go build -v -o example
docker build -t pkger:example .
docker run -p 3000:3000 pkger:example
2 changes: 2 additions & 0 deletions internal/examples/app/go.sum
Expand Up @@ -4,6 +4,8 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/gobuffalo/here v0.2.2 h1:AXEK2ApOb4F5cKZ46Ofi8inGWa0qy5ChmJXAK5/IDmo=
github.com/gobuffalo/here v0.2.2/go.mod h1:2a6G14FaAKOGJMK/5UNa4Og/+iyFS5cq3MnlvFR7YDk=
github.com/markbates/errx v1.1.0/go.mod h1:PLa46Oex9KNbVDZhKel8v1OT7hD5JZ2eI7AHhA0wswc=
github.com/markbates/hepa v0.0.0-20190718154049-1d900199db5b h1:ns0oO2sMEoFJMmrbiWzGQO5AR3GgqfYRAos0gz8C0Cw=
github.com/markbates/hepa v0.0.0-20190718154049-1d900199db5b/go.mod h1:jHlCX3RNqF+epcY1FxjLyDGzr3l9+mNCh3YDDw6BFvY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down

0 comments on commit a4a55a5

Please sign in to comment.