|
| 1 | +/* |
| 2 | + * This file is part of PathsHelper library. |
| 3 | + * |
| 4 | + * Copyright 2018-2022 Arduino AG (http://www.arduino.cc/) |
| 5 | + * |
| 6 | + * PathsHelper library is free software; you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation; either version 2 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License |
| 17 | + * along with this program; if not, write to the Free Software |
| 18 | + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | + * |
| 20 | + * As a special exception, you may use this file as part of a free software |
| 21 | + * library without restriction. Specifically, if other files instantiate |
| 22 | + * templates or use macros or inline functions from this file, or you compile |
| 23 | + * this file and link it with other files to produce an executable, this |
| 24 | + * file does not by itself cause the resulting executable to be covered by |
| 25 | + * the GNU General Public License. This exception does not however |
| 26 | + * invalidate any other reasons why the executable file might be covered by |
| 27 | + * the GNU General Public License. |
| 28 | + */ |
| 29 | + |
| 30 | +package paths |
| 31 | + |
| 32 | +import ( |
| 33 | + "io/ioutil" |
| 34 | + "strings" |
| 35 | +) |
| 36 | + |
| 37 | +// ReadDirFilter is a filter for Path.ReadDir and Path.ReadDirRecursive methods. |
| 38 | +// The filter should return true to accept a file or false to reject it. |
| 39 | +type ReadDirFilter func(file *Path) bool |
| 40 | + |
| 41 | +// ReadDir returns a PathList containing the content of the directory |
| 42 | +// pointed by the current Path. The resulting list is filtered by the given filters chained. |
| 43 | +func (p *Path) ReadDir(filters ...ReadDirFilter) (PathList, error) { |
| 44 | + infos, err := ioutil.ReadDir(p.path) |
| 45 | + if err != nil { |
| 46 | + return nil, err |
| 47 | + } |
| 48 | + |
| 49 | + accept := func(p *Path) bool { |
| 50 | + for _, filter := range filters { |
| 51 | + if !filter(p) { |
| 52 | + return false |
| 53 | + } |
| 54 | + } |
| 55 | + return true |
| 56 | + } |
| 57 | + |
| 58 | + paths := PathList{} |
| 59 | + for _, info := range infos { |
| 60 | + path := p.Join(info.Name()) |
| 61 | + if !accept(path) { |
| 62 | + continue |
| 63 | + } |
| 64 | + paths.Add(path) |
| 65 | + } |
| 66 | + return paths, nil |
| 67 | +} |
| 68 | + |
| 69 | +// ReadDirRecursive returns a PathList containing the content of the directory |
| 70 | +// and its subdirectories pointed by the current Path |
| 71 | +func (p *Path) ReadDirRecursive() (PathList, error) { |
| 72 | + infos, err := ioutil.ReadDir(p.path) |
| 73 | + if err != nil { |
| 74 | + return nil, err |
| 75 | + } |
| 76 | + paths := PathList{} |
| 77 | + for _, info := range infos { |
| 78 | + path := p.Join(info.Name()) |
| 79 | + paths.Add(path) |
| 80 | + |
| 81 | + if isDir, err := path.IsDirCheck(); err != nil { |
| 82 | + return nil, err |
| 83 | + } else if isDir { |
| 84 | + subPaths, err := path.ReadDirRecursive() |
| 85 | + if err != nil { |
| 86 | + return nil, err |
| 87 | + } |
| 88 | + paths.AddAll(subPaths) |
| 89 | + } |
| 90 | + |
| 91 | + } |
| 92 | + return paths, nil |
| 93 | +} |
| 94 | + |
| 95 | +// ReadDirRecursiveFiltered returns a PathList containing the content of the directory |
| 96 | +// and its subdirectories pointed by the current Path, filtered by the given skipFilter |
| 97 | +// and filters: |
| 98 | +// - `recursionFilter` is a filter that is checked to determine if the subdirectory must |
| 99 | +// by visited recursively (if the filter rejects the entry, the entry is not visited |
| 100 | +// but can still be added to the result) |
| 101 | +// - `filters` are the filters that are checked to determine if the entry should be |
| 102 | +// added to the resulting PathList |
| 103 | +func (p *Path) ReadDirRecursiveFiltered(recursionFilter ReadDirFilter, filters ...ReadDirFilter) (PathList, error) { |
| 104 | + infos, err := ioutil.ReadDir(p.path) |
| 105 | + if err != nil { |
| 106 | + return nil, err |
| 107 | + } |
| 108 | + |
| 109 | + accept := func(p *Path) bool { |
| 110 | + for _, filter := range filters { |
| 111 | + if !filter(p) { |
| 112 | + return false |
| 113 | + } |
| 114 | + } |
| 115 | + return true |
| 116 | + } |
| 117 | + |
| 118 | + paths := PathList{} |
| 119 | + for _, info := range infos { |
| 120 | + path := p.Join(info.Name()) |
| 121 | + |
| 122 | + if accept(path) { |
| 123 | + paths.Add(path) |
| 124 | + } |
| 125 | + |
| 126 | + if recursionFilter == nil || recursionFilter(path) { |
| 127 | + if isDir, err := path.IsDirCheck(); err != nil { |
| 128 | + return nil, err |
| 129 | + } else if isDir { |
| 130 | + subPaths, err := path.ReadDirRecursiveFiltered(recursionFilter, filters...) |
| 131 | + if err != nil { |
| 132 | + return nil, err |
| 133 | + } |
| 134 | + paths.AddAll(subPaths) |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + return paths, nil |
| 139 | +} |
| 140 | + |
| 141 | +// FilterDirectories is a ReadDirFilter that accepts only directories |
| 142 | +func FilterDirectories() ReadDirFilter { |
| 143 | + return func(path *Path) bool { |
| 144 | + return path.IsDir() |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +// FilterOutDirectories is a ReadDirFilter that rejects all directories |
| 149 | +func FilterOutDirectories() ReadDirFilter { |
| 150 | + return func(path *Path) bool { |
| 151 | + return !path.IsDir() |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +// FilterSuffixes creates a ReadDirFilter that accepts only the given |
| 156 | +// filename suffixes |
| 157 | +func FilterSuffixes(allowedSuffixes ...string) ReadDirFilter { |
| 158 | + return func(file *Path) bool { |
| 159 | + for _, suffix := range allowedSuffixes { |
| 160 | + if strings.HasSuffix(file.String(), suffix) { |
| 161 | + return true |
| 162 | + } |
| 163 | + } |
| 164 | + return false |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +// FilterOutSuffixes creates a ReadDirFilter that rejects all the given |
| 169 | +// filename suffixes |
| 170 | +func FilterOutSuffixes(rejectedSuffixes ...string) ReadDirFilter { |
| 171 | + return func(file *Path) bool { |
| 172 | + for _, suffix := range rejectedSuffixes { |
| 173 | + if strings.HasSuffix(file.String(), suffix) { |
| 174 | + return false |
| 175 | + } |
| 176 | + } |
| 177 | + return true |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +// FilterPrefixes creates a ReadDirFilter that accepts only the given |
| 182 | +// filename prefixes |
| 183 | +func FilterPrefixes(allowedPrefixes ...string) ReadDirFilter { |
| 184 | + return func(file *Path) bool { |
| 185 | + name := file.Base() |
| 186 | + for _, prefix := range allowedPrefixes { |
| 187 | + if strings.HasPrefix(name, prefix) { |
| 188 | + return true |
| 189 | + } |
| 190 | + } |
| 191 | + return false |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +// FilterOutPrefixes creates a ReadDirFilter that rejects all the given |
| 196 | +// filename prefixes |
| 197 | +func FilterOutPrefixes(rejectedPrefixes ...string) ReadDirFilter { |
| 198 | + return func(file *Path) bool { |
| 199 | + name := file.Base() |
| 200 | + for _, prefix := range rejectedPrefixes { |
| 201 | + if strings.HasPrefix(name, prefix) { |
| 202 | + return false |
| 203 | + } |
| 204 | + } |
| 205 | + return true |
| 206 | + } |
| 207 | +} |
| 208 | + |
| 209 | +// OrFilter creates a ReadDirFilter that accepts all items that are accepted by x or by y |
| 210 | +func OrFilter(x, y ReadDirFilter) ReadDirFilter { |
| 211 | + return func(path *Path) bool { |
| 212 | + return x(path) || y(path) |
| 213 | + } |
| 214 | +} |
| 215 | + |
| 216 | +// AndFilter creates a ReadDirFilter that accepts all items that are accepted by both x and y |
| 217 | +func AndFilter(x, y ReadDirFilter) ReadDirFilter { |
| 218 | + return func(path *Path) bool { |
| 219 | + return x(path) && y(path) |
| 220 | + } |
| 221 | +} |
| 222 | + |
| 223 | +// NotFilter creates a ReadDifFilter that accepts all items rejected by x and viceversa |
| 224 | +func NotFilter(x ReadDirFilter) ReadDirFilter { |
| 225 | + return func(path *Path) bool { |
| 226 | + return !x(path) |
| 227 | + } |
| 228 | +} |
0 commit comments