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

Add exclude flag to list and pack commands #112

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ package <reader>
// Pkger stuff here
```

### Including Files at Package Time
### Including and Excluding Files at Package Time

There may be reasons where you don't reference a particular file, or folder, that you want embedded in your application, such as a build artifact.

To do this you may use either the [`github.com/markbates/pkger#Include`](https://godoc.org/github.com/markbates/pkger#Include) function to set a no-op parser directive to include the specified path.

Alternatively, you may use the `-include` flag with the `pkger` and `pkger list` commands.
Alternatively, you may use the `-include` flag with the `pkger` and `pkger list` commands. To exclude files and directories when parsing, you may additionally use the `-exclude` flag. Directories such as `node_modules` are excluded by default.

```bash
$ pkger list -include /actions -include github.com/gobuffalo/buffalo:/app.go
Expand Down
4 changes: 3 additions & 1 deletion cmd/pkger/cmds/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type listCmd struct {
help bool
json bool
include slice
exclude slice
subs []command
}

Expand All @@ -44,7 +45,7 @@ func (e *listCmd) Exec(args []string) error {
fp := filepath.Join(info.Dir, outName)
os.RemoveAll(fp)

decls, err := parser.Parse(info, e.include...)
decls, err := parser.Parse(info, e.include, e.exclude)
if err != nil {
return err
}
Expand Down Expand Up @@ -95,6 +96,7 @@ func (e *listCmd) Flags() *flag.FlagSet {
e.FlagSet = flag.NewFlagSet("list", flag.ExitOnError)
e.BoolVar(&e.json, "json", false, "prints in JSON format")
e.Var(&e.include, "include", "packages the specified file or directory")
e.Var(&e.exclude, "exclude", "exclude files or directories from parsing")
}
e.Usage = Usage(os.Stderr, e.FlagSet)
return e.FlagSet
Expand Down
4 changes: 3 additions & 1 deletion cmd/pkger/cmds/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type packCmd struct {
out string
help bool
include slice
exclude slice
subs []command
}

Expand All @@ -48,7 +49,7 @@ func (e *packCmd) Exec(args []string) error {
fp := filepath.Join(info.Dir, e.out, outName)
os.RemoveAll(fp)

decls, err := parser.Parse(info, e.include...)
decls, err := parser.Parse(info, e.include, e.exclude)
if err != nil {
return err
}
Expand Down Expand Up @@ -105,6 +106,7 @@ func New() (*packCmd, error) {
c.BoolVar(&c.help, "h", false, "prints help information")
c.StringVar(&c.out, "o", "", "output directory for pkged.go")
c.Var(&c.include, "include", "packages the specified file or directory")
c.Var(&c.exclude, "exclude", "exclude files or directories from parsing")
c.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage:\n\n")
Usage(os.Stderr, c.FlagSet)()
Expand Down
2 changes: 1 addition & 1 deletion cmd/pkger/cmds/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (c *parseCmd) Exec(args []string) error {
}

}
decls, err := parser.Parse(info)
decls, err := parser.Parse(info, nil, nil)
if err != nil {
return err
}
Expand Down
6 changes: 4 additions & 2 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,17 @@ type Parser struct {
decls map[string]Decls
once sync.Once
includes []string
excludes []string
err error
}

func Parse(her here.Info, includes ...string) (Decls, error) {
func Parse(her here.Info, includes []string, excludes []string) (Decls, error) {
p, err := New(her)
if err != nil {
return nil, err
}
p.includes = includes
p.excludes = excludes

return p.Decls()
}
Expand Down Expand Up @@ -200,7 +202,7 @@ func (p *Parser) parse() error {
}

base := filepath.Base(path)
for _, x := range defaultIgnoredFolders {
for _, x := range append(defaultIgnoredFolders, p.excludes...) {
if strings.HasPrefix(base, x) {
return filepath.SkipDir
}
Expand Down
36 changes: 32 additions & 4 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func Test_Parser_Ref(t *testing.T) {
_, err = pkgtest.LoadFiles("/", ref, disk)
r.NoError(err)

res, err := Parse(ref.Info)
res, err := Parse(ref.Info, nil, nil)

r.NoError(err)

Expand Down Expand Up @@ -60,7 +60,7 @@ func Test_Parser_Ref_Include(t *testing.T) {
_, err = pkgtest.LoadFiles("/", ref, disk)
r.NoError(err)

res, err := Parse(ref.Info, "github.com/stretchr/testify:/go.mod")
res, err := Parse(ref.Info, []string{"github.com/stretchr/testify:/go.mod"}, nil)

r.NoError(err)

Expand All @@ -71,6 +71,34 @@ func Test_Parser_Ref_Include(t *testing.T) {
r.Equal(26, l)
}

func Test_Parser_Ref_Exclude(t *testing.T) {
defer func() {
c := exec.Command("go", "mod", "tidy", "-v")
c.Run()
}()
r := require.New(t)

ref, err := pkgtest.NewRef()
r.NoError(err)
defer os.RemoveAll(ref.Dir)

disk, err := stdos.New(ref.Info)
r.NoError(err)

_, err = pkgtest.LoadFiles("/", ref, disk)
r.NoError(err)

res, err := Parse(ref.Info, nil, []string{"models"})

r.NoError(err)

files, err := res.Files()
r.NoError(err)

l := len(files)
r.Equal(24, l)
}

func Test_Parser_dotGo_Directory(t *testing.T) {
r := require.New(t)

Expand All @@ -87,7 +115,7 @@ func Test_Parser_dotGo_Directory(t *testing.T) {
_, err = pkgtest.LoadFiles("/", ref, disk)
r.NoError(err)

res, err := Parse(ref.Info)
res, err := Parse(ref.Info, nil, nil)
r.NoError(err)
r.Equal(11, len(res))
}
Expand All @@ -108,7 +136,7 @@ func Test_Parser_Example_HTTP(t *testing.T) {
her, err := here.Dir(root)
r.NoError(err)

res, err := Parse(her)
res, err := Parse(her, nil, nil)
r.NoError(err)

files, err := res.Files()
Expand Down
2 changes: 1 addition & 1 deletion pkging/pkgutil/stuff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func Test_Stuff(t *testing.T) {
r.NoError(err)
r.Len(infos, 34)

decls, err := parser.Parse(ref.Info)
decls, err := parser.Parse(ref.Info, nil, nil)
r.NoError(err)

r.Len(decls, 11)
Expand Down