util.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. package nginx_log
  2. import (
  3. "os"
  4. "path/filepath"
  5. )
  6. // ExpandLogGroupPath finds all physical files belonging to a log group using filesystem globbing.
  7. func ExpandLogGroupPath(basePath string) ([]string, error) {
  8. // Find all files belonging to this log group by globbing
  9. globPath := basePath + "*"
  10. matches, err := filepath.Glob(globPath)
  11. if err != nil {
  12. return nil, err
  13. }
  14. // filepath.Glob might not match the base file itself if it has no extension,
  15. // so we check for it explicitly and add it to the list.
  16. info, err := os.Stat(basePath)
  17. if err == nil && info.Mode().IsRegular() {
  18. matches = append(matches, basePath)
  19. }
  20. // Deduplicate file list
  21. seen := make(map[string]struct{})
  22. uniqueFiles := make([]string, 0)
  23. for _, match := range matches {
  24. if _, ok := seen[match]; !ok {
  25. // Further check if it's a file, not a directory. Glob can match dirs.
  26. info, err := os.Stat(match)
  27. if err == nil && info.Mode().IsRegular() {
  28. seen[match] = struct{}{}
  29. uniqueFiles = append(uniqueFiles, match)
  30. }
  31. }
  32. }
  33. return uniqueFiles, nil
  34. }