Skip to content
This repository has been archived by the owner on Aug 22, 2022. It is now read-only.

Commit

Permalink
Merge branch 'master' of https://github.com/markbates/pkger
Browse files Browse the repository at this point in the history
  • Loading branch information
markbates committed Oct 23, 2019
2 parents 5365ef9 + 0cc509f commit 545b970
Show file tree
Hide file tree
Showing 21 changed files with 1,568 additions and 53 deletions.
13 changes: 4 additions & 9 deletions cmd/pkger/cmds/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,11 @@ func (e *listCmd) Exec(args []string) error {
ImportPath: info.ImportPath,
}

for _, decl := range decls {
if fl, ok := decl.(parser.Filer); ok {
files, err := fl.Files()
if err != nil {
return err
}
jay.Files = append(jay.Files, files...)
}

files, err := decls.Files()
if err != nil {
return err
}
jay.Files = files

if e.json {
bb := &bytes.Buffer{}
Expand Down
7 changes: 7 additions & 0 deletions examples/dynamic/pkger/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM alpine

EXPOSE 3000
COPY example /bin/


CMD /bin/example
5 changes: 5 additions & 0 deletions examples/dynamic/pkger/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
default:
pkger
GOOS=linux go build -v -o example
docker build -t pkger:example .
docker run -p 3000:3000 pkger:example
5 changes: 5 additions & 0 deletions examples/dynamic/pkger/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module app

go 1.13

require github.com/markbates/pkger v0.5.1
22 changes: 22 additions & 0 deletions examples/dynamic/pkger/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
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 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/markbates/pkger v0.5.1 h1:l0s55z4X5XbwIat2LyLe0HABMBswVK1GJbzLXQbuAXs=
github.com/markbates/pkger v0.5.1/go.mod h1:so/QD8FeTM0IilC3nRArkwOvUT+tsJsaXLFUAKmjzJk=
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/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
43 changes: 43 additions & 0 deletions examples/dynamic/pkger/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

import (
"fmt"
"io"
"log"
"os"

"github.com/markbates/pkger"
)

func main() {
if err := run(); err != nil {
log.Fatal(err)
}
}

func run() error {
info, err := pkger.Stat("/go.mod")
if err != nil {
return err
}
fmt.Println(info)
if err := pkger.MkdirAll("/foo/bar/baz", 0755); err != nil {
return err
}

f, err := pkger.Create("/foo/bar/baz/biz.txt")
if err != nil {
return err
}
f.Write([]byte("BIZ!!"))
if err := f.Close(); err != nil {
return err
}

f, err = pkger.Open("/foo/bar/baz/biz.txt")
if err != nil {
return err
}
io.Copy(os.Stdout, f)
return f.Close()
}
Binary file added examples/dynamic/pkger/public/images/img1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/dynamic/pkger/public/images/img2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions examples/dynamic/pkger/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width" />
<title>HI</title>
</head>
<body>
HELLO
<img src="/images/img1.png" alt=""/>
</body>
</html>
54 changes: 54 additions & 0 deletions parser/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package parser

import (
"encoding/json"
"go/token"
"os"
)

var _ Decl = CreateDecl{}

type CreateDecl struct {
file *File
pos token.Pos
value string
}

func (d CreateDecl) String() string {
b, _ := json.Marshal(d)
return string(b)
}

func (d CreateDecl) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"type": "pkger.Create",
"file": d.file,
"pos": d.pos,
"value": d.value,
})
}

func (d CreateDecl) File() (*File, error) {
if d.file == nil {
return nil, os.ErrNotExist
}
return d.file, nil
}

func (d CreateDecl) Pos() (token.Pos, error) {
if d.pos <= 0 {
return -1, os.ErrNotExist
}
return d.pos, nil
}

func (d CreateDecl) Value() (string, error) {
if d.value == "" {
return "", os.ErrNotExist
}
return d.value, nil
}

func (d CreateDecl) VirtualPaths() []string {
return []string{d.value}
}
15 changes: 13 additions & 2 deletions parser/decl.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,32 @@ type Decl interface {
}

type Filer interface {
Files() ([]*File, error)
Files(map[string]string) ([]*File, error)
}

type Virtualer interface {
VirtualPaths() []string
}

type Decls []Decl

func (decls Decls) Files() ([]*File, error) {
m := map[string]*File{}
v := map[string]string{}

for _, d := range decls {
if vt, ok := d.(Virtualer); ok {
for _, s := range vt.VirtualPaths() {
v[s] = s
}
}

fl, ok := d.(Filer)
if !ok {
continue
}

files, err := fl.Files()
files, err := fl.Files(v)
if err != nil {
return nil, err
}
Expand Down
50 changes: 11 additions & 39 deletions parser/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ import (
"encoding/json"
"go/token"
"os"
"path/filepath"

"github.com/markbates/pkger"
"github.com/markbates/pkger/here"
)

var _ Decl = HTTPDecl{}
Expand All @@ -18,6 +14,11 @@ type HTTPDecl struct {
value string
}

func (d HTTPDecl) String() string {
b, _ := json.Marshal(d)
return string(b)
}

func (d HTTPDecl) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"type": "pkger.HTTP",
Expand Down Expand Up @@ -48,40 +49,11 @@ func (d HTTPDecl) Value() (string, error) {
return d.value, nil
}

func (d HTTPDecl) Files() ([]*File, error) {

pt, err := pkger.Parse(d.value)
if err != nil {
return nil, err
}

her, err := here.Package(pt.Pkg)
if err != nil {
return nil, err
}

fp := filepath.Join(her.Dir, pt.Name)

osf, err := os.Stat(fp)
if err != nil {
return nil, err
}

if osf.IsDir() {
wd := WalkDecl{
file: d.file,
pos: d.pos,
value: d.value,
}
return wd.Files()
func (d HTTPDecl) Files(virtual map[string]string) ([]*File, error) {
od := OpenDecl{
file: d.file,
pos: d.pos,
value: d.value,
}

var files []*File
files = append(files, &File{
Abs: filepath.Join(her.Dir, pt.Name),
Path: pt,
Here: her,
})

return files, nil
return od.Files(virtual)
}
54 changes: 54 additions & 0 deletions parser/mkdir.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package parser

import (
"encoding/json"
"go/token"
"os"
)

var _ Decl = MkdirAllDecl{}

type MkdirAllDecl struct {
file *File
pos token.Pos
value string
}

func (d MkdirAllDecl) String() string {
b, _ := json.Marshal(d)
return string(b)
}

func (d MkdirAllDecl) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"type": "pkger.MkdirAll",
"file": d.file,
"pos": d.pos,
"value": d.value,
})
}

func (d MkdirAllDecl) File() (*File, error) {
if d.file == nil {
return nil, os.ErrNotExist
}
return d.file, nil
}

func (d MkdirAllDecl) Pos() (token.Pos, error) {
if d.pos <= 0 {
return -1, os.ErrNotExist
}
return d.pos, nil
}

func (d MkdirAllDecl) Value() (string, error) {
if d.value == "" {
return "", os.ErrNotExist
}
return d.value, nil
}

func (d MkdirAllDecl) VirtualPaths() []string {
return []string{d.value}
}
12 changes: 10 additions & 2 deletions parser/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ type OpenDecl struct {
value string
}

func (d OpenDecl) String() string {
b, _ := json.Marshal(d)
return string(b)
}

func (d OpenDecl) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"type": "pkger.Open",
Expand Down Expand Up @@ -48,7 +53,10 @@ func (d OpenDecl) Value() (string, error) {
return d.value, nil
}

func (d OpenDecl) Files() ([]*File, error) {
func (d OpenDecl) Files(virtual map[string]string) ([]*File, error) {
if _, ok := virtual[d.value]; ok {
return nil, nil
}

pt, err := pkger.Parse(d.value)
if err != nil {
Expand All @@ -73,7 +81,7 @@ func (d OpenDecl) Files() ([]*File, error) {
pos: d.pos,
value: d.value,
}
return wd.Files()
return wd.Files(virtual)
}

var files []*File
Expand Down
Loading

0 comments on commit 545b970

Please sign in to comment.