diff --git a/file.go b/file.go index 0b35ecd..4d42823 100644 --- a/file.go +++ b/file.go @@ -5,7 +5,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "net/http" "os" "strings" @@ -91,73 +90,6 @@ func (f File) Info() here.Info { return f.her } -func (f File) MarshalJSON() ([]byte, error) { - m := map[string]interface{}{} - m["info"] = f.info - m["her"] = f.her - m["path"] = f.path - m["data"] = f.data - m["parent"] = f.parent - if !f.info.virtual { - if len(f.data) == 0 && !f.info.IsDir() { - b, err := ioutil.ReadAll(&f) - if err != nil { - return nil, err - } - m["data"] = b - } - } - - return json.Marshal(m) -} - -func (f *File) UnmarshalJSON(b []byte) error { - m := map[string]json.RawMessage{} - if err := json.Unmarshal(b, &m); err != nil { - return err - } - - info, ok := m["info"] - if !ok { - return fmt.Errorf("missing info") - } - - f.info = &FileInfo{} - if err := json.Unmarshal(info, f.info); err != nil { - return err - } - - her, ok := m["her"] - if !ok { - return fmt.Errorf("missing her") - } - if err := json.Unmarshal(her, &f.her); err != nil { - return err - } - - path, ok := m["path"] - if !ok { - return fmt.Errorf("missing path") - } - if err := json.Unmarshal(path, &f.path); err != nil { - return err - } - - parent, ok := m["parent"] - if !ok { - return fmt.Errorf("missing parent") - } - if err := json.Unmarshal(parent, &f.parent); err != nil { - return err - } - - if err := json.Unmarshal(m["data"], &f.data); err != nil { - return err - } - - return nil -} - func (f File) Stat() (os.FileInfo, error) { if f.info == nil { return nil, os.ErrNotExist diff --git a/file_info.go b/file_info.go index 84c3729..9611f8b 100644 --- a/file_info.go +++ b/file_info.go @@ -30,7 +30,7 @@ func (f *FileInfo) MarshalJSON() ([]byte, error) { "mode": f.mode, "modTime": f.modTime.Format(timeFmt), "isDir": f.isDir, - "virtual": f.virtual, + "virtual": true, "sys": f.sys, }) } @@ -75,6 +75,7 @@ func (f *FileInfo) UnmarshalJSON(b []byte) error { return fmt.Errorf("could not determine isDir %q", m["isDir"]) } f.sys = m["sys"] + f.virtual = true return nil } diff --git a/internal/examples/app/p.go b/internal/examples/app/p.go new file mode 100644 index 0000000..e9aa4c0 --- /dev/null +++ b/internal/examples/app/p.go @@ -0,0 +1,5 @@ +package main + +import "github.com/markbates/pkger" + +var _ = pkger.Unpack(`1f8b08000000000000ffec57dd6fdb38127fbfbfc29867d6d4a725eb69af97dba0c0a5d86bb3fb9204052dd112cf14a92329d7dec2fffb81946ccbf1479ddd1c50608320e6fc66f831331cfe487d8339e35443f60dbe3d42b3281f217b849299aa9d8d7359e352cedaf99c7089a958ae1f013d82203575ddf0236cecc88218029968394750516555374c4106f8574d95c635518b193154e31b259b995ce152e26651e25a16f8ec5a3f2dfd7132f600c187ba91cafc424c05d979df00c1475253c8a0473732870c1a922f4849475639aac982ead157a9164c94a3afcc54a37f7efc6db4248a9119a77ac4c4e8568e8c624b46f81810dc135552031900824f52f6d21d317905d9c3056f9e107c368453c88c6a690f3e51a2a5800c662de3c5e8c3cda866ba769321b8953f777bf1e022189712102ca9d24c0a0b9eb699d0db64dfd06627df536d9ecdf0c5506dfa91d6fc6cf49d2c5aebdf37b822b5778409c8e6846b8a5e77776fe59d2cae9c2e2779457121bf0a2ec985d9f14f4bdc2d30ae65e116f9ad4ba4ddbfcd06011373694367da05d3ed512d8b7be64a28f0fce93b2f7de705f77e9c05511606633f0cbd309e4ed2775e9479d6f55a1614b2c08f92280da79314b9b3d1958866bf53c8265e8a40afdd01bba14bc8fc49922441e0b9f45387bd08c147cec402327f8ae0839090a5699a7ad3340a6304bfb202b2d8f311dc5ac98efd54d8b93c04bf90e24b5eca2f1e640f1e727f4f08fe6e584d754373bbec67dbf8f1240ed328f542041fb5d324619806611a6f10dc9dea1f25499826c1aeff36fa0d827f5cd33ff2c220987ad30d82f74c99ea9a317e1a249eeffb1b049ff7f97bcf65bed02edef77cd12536f2a613043f735276865b2a5cfb2fdd10459df8ef5eb48979da205832655ac2bbcdde20b05661ac3bcda2ec366dbb7dce6acfc4d676e968f483b0ababd32cba2fea6651528599305409c2315d91bae154e3af842f0ed9f5cf90ab56397ef1f21798f6ea097a06ae2d5b6c1978c8a2979c9e31b19da667da0bbdcf90f0958e5e26676dd1a8a00d150515f93a1ba956d8f23d24691be4255e7e80edb0a767f4bc67ec1750f2d57bd0517517d8ffaf54aea1ed3f34372ee509d2f6c77ef022e29edefb49e64db2281cfb4918245e18c427893bf223ff98b8fd69f03de24e936447dc933d6f4fbc78ea45afcddb3b8e9c4c3d3f89c22439c7dbc73cdf057f8eb72ff43fcfdbb11f4cc3e9764c1cdbd484931d6dbbecfd58b47d75610fe8bc2f375704e76f80b7e7eedb73f7477feebefc20bcbd33dede197fdd77467f9338e67ffda85ee57e7a75af0e69e2f8927bc997d085a932fca726bb2e98338bfc91470082bc55f6c9f165fbf47ce3c5435e1cbf98e7ce87f67dea7b80796de0524d00022e4b4020b51db965c9593b67d6efd9da500d087259378a6a8de79c183a5494bfb3c6616108135461ceb4e91574e524b56e8cdc099874337620674de5bce8713134169aec01cd0f21e7ac312cdf6baa9a0cd06edb1511456b183f61d2edcc70ba37d445bc0776dc00e5d1000c1dd115f10f50104f0e70ec0703fc6c49c307f1ae626f7a8870b3602b4040452e0b26ca81888916fe10cf88a693e840c30451eba1a6a2c3d9f07f6cd10d70436b0b9592caba7545f15c38781555f4728fdd461474d6dad82aa2abbec1b9ca439bb85d275b8b84974355deb44338af8d96ca0c55821aa3484e87ba46723ec4cfed9a8992d33967657530975eeb9c708e5be1f664a7b76fe9ee10ed542bbcadef8ae41509bc46f2b51f7af1e94eaeb1f17dc7bcddf3137d5ab5a4411cfbd393e66a51cc4f1af6a9ec1d3dd9ebb4f7821a5c086dff6baa3529e9b1bd32a6713f656bf40573a3e46a7dda1ee0ca7ea91d1b5921c8b156c9f6791e2d11614df356513c6305532d3fd5c32822f45caafa84ad152c974537fc8259f4a3a5fbc14cf6ccd395476d2fabaec13356ee45971a27f7ac53772f5bdbe0bae58635c4d5b553fcb79586168d62c2d84f537b0553d3fdba840d44f7b33d003be5d6fb5e67236894741c6d71ab787721d81f4c57d4d26ad3f96e1b3c679cf658d139a7b971524957cd4ec07a2d0cb1e764ff50ef259cbb3b718b76b92446d68ed08f2c7de28ef47a6d9deccfbc362a9762d9494c94ceb41679dfeca7efcf3120e8fdea776f2fe1d6ccfdc9214e1dd464fef6edf08adf0e7ffb5f000000ffffc35bfd03561d0000`) diff --git a/internal/examples/walk/go.mod b/internal/examples/walk/go.mod index b8da396..028bd65 100644 --- a/internal/examples/walk/go.mod +++ b/internal/examples/walk/go.mod @@ -2,9 +2,6 @@ module github.com/markbates/pkger/internal/examples/walk go 1.12 -require ( - github.com/gobuffalo/envy v1.7.0 // indirect - github.com/markbates/pkger v0.0.0 -) +require github.com/markbates/pkger v0.0.0 replace github.com/markbates/pkger => ../../../ diff --git a/internal/examples/walk/go.sum b/internal/examples/walk/go.sum index fd60a48..e1fc017 100644 --- a/internal/examples/walk/go.sum +++ b/internal/examples/walk/go.sum @@ -1,24 +1,9 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/gobuffalo/envy v1.7.0 h1:GlXgaiBkmrYMHco6t4j7SacKO4XUjvh5pwXh0f4uxXU= -github.com/gobuffalo/envy v1.7.0/go.mod h1:n7DRkBerg/aorDM8kbduw5dN3oXGswK5liaSCx4T5NI= -github.com/gobuffalo/here v0.2.1 h1:YWZUvrHnxNCIY2nnHPnF5Ob99Z5Iq29wHioLgcY+2G0= -github.com/gobuffalo/here v0.2.1/go.mod h1:2a6G14FaAKOGJMK/5UNa4Og/+iyFS5cq3MnlvFR7YDk= -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/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc= -github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= -github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= -github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= -github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/markbates/errx v1.1.0/go.mod h1:PLa46Oex9KNbVDZhKel8v1OT7hD5JZ2eI7AHhA0wswc= 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/rogpeppe/go-internal v1.1.0 h1:g0fH8RicVgNl+zVZDCDfbdWxAWoAEJyI7I3TZYXFiig= -github.com/rogpeppe/go-internal v1.1.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= diff --git a/json.go b/json.go index a22fe0b..1b3a07e 100644 --- a/json.go +++ b/json.go @@ -1,6 +1,10 @@ package pkger import ( + "encoding/json" + "fmt" + "io/ioutil" + "github.com/markbates/pkger/here" ) @@ -10,3 +14,70 @@ type jason struct { Paths *pathsMap `json:"paths"` CurrentInfo here.Info `json:"current_info"` } + +func (f File) MarshalJSON() ([]byte, error) { + m := map[string]interface{}{} + m["info"] = f.info + m["her"] = f.her + m["path"] = f.path + m["data"] = f.data + m["parent"] = f.parent + if !f.info.virtual { + if len(f.data) == 0 && !f.info.IsDir() { + b, err := ioutil.ReadAll(&f) + if err != nil { + return nil, err + } + m["data"] = b + } + } + + return json.Marshal(m) +} + +func (f *File) UnmarshalJSON(b []byte) error { + m := map[string]json.RawMessage{} + if err := json.Unmarshal(b, &m); err != nil { + return err + } + + info, ok := m["info"] + if !ok { + return fmt.Errorf("missing info") + } + + f.info = &FileInfo{} + if err := json.Unmarshal(info, f.info); err != nil { + return err + } + + her, ok := m["her"] + if !ok { + return fmt.Errorf("missing her") + } + if err := json.Unmarshal(her, &f.her); err != nil { + return err + } + + path, ok := m["path"] + if !ok { + return fmt.Errorf("missing path") + } + if err := json.Unmarshal(path, &f.path); err != nil { + return err + } + + parent, ok := m["parent"] + if !ok { + return fmt.Errorf("missing parent") + } + if err := json.Unmarshal(parent, &f.parent); err != nil { + return err + } + + if err := json.Unmarshal(m["data"], &f.data); err != nil { + return err + } + + return nil +} diff --git a/pkger.go b/pkger.go index 6db64da..0fde63a 100644 --- a/pkger.go +++ b/pkger.go @@ -20,12 +20,16 @@ var pathsCache = &pathsMap{} var curOnce = &sync.Once{} var currentInfo here.Info +var packMU = &sync.RWMutex{} + func dubeg(key, format string, args ...interface{}) { s := fmt.Sprintf(format, args...) debug.Debug("[%s|%s] %s", key, s) } func Unpack(ind string) error { + packMU.Lock() + defer packMU.Unlock() b, err := hex.DecodeString(ind) if err != nil { log.Fatal("hex.DecodeString", err) @@ -43,15 +47,25 @@ func Unpack(ind string) error { if err := json.NewDecoder(gz).Decode(&jay); err != nil { return err } - - filesCache = jay.Files - infosCache = jay.Infos - pathsCache = jay.Paths + jay.Files.Range(func(key Path, value *File) bool { + filesCache.Store(key, value) + return true + }) + jay.Infos.Range(func(key string, value here.Info) bool { + infosCache.Store(key, value) + return true + }) + jay.Paths.Range(func(key string, value Path) bool { + pathsCache.Store(key, value) + return true + }) currentInfo = jay.CurrentInfo return nil } func Pack(out io.Writer, paths []Path) error { + packMU.RLock() + defer packMU.RUnlock() bb := &bytes.Buffer{} gz := gzip.NewWriter(bb) defer gz.Close()