Name extendedGlob() anonymous functions
Moves inline function definitions out of the function. Helps with code readability. Signed-off-by: Sebastian Soto <ssoto@redhat.com>
This commit is contained in:
parent
1b5cdd50fa
commit
a3dcd82d96
116
copier/copier.go
116
copier/copier.go
|
@ -51,62 +51,6 @@ func init() {
|
||||||
// will be concatenated.
|
// will be concatenated.
|
||||||
// The matched paths are returned in lexical order, which makes the output deterministic.
|
// The matched paths are returned in lexical order, which makes the output deterministic.
|
||||||
func extendedGlob(pattern string) (matches []string, err error) {
|
func extendedGlob(pattern string) (matches []string, err error) {
|
||||||
subdirs := func(dir string) []string {
|
|
||||||
var subdirectories []string
|
|
||||||
if err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
|
|
||||||
if err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if d.IsDir() {
|
|
||||||
if rel, err := filepath.Rel(dir, path); err == nil {
|
|
||||||
subdirectories = append(subdirectories, rel)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}); err != nil {
|
|
||||||
subdirectories = []string{"."}
|
|
||||||
}
|
|
||||||
return subdirectories
|
|
||||||
}
|
|
||||||
expandPatterns := func(pattern string) []string {
|
|
||||||
components := []string{}
|
|
||||||
dir := pattern
|
|
||||||
file := ""
|
|
||||||
for dir != filepath.VolumeName(dir) && dir != string(os.PathSeparator) {
|
|
||||||
dir, file = filepath.Split(dir)
|
|
||||||
if file != "" {
|
|
||||||
components = append([]string{file}, components...)
|
|
||||||
}
|
|
||||||
dir = strings.TrimSuffix(dir, string(os.PathSeparator))
|
|
||||||
}
|
|
||||||
patterns := []string{filepath.VolumeName(dir) + string(os.PathSeparator)}
|
|
||||||
for i := range components {
|
|
||||||
var nextPatterns []string
|
|
||||||
if components[i] == "**" {
|
|
||||||
for _, parent := range patterns {
|
|
||||||
nextSubdirs := subdirs(parent)
|
|
||||||
for _, nextSubdir := range nextSubdirs {
|
|
||||||
nextPatterns = append(nextPatterns, filepath.Join(parent, nextSubdir))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
for _, parent := range patterns {
|
|
||||||
nextPattern := filepath.Join(parent, components[i])
|
|
||||||
nextPatterns = append(nextPatterns, nextPattern)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
patterns = []string{}
|
|
||||||
seen := map[string]struct{}{}
|
|
||||||
for _, nextPattern := range nextPatterns {
|
|
||||||
if _, seen := seen[nextPattern]; seen {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
patterns = append(patterns, nextPattern)
|
|
||||||
seen[nextPattern] = struct{}{}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return patterns
|
|
||||||
}
|
|
||||||
patterns := expandPatterns(pattern)
|
patterns := expandPatterns(pattern)
|
||||||
for _, pattern := range patterns {
|
for _, pattern := range patterns {
|
||||||
theseMatches, err := filepath.Glob(pattern)
|
theseMatches, err := filepath.Glob(pattern)
|
||||||
|
@ -119,6 +63,66 @@ func extendedGlob(pattern string) (matches []string, err error) {
|
||||||
return matches, nil
|
return matches, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// subDirs returns a recursive search of all directories under the given dir
|
||||||
|
func subDirs(dir string) []string {
|
||||||
|
var subdirectories []string
|
||||||
|
if err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if d.IsDir() {
|
||||||
|
if rel, err := filepath.Rel(dir, path); err == nil {
|
||||||
|
subdirectories = append(subdirectories, rel)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}); err != nil {
|
||||||
|
subdirectories = []string{"."}
|
||||||
|
}
|
||||||
|
return subdirectories
|
||||||
|
}
|
||||||
|
|
||||||
|
// expandPatterns fans out a glob pattern so that "**" is expanded to all possible subdirectory paths at that location.
|
||||||
|
func expandPatterns(pattern string) []string {
|
||||||
|
components := []string{}
|
||||||
|
dir := pattern
|
||||||
|
file := ""
|
||||||
|
for dir != filepath.VolumeName(dir) && dir != string(os.PathSeparator) {
|
||||||
|
dir, file = filepath.Split(dir)
|
||||||
|
if file != "" {
|
||||||
|
components = append([]string{file}, components...)
|
||||||
|
}
|
||||||
|
dir = strings.TrimSuffix(dir, string(os.PathSeparator))
|
||||||
|
}
|
||||||
|
patterns := []string{filepath.VolumeName(dir) + string(os.PathSeparator)}
|
||||||
|
for i := range components {
|
||||||
|
var nextPatterns []string
|
||||||
|
if components[i] == "**" {
|
||||||
|
for _, parent := range patterns {
|
||||||
|
nextSubdirs := subDirs(parent)
|
||||||
|
for _, nextSubdir := range nextSubdirs {
|
||||||
|
nextPatterns = append(nextPatterns, filepath.Join(parent, nextSubdir))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for _, parent := range patterns {
|
||||||
|
nextPattern := filepath.Join(parent, components[i])
|
||||||
|
nextPatterns = append(nextPatterns, nextPattern)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
patterns = []string{}
|
||||||
|
seen := map[string]struct{}{}
|
||||||
|
for _, nextPattern := range nextPatterns {
|
||||||
|
if _, seen := seen[nextPattern]; seen {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
patterns = append(patterns, nextPattern)
|
||||||
|
seen[nextPattern] = struct{}{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return patterns
|
||||||
|
}
|
||||||
|
|
||||||
// isArchivePath returns true if the specified path can be read like a (possibly
|
// isArchivePath returns true if the specified path can be read like a (possibly
|
||||||
// compressed) tarball.
|
// compressed) tarball.
|
||||||
func isArchivePath(path string) bool {
|
func isArchivePath(path string) bool {
|
||||||
|
|
Loading…
Reference in New Issue