feat: -exclude flag pack command

This commit is contained in:
Jan Kaßel 2020-07-09 17:28:31 +02:00
parent 992ea93a2a
commit f3870921de
5 changed files with 41 additions and 9 deletions

View File

@ -44,7 +44,7 @@ func (e *listCmd) Exec(args []string) error {
fp := filepath.Join(info.Dir, outName) fp := filepath.Join(info.Dir, outName)
os.RemoveAll(fp) os.RemoveAll(fp)
decls, err := parser.Parse(info, e.include...) decls, err := parser.Parse(info, e.include, nil)
if err != nil { if err != nil {
return err return err
} }

View File

@ -32,6 +32,7 @@ type packCmd struct {
out string out string
help bool help bool
include slice include slice
exclude slice
subs []command subs []command
} }
@ -48,7 +49,7 @@ func (e *packCmd) Exec(args []string) error {
fp := filepath.Join(info.Dir, e.out, outName) fp := filepath.Join(info.Dir, e.out, outName)
os.RemoveAll(fp) os.RemoveAll(fp)
decls, err := parser.Parse(info, e.include...) decls, err := parser.Parse(info, e.include, e.exclude)
if err != nil { if err != nil {
return err return err
} }
@ -105,6 +106,7 @@ func New() (*packCmd, error) {
c.BoolVar(&c.help, "h", false, "prints help information") c.BoolVar(&c.help, "h", false, "prints help information")
c.StringVar(&c.out, "o", "", "output directory for pkged.go") c.StringVar(&c.out, "o", "", "output directory for pkged.go")
c.Var(&c.include, "include", "packages the specified file or directory") 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() { c.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage:\n\n") fmt.Fprintf(os.Stderr, "Usage:\n\n")
Usage(os.Stderr, c.FlagSet)() Usage(os.Stderr, c.FlagSet)()

View File

@ -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 { if err != nil {
return err return err
} }

View File

@ -28,15 +28,17 @@ type Parser struct {
decls map[string]Decls decls map[string]Decls
once sync.Once once sync.Once
includes []string includes []string
excludes []string
err error 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) p, err := New(her)
if err != nil { if err != nil {
return nil, err return nil, err
} }
p.includes = includes p.includes = includes
p.excludes = excludes
return p.Decls() return p.Decls()
} }
@ -200,7 +202,7 @@ func (p *Parser) parse() error {
} }
base := filepath.Base(path) base := filepath.Base(path)
for _, x := range defaultIgnoredFolders { for _, x := range append(defaultIgnoredFolders, p.excludes...) {
if strings.HasPrefix(base, x) { if strings.HasPrefix(base, x) {
return filepath.SkipDir return filepath.SkipDir
} }

View File

@ -31,7 +31,7 @@ func Test_Parser_Ref(t *testing.T) {
_, err = pkgtest.LoadFiles("/", ref, disk) _, err = pkgtest.LoadFiles("/", ref, disk)
r.NoError(err) r.NoError(err)
res, err := Parse(ref.Info) res, err := Parse(ref.Info, nil, nil)
r.NoError(err) r.NoError(err)
@ -60,7 +60,7 @@ func Test_Parser_Ref_Include(t *testing.T) {
_, err = pkgtest.LoadFiles("/", ref, disk) _, err = pkgtest.LoadFiles("/", ref, disk)
r.NoError(err) 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) r.NoError(err)
@ -71,6 +71,34 @@ func Test_Parser_Ref_Include(t *testing.T) {
r.Equal(26, l) 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) { func Test_Parser_dotGo_Directory(t *testing.T) {
r := require.New(t) r := require.New(t)
@ -87,7 +115,7 @@ func Test_Parser_dotGo_Directory(t *testing.T) {
_, err = pkgtest.LoadFiles("/", ref, disk) _, err = pkgtest.LoadFiles("/", ref, disk)
r.NoError(err) r.NoError(err)
res, err := Parse(ref.Info) res, err := Parse(ref.Info, nil, nil)
r.NoError(err) r.NoError(err)
r.Equal(11, len(res)) r.Equal(11, len(res))
} }
@ -108,7 +136,7 @@ func Test_Parser_Example_HTTP(t *testing.T) {
her, err := here.Dir(root) her, err := here.Dir(root)
r.NoError(err) r.NoError(err)
res, err := Parse(her) res, err := Parse(her, nil, nil)
r.NoError(err) r.NoError(err)
files, err := res.Files() files, err := res.Files()