From f3870921de210862673b62a9a50747a8fd43db66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Ka=C3=9Fel?= Date: Thu, 9 Jul 2020 17:28:31 +0200 Subject: [PATCH] feat: -exclude flag pack command --- cmd/pkger/cmds/list.go | 2 +- cmd/pkger/cmds/pack.go | 4 +++- cmd/pkger/cmds/parse.go | 2 +- parser/parser.go | 6 ++++-- parser/parser_test.go | 36 ++++++++++++++++++++++++++++++++---- 5 files changed, 41 insertions(+), 9 deletions(-) diff --git a/cmd/pkger/cmds/list.go b/cmd/pkger/cmds/list.go index 0bd844f..5238047 100644 --- a/cmd/pkger/cmds/list.go +++ b/cmd/pkger/cmds/list.go @@ -44,7 +44,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, nil) if err != nil { return err } diff --git a/cmd/pkger/cmds/pack.go b/cmd/pkger/cmds/pack.go index d91e4cf..7fc4ebc 100644 --- a/cmd/pkger/cmds/pack.go +++ b/cmd/pkger/cmds/pack.go @@ -32,6 +32,7 @@ type packCmd struct { out string help bool include slice + exclude slice subs []command } @@ -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 } @@ -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)() diff --git a/cmd/pkger/cmds/parse.go b/cmd/pkger/cmds/parse.go index ead6334..7b5aff7 100644 --- a/cmd/pkger/cmds/parse.go +++ b/cmd/pkger/cmds/parse.go @@ -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 } diff --git a/parser/parser.go b/parser/parser.go index 72fd023..ab5f0d6 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -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() } @@ -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 } diff --git a/parser/parser_test.go b/parser/parser_test.go index baaeaa1..42aa630 100644 --- a/parser/parser_test.go +++ b/parser/parser_test.go @@ -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) @@ -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) @@ -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) @@ -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)) } @@ -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()