MCPcopy
hub / github.com/bitfield/script / ListFiles

Function ListFiles

script.go:141–166  ·  view source on GitHub ↗

ListFiles creates a pipe containing the files or directories specified by path, one per line. path can be a glob expression, as for [filepath.Match]. For example: ListFiles("/data/*").Stdout() ListFiles does not recurse into subdirectories; use [FindFiles] instead.

(path string)

Source from the content-addressed store, hash-verified

139//
140// ListFiles does not recurse into subdirectories; use [FindFiles] instead.
141func ListFiles(path string) *Pipe {
142 if strings.ContainsAny(path, "[]^*?\\{}!") {
143 fileNames, err := filepath.Glob(path)
144 if err != nil {
145 return NewPipe().WithError(err)
146 }
147 return Slice(fileNames)
148 }
149 entries, err := os.ReadDir(path)
150 if err != nil {
151 // Check for the case where the path matches exactly one file
152 s, err := os.Stat(path)
153 if err != nil {
154 return NewPipe().WithError(err)
155 }
156 if !s.IsDir() {
157 return Echo(path)
158 }
159 return NewPipe().WithError(err)
160 }
161 matches := make([]string, len(entries))
162 for i, e := range entries {
163 matches[i] = filepath.Join(path, e.Name())
164 }
165 return Slice(matches)
166}
167
168// NewPipe creates a new pipe with an empty reader (use [Pipe.WithReader] to
169// attach another reader to it).

Calls 5

NewPipeFunction · 0.85
SliceFunction · 0.85
EchoFunction · 0.85
WithErrorMethod · 0.80
JoinMethod · 0.80

Used in the wild real call sites across dependent graphs

searching dependent graphs…