File: imperative_shell.go

package info (click to toggle)
golang-github-smartystreets-goconvey 1.6.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,672 kB
  • sloc: makefile: 8
file content (82 lines) | stat: -rw-r--r-- 1,938 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package watch

import (
	"io"
	"io/ioutil"
	"os"
	"path/filepath"
	"strings"
)

///////////////////////////////////////////////////////////////////////////////

type FileSystemItem struct {
	Root     string
	Path     string
	Name     string
	Size     int64
	Modified int64
	IsFolder bool

	ProfileDisabled  bool
	ProfileTags      []string
	ProfileArguments []string
}

///////////////////////////////////////////////////////////////////////////////

func YieldFileSystemItems(root string, excludedDirs []string) chan *FileSystemItem {
	items := make(chan *FileSystemItem)

	go func() {
		filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
			if err != nil {
				return filepath.SkipDir
			}

			if info.IsDir() && strings.HasPrefix(info.Name(), ".") {
				return filepath.SkipDir
			}

			basePath := filepath.Base(path)
			for _, item := range excludedDirs {
				if item == basePath && info.IsDir() && item != "" && basePath != "" {
					return filepath.SkipDir
				}
			}

			items <- &FileSystemItem{
				Root:     root,
				Path:     path,
				Name:     info.Name(),
				Size:     info.Size(),
				Modified: info.ModTime().Unix(),
				IsFolder: info.IsDir(),
			}

			return nil
		})
		close(items)
	}()

	return items
}

///////////////////////////////////////////////////////////////////////////////

// ReadContents reads files wholesale. This function is only called on files
// that end in '.goconvey'. These files should be very small, probably not
// ever more than a few hundred bytes. The ignored errors are ok because in
// the event of an IO error all that need be returned is an empty string.
func ReadContents(path string) string {
	file, err := os.Open(path)
	if err != nil {
		return ""
	}
	defer file.Close()
	reader := io.LimitReader(file, 1024*4)
	content, _ := ioutil.ReadAll(reader)
	return string(content)
}

///////////////////////////////////////////////////////////////////////////////