matcher.go 635 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package util
  2. import (
  3. "io/ioutil"
  4. "path/filepath"
  5. )
  6. // MatchFiles returns the match values by globbing patterns
  7. func MatchFiles(in string) ([]string, error) {
  8. dir, pattern := filepath.Split(in)
  9. abs, err := filepath.Abs(dir)
  10. if err != nil {
  11. return nil, err
  12. }
  13. files, err := ioutil.ReadDir(abs)
  14. if err != nil {
  15. return nil, err
  16. }
  17. var res []string
  18. for _, file := range files {
  19. if file.IsDir() {
  20. continue
  21. }
  22. name := file.Name()
  23. match, err := filepath.Match(pattern, name)
  24. if err != nil {
  25. return nil, err
  26. }
  27. if !match {
  28. continue
  29. }
  30. res = append(res, filepath.Join(abs, name))
  31. }
  32. return res, nil
  33. }