Allow globs in filenames for locators

Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
Evan Lezar 2022-06-30 11:00:58 +02:00
parent e8843c38f2
commit 7f47a61986
3 changed files with 35 additions and 26 deletions

View File

@ -49,18 +49,19 @@ func NewExecutableLocator(logger *log.Logger, root string) Locator {
var _ Locator = (*executable)(nil) var _ Locator = (*executable)(nil)
// Locate finds executable files in the path. If a relative or absolute path is specified, the prefix paths are not considered. // Locate finds executable files with the specified pattern in the path.
func (p executable) Locate(filename string) ([]string, error) { // If a relative or absolute path is specified, the prefix paths are not considered.
func (p executable) Locate(pattern string) ([]string, error) {
// For absolute paths we ensure that it is executable // For absolute paths we ensure that it is executable
if strings.Contains(filename, "/") { if strings.Contains(pattern, "/") {
err := assertExecutable(filename) err := assertExecutable(pattern)
if err != nil { if err != nil {
return nil, fmt.Errorf("absolute path %v is not an executable file: %v", filename, err) return nil, fmt.Errorf("absolute path %v is not an executable file: %v", pattern, err)
} }
return []string{filename}, nil return []string{pattern}, nil
} }
return p.file.Locate(filename) return p.file.Locate(pattern)
} }
// assertExecutable checks whether the specified path is an execuable file. // assertExecutable checks whether the specified path is an execuable file.

View File

@ -50,22 +50,29 @@ func newFileLocator(logger *log.Logger, root string) file {
var _ Locator = (*file)(nil) var _ Locator = (*file)(nil)
// Locate attempts to find the specified file. All prefixes are searched and any matching // Locate attempts to find files with names matching the specified pattern.
// candidates are returned. If no matches are found, an error is returned. // All prefixes are searched and any matching candidates are returned. If no matches are found, an error is returned.
func (p file) Locate(filename string) ([]string, error) { func (p file) Locate(pattern string) ([]string, error) {
var filenames []string var filenames []string
for _, prefix := range p.prefixes { for _, prefix := range p.prefixes {
candidate := filepath.Join(prefix, filename) pathPattern := filepath.Join(prefix, pattern)
p.logger.Debugf("Checking candidate '%v'", candidate) candidates, err := filepath.Glob(pathPattern)
err := p.filter(candidate)
if err != nil { if err != nil {
p.logger.Debugf("Candidate '%v' does not meet requirements: %v", candidate, err) p.logger.Debugf("Checking pattern '%v' failed: %v", pathPattern, err)
continue }
for _, candidate := range candidates {
p.logger.Debugf("Checking candidate '%v'", candidate)
err := p.filter(candidate)
if err != nil {
p.logger.Debugf("Candidate '%v' does not meet requirements: %v", candidate, err)
continue
}
filenames = append(filenames, candidate)
} }
filenames = append(filenames, candidate)
} }
if len(filename) == 0 { if len(filenames) == 0 {
return nil, fmt.Errorf("file %v not found", filename) return nil, fmt.Errorf("pattern %v not found", pattern)
} }
return filenames, nil return filenames, nil
} }

View File

@ -52,10 +52,10 @@ func NewSymlinkLocator(logger *logrus.Logger, root string) Locator {
return &l return &l
} }
// Locate finds the specified file at the specified root. If the file is a symlink, the link is followed and all candidates // Locate finds the specified pattern at the specified root.
// to the final target are returned. // If the file is a symlink, the link is followed and all candidates to the final target are returned.
func (p symlinkChain) Locate(filename string) ([]string, error) { func (p symlinkChain) Locate(pattern string) ([]string, error) {
candidates, err := p.file.Locate(filename) candidates, err := p.file.Locate(pattern)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -104,14 +104,15 @@ func (p symlinkChain) Locate(filename string) ([]string, error) {
return filenames, nil return filenames, nil
} }
// Locate finds the specified file at the specified root. If the file is a symlink, the link is resolved and the target returned. // Locate finds the specified pattern at the specified root.
func (p symlink) Locate(filename string) ([]string, error) { // If the file is a symlink, the link is resolved and the target returned.
candidates, err := p.file.Locate(filename) func (p symlink) Locate(pattern string) ([]string, error) {
candidates, err := p.file.Locate(pattern)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if len(candidates) != 1 { if len(candidates) != 1 {
return nil, fmt.Errorf("failed to uniquely resolve symlink %v: %v", filename, candidates) return nil, fmt.Errorf("failed to uniquely resolve symlink %v: %v", pattern, candidates)
} }
target, err := filepath.EvalSymlinks(candidates[0]) target, err := filepath.EvalSymlinks(candidates[0])