pkger/pkger.go

118 lines
2.0 KiB
Go
Raw Normal View History

2019-07-31 00:21:26 +03:00
package pkger
2019-08-02 07:22:17 +03:00
import (
"bytes"
"compress/gzip"
2019-08-03 01:14:48 +03:00
"encoding/hex"
2019-08-09 22:30:12 +03:00
"encoding/json"
2019-08-03 01:14:48 +03:00
"fmt"
2019-08-02 07:22:17 +03:00
"io"
2019-08-19 16:56:20 +03:00
"io/ioutil"
2019-08-02 07:22:17 +03:00
"log"
2019-08-09 22:43:58 +03:00
"sync"
"github.com/markbates/pkger/here"
2019-08-02 07:22:17 +03:00
)
2019-08-09 22:43:58 +03:00
var filesCache = &filesMap{}
var infosCache = &infosMap{}
var pathsCache = &pathsMap{}
var curOnce = &sync.Once{}
var currentInfo here.Info
2019-08-18 19:54:59 +03:00
var packed bool
2019-08-10 00:15:14 +03:00
var packMU = &sync.RWMutex{}
2019-08-19 16:56:20 +03:00
func ReadFile(s string) ([]byte, error) {
f, err := Open(s)
if err != nil {
return nil, err
}
defer f.Close()
return ioutil.ReadAll(f)
}
2019-08-02 07:22:17 +03:00
func Unpack(ind string) error {
2019-08-18 19:54:59 +03:00
packed = true
2019-08-10 00:15:14 +03:00
packMU.Lock()
defer packMU.Unlock()
2019-08-03 01:14:48 +03:00
b, err := hex.DecodeString(ind)
2019-08-02 07:22:17 +03:00
if err != nil {
2019-08-03 01:14:48 +03:00
log.Fatal("hex.DecodeString", err)
2019-08-02 07:22:17 +03:00
return err
}
gz, err := gzip.NewReader(bytes.NewReader(b))
if err != nil {
2019-08-03 01:14:48 +03:00
log.Fatal("gzip.NewReader", err)
2019-08-02 07:22:17 +03:00
return err
}
defer gz.Close()
2019-08-09 22:30:12 +03:00
var jay jason
if err := json.NewDecoder(gz).Decode(&jay); err != nil {
return err
}
2019-08-10 00:15:14 +03:00
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
})
2019-08-09 22:30:12 +03:00
currentInfo = jay.CurrentInfo
2019-08-02 07:22:17 +03:00
return nil
}
func Pack(out io.Writer, paths []Path) error {
2019-08-10 00:15:14 +03:00
packMU.RLock()
defer packMU.RUnlock()
2019-08-03 01:14:48 +03:00
bb := &bytes.Buffer{}
gz := gzip.NewWriter(bb)
defer gz.Close()
2019-08-02 07:22:17 +03:00
for _, p := range paths {
f, err := Open(p.String())
if err != nil {
return err
}
2019-08-05 00:13:27 +03:00
fi, err := f.Stat()
if err != nil {
return err
}
if fi.IsDir() {
2019-08-09 05:35:01 +03:00
filesCache.Store(p, f)
2019-08-05 00:13:27 +03:00
f.Close()
continue
}
2019-08-09 05:35:01 +03:00
filesCache.Store(p, f)
2019-08-02 07:22:17 +03:00
f.Close()
2019-08-05 00:13:27 +03:00
2019-08-02 07:22:17 +03:00
}
2019-08-03 01:14:48 +03:00
2019-08-09 22:30:12 +03:00
jay := jason{
Files: filesCache,
Infos: infosCache,
Paths: pathsCache,
CurrentInfo: currentInfo,
}
if err := json.NewEncoder(gz).Encode(jay); err != nil {
return err
}
2019-08-03 01:14:48 +03:00
if err := gz.Close(); err != nil {
return err
}
s := hex.EncodeToString(bb.Bytes())
2019-08-05 00:13:27 +03:00
_, err := fmt.Fprint(out, s)
return err
2019-08-02 07:22:17 +03:00
}