feat: support wildcard matching on ignore

See decentral1se/distribusi-go#3.
This commit is contained in:
decentral1se 2022-02-04 11:48:39 +01:00
parent 6923876c27
commit 7ef67b2130
No known key found for this signature in database
GPG Key ID: 03789458B3D0C410

View File

@ -104,6 +104,8 @@ with relevant html classes and tags for easy styling.
Example:
distribusi -p <path> -s
See https://pkg.go.dev/path/filepath#Match for supported patterns for "-i/--ignore".
`,
Flags: []cli.Flag{
&cli.StringFlag{
@ -127,7 +129,7 @@ Example:
&cli.StringFlag{
Name: "ignore",
Aliases: []string{"i"},
Usage: "ignore specific paths (e.g. \"mydir, myfile.txt\" )",
Usage: "ignore specific paths, (e.g. \"*.gif, *.txt, mydir\" )",
Value: "",
},
&cli.StringFlag{
@ -228,7 +230,11 @@ func distribusify(c *cli.Context, root string, ignore []string) error {
if err := filepath.Walk(root, func(fpath string, finfo os.FileInfo, err error) error {
if skip := shouldSkip(c, fpath, ignore); skip {
skip, err := shouldSkip(c, fpath, ignore)
if err != nil {
return err
}
if skip {
return nil
}
@ -283,7 +289,11 @@ func distribusify(c *cli.Context, root string, ignore []string) error {
for _, dir := range dirs {
fname := filepath.Base(dir)
if skip := shouldSkip(c, dir, ignore); skip {
skip, err := shouldSkip(c, fname, ignore)
if err != nil {
return err
}
if skip {
continue
}
@ -301,7 +311,11 @@ func distribusify(c *cli.Context, root string, ignore []string) error {
for _, file := range files {
fname := filepath.Base(file)
if skip := shouldSkip(c, file, ignore); skip {
skip, err := shouldSkip(c, fname, ignore)
if err != nil {
return err
}
if skip {
continue
}
@ -629,19 +643,27 @@ func wipeGeneratedFiles(dir string) error {
}
// shouldSkip checks if paths should be skipped over.
func shouldSkip(c *cli.Context, fpath string, ignore []string) bool {
if sliceContains(ignore, filepath.Base(fpath)) {
return true
func shouldSkip(c *cli.Context, fpath string, ignore []string) (bool, error) {
for _, pattern := range ignore {
match, err := filepath.Match(pattern, filepath.Base(fpath))
if err != nil {
return false, err
}
if match {
logrus.Debugf("skipping %s, matched %s", filepath.Base(fpath), pattern)
return true, nil
}
}
fpaths := strings.Split(fpath, "/")
for _, part := range fpaths {
if strings.HasPrefix(part, ".") {
if !c.Bool("hidden") {
return true
logrus.Debugf("skipping %s, hidden directory", fpath)
return true, nil
}
}
}
return false
return false, nil
}