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)
| 139 | // |
| 140 | // ListFiles does not recurse into subdirectories; use [FindFiles] instead. |
| 141 | func 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). |
searching dependent graphs…