imperative_shell.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package watch
  2. import (
  3. "io"
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. )
  9. ///////////////////////////////////////////////////////////////////////////////
  10. type FileSystemItem struct {
  11. Root string
  12. Path string
  13. Name string
  14. Size int64
  15. Modified int64
  16. IsFolder bool
  17. ProfileDisabled bool
  18. ProfileTags []string
  19. ProfileArguments []string
  20. }
  21. ///////////////////////////////////////////////////////////////////////////////
  22. func YieldFileSystemItems(root string, excludedDirs []string) chan *FileSystemItem {
  23. items := make(chan *FileSystemItem)
  24. go func() {
  25. filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
  26. if err != nil {
  27. return filepath.SkipDir
  28. }
  29. if info.IsDir() && strings.HasPrefix(info.Name(), ".") {
  30. return filepath.SkipDir
  31. }
  32. basePath := filepath.Base(path)
  33. for _, item := range excludedDirs {
  34. if item == basePath && info.IsDir() && item != "" && basePath != "" {
  35. return filepath.SkipDir
  36. }
  37. }
  38. items <- &FileSystemItem{
  39. Root: root,
  40. Path: path,
  41. Name: info.Name(),
  42. Size: info.Size(),
  43. Modified: info.ModTime().Unix(),
  44. IsFolder: info.IsDir(),
  45. }
  46. return nil
  47. })
  48. close(items)
  49. }()
  50. return items
  51. }
  52. ///////////////////////////////////////////////////////////////////////////////
  53. // ReadContents reads files wholesale. This function is only called on files
  54. // that end in '.goconvey'. These files should be very small, probably not
  55. // ever more than a few hundred bytes. The ignored errors are ok because in
  56. // the event of an IO error all that need be returned is an empty string.
  57. func ReadContents(path string) string {
  58. file, err := os.Open(path)
  59. if err != nil {
  60. return ""
  61. }
  62. defer file.Close()
  63. reader := io.LimitReader(file, 1024*4)
  64. content, _ := ioutil.ReadAll(reader)
  65. return string(content)
  66. }
  67. ///////////////////////////////////////////////////////////////////////////////