github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/build/list/list.go (about) 1 /* 2 Copyright 2020 The Skaffold Authors 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package list 18 19 import ( 20 "fmt" 21 "path/filepath" 22 "sort" 23 24 "github.com/bmatcuk/doublestar" 25 26 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/walk" 27 ) 28 29 // Files list files in a workspace, given a list of patterns and exclusions. 30 // It supports globstar (`**`) for recursive listing of subdirectories. 31 func Files(workspace string, patterns, excludes []string) ([]string, error) { 32 // doublestar.Glob() doesn't find matches for patterns such as "." 33 // Solve this by turning the relative workspace directory into an absolute path. 34 workspace, err := filepath.Abs(workspace) 35 if err != nil { 36 return nil, fmt.Errorf("could not get absolute path of workspace %q: %w", workspace, err) 37 } 38 39 notExcluded := notExcluded(workspace, excludes) 40 41 depSet := map[string]bool{} 42 43 for _, pattern := range patterns { 44 expanded, err := doublestar.Glob(filepath.Join(workspace, pattern)) 45 if err != nil { 46 return nil, err 47 } 48 if len(expanded) == 0 { 49 return nil, fmt.Errorf("pattern %q did not match any file", pattern) 50 } 51 52 for _, absFrom := range expanded { 53 if err := walk.From(absFrom).Unsorted().When(notExcluded).WhenIsFile().Do(func(path string, info walk.Dirent) error { 54 relPath, err := filepath.Rel(workspace, path) 55 if err != nil { 56 return err 57 } 58 depSet[relPath] = true 59 return nil 60 }); err != nil { 61 return nil, fmt.Errorf("walking %q: %w", absFrom, err) 62 } 63 } 64 } 65 66 if len(depSet) == 0 { 67 // Adding this conditional in order to be very (overly?) careful with backwards compatibility. 68 // The previous implementation returned nil rather than empty slice in the case of no file dependencies. 69 return nil, nil 70 } 71 72 dependencies := make([]string, len(depSet)) 73 i := 0 74 for dep := range depSet { 75 dependencies[i] = dep 76 i++ 77 } 78 sort.Strings(dependencies) 79 return dependencies, nil 80 } 81 82 // notExcluded creates a walk.Predicate that matches file system entries 83 // only if they don't match a list of exclusion patterns. 84 func notExcluded(workspace string, excludes []string) walk.Predicate { 85 return func(path string, info walk.Dirent) (bool, error) { 86 relPath, err := filepath.Rel(workspace, path) 87 if err != nil { 88 return false, err 89 } 90 91 for _, exclude := range excludes { 92 matches, err := doublestar.PathMatch(exclude, relPath) 93 if err != nil { 94 return false, err 95 } 96 if matches { 97 if info.IsDir() { 98 return false, filepath.SkipDir 99 } 100 return false, nil 101 } 102 } 103 104 return true, nil 105 } 106 }