From 34a3aeb632dca2f35be78babf11c334ff55ace45 Mon Sep 17 00:00:00 2001 From: Mark Bates Date: Fri, 2 Aug 2019 18:14:48 -0400 Subject: [PATCH] lost in the supermarket! --- cmd/pkger/pack.go | 22 +++------------------- files_map.go | 2 +- index.go | 5 ++--- index_test.go | 4 ++++ pkger.go | 28 ++++++++++++++++++++++------ 5 files changed, 32 insertions(+), 29 deletions(-) diff --git a/cmd/pkger/pack.go b/cmd/pkger/pack.go index 380092c..c283783 100644 --- a/cmd/pkger/pack.go +++ b/cmd/pkger/pack.go @@ -2,10 +2,7 @@ package main import ( "bytes" - "compress/gzip" - "encoding/base64" "os" - "strconv" "text/template" "github.com/markbates/pkger" @@ -39,19 +36,11 @@ func Package(out string, paths []pkger.Path) error { os.RemoveAll(out) bb := &bytes.Buffer{} - gz := gzip.NewWriter(bb) - defer gz.Close() - if err := pkger.Pack(gz, paths); err != nil { + if err := pkger.Pack(bb, paths); err != nil { return err } - if err := gz.Close(); err != nil { - return err - } - - s := base64.StdEncoding.EncodeToString(bb.Bytes()) - c, err := pkger.Current() if err != nil { return err @@ -61,7 +50,7 @@ func Package(out string, paths []pkger.Path) error { Data string }{ Pkg: c.Name, - Data: strconv.Quote(s), + Data: bb.String(), } f, err := os.Create(out) @@ -80,9 +69,4 @@ func Package(out string, paths []pkger.Path) error { return nil } -const outTmpl = `package {{.Pkg}} - -import "github.com/markbates/pkger" - -var _ = pkger.Unpack({{.Data}}) -` +const outTmpl = "package {{.Pkg}}\nimport \"github.com/markbates/pkger\"\nvar _ = pkger.Unpack(`{{.Data}}`) " diff --git a/files_map.go b/files_map.go index eab1ea1..056e141 100644 --- a/files_map.go +++ b/files_map.go @@ -25,7 +25,7 @@ func (m *filesMap) Data() *sync.Map { func (m *filesMap) MarshalJSON() ([]byte, error) { var err error mm := map[string]interface{}{} - m.data.Range(func(key, value interface{}) bool { + m.Data().Range(func(key, value interface{}) bool { var b []byte b, err = json.Marshal(key) if err != nil { diff --git a/index.go b/index.go index 226dae8..569f903 100644 --- a/index.go +++ b/index.go @@ -36,14 +36,13 @@ func (i *index) Info(p string) (here.Info, error) { } func (i *index) Current() (here.Info, error) { - var err error i.once.Do(func() { - i.current, err = here.Cache("", func(string) (here.Info, error) { + i.current, _ = here.Cache("", func(string) (here.Info, error) { return here.Current() }) }) - return i.current, err + return i.current, nil } func (i *index) Create(pt Path) (*File, error) { diff --git a/index_test.go b/index_test.go index 574e48c..67eb68d 100644 --- a/index_test.go +++ b/index_test.go @@ -101,4 +101,8 @@ func Test_index_JSON(t *testing.T) { r.NotZero(i2.current) r.Equal(1, len(i2.Files.Keys())) r.Equal(1, len(i2.Infos.Keys())) + + f2, err := i2.Open(Path{Name: "/radio.radio"}) + r.NoError(err) + r.Equal(f.data, f2.data) } diff --git a/pkger.go b/pkger.go index 9141459..99eeec2 100644 --- a/pkger.go +++ b/pkger.go @@ -3,8 +3,9 @@ package pkger import ( "bytes" "compress/gzip" - "encoding/base64" + "encoding/hex" "encoding/json" + "fmt" "io" "log" ) @@ -28,28 +29,34 @@ func Create(p string) (*File, error) { } func Unpack(ind string) error { - b, err := base64.StdEncoding.DecodeString(ind) + b, err := hex.DecodeString(ind) if err != nil { - log.Fatal(err) + log.Fatal("hex.DecodeString", err) return err } gz, err := gzip.NewReader(bytes.NewReader(b)) if err != nil { - log.Fatal(err) + log.Fatal("gzip.NewReader", err) return err } defer gz.Close() if err := json.NewDecoder(gz).Decode(rootIndex); err != nil { - log.Fatal(err) + log.Fatal("json.NewDecoder", err) return err } + fmt.Println(rootIndex.Files.Keys()) + return nil } func Pack(out io.Writer, paths []Path) error { + bb := &bytes.Buffer{} + gz := gzip.NewWriter(bb) + defer gz.Close() + for _, p := range paths { f, err := Open(p.String()) if err != nil { @@ -65,5 +72,14 @@ func Pack(out io.Writer, paths []Path) error { rootIndex.Files.Store(p, f) f.Close() } - return json.NewEncoder(out).Encode(rootIndex) + + if err := json.NewEncoder(gz).Encode(rootIndex); err != nil { + return err + } + if err := gz.Close(); err != nil { + return err + } + s := hex.EncodeToString(bb.Bytes()) + fmt.Fprint(out, s) + return nil }