lost in the supermarket!

This commit is contained in:
Mark Bates 2019-08-02 18:14:48 -04:00
parent be03e3492e
commit 34a3aeb632
5 changed files with 32 additions and 29 deletions

View File

@ -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}}`) "

View File

@ -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 {

View File

@ -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) {

View File

@ -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)
}

View File

@ -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
}