pkger/pkger.go

70 lines
1.3 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"
"encoding/base64"
"encoding/json"
"io"
"log"
)
2019-07-31 23:29:49 +03:00
// Open opens the named file for reading.
func Open(p string) (*File, error) {
2019-08-02 05:34:32 +03:00
pt, err := Parse(p)
2019-07-31 00:21:26 +03:00
if err != nil {
2019-07-31 23:29:49 +03:00
return nil, err
2019-07-31 00:21:26 +03:00
}
2019-07-31 23:29:49 +03:00
return rootIndex.Open(pt)
2019-07-31 00:21:26 +03:00
}
2019-07-31 23:29:49 +03:00
// Create creates the named file with mode 0666 (before umask), truncating it if it already exists. If successful, methods on the returned File can be used for I/O; the associated file descriptor has mode O_RDWR. If there is an error, it will be of type *PathError.
func Create(p string) (*File, error) {
2019-08-02 05:34:32 +03:00
pt, err := Parse(p)
2019-07-31 00:21:26 +03:00
if err != nil {
return nil, err
}
2019-07-31 23:29:49 +03:00
return rootIndex.Create(pt)
2019-07-31 00:21:26 +03:00
}
2019-08-02 07:22:17 +03:00
func Unpack(ind string) error {
b, err := base64.StdEncoding.DecodeString(ind)
if err != nil {
log.Fatal(err)
return err
}
gz, err := gzip.NewReader(bytes.NewReader(b))
if err != nil {
log.Fatal(err)
return err
}
defer gz.Close()
if err := json.NewDecoder(gz).Decode(rootIndex); err != nil {
log.Fatal(err)
return err
}
return nil
}
func Pack(out io.Writer, paths []Path) error {
for _, p := range paths {
f, err := Open(p.String())
if err != nil {
return err
}
fi, err := f.Stat()
if err != nil {
return err
}
if fi.IsDir() {
continue
}
rootIndex.Files[p] = f
f.Close()
}
return json.NewEncoder(out).Encode(rootIndex)
}