2022-03-09 15:51:51 +00:00
|
|
|
/*
|
|
|
|
# Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package lookup
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2022-04-06 13:24:02 +00:00
|
|
|
type executable struct {
|
2022-03-09 15:51:51 +00:00
|
|
|
file
|
|
|
|
}
|
|
|
|
|
2022-04-06 17:56:43 +00:00
|
|
|
// NewExecutableLocator creates a locator to fine executable files in the path. A logger can also be specified.
|
|
|
|
func NewExecutableLocator(logger *log.Logger, root string) Locator {
|
2022-05-10 12:48:01 +00:00
|
|
|
paths := GetPaths(root)
|
2022-03-09 15:51:51 +00:00
|
|
|
|
2022-11-25 12:59:11 +00:00
|
|
|
return newExecutableLocator(logger, root, paths...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newExecutableLocator(logger *log.Logger, root string, paths ...string) *executable {
|
2022-12-02 10:38:40 +00:00
|
|
|
f := newFileLocator(
|
|
|
|
WithLogger(logger),
|
|
|
|
WithRoot(root),
|
|
|
|
WithSearchPaths(paths...),
|
|
|
|
WithFilter(assertExecutable),
|
2023-01-20 14:08:47 +00:00
|
|
|
WithCount(1),
|
2022-12-02 10:38:40 +00:00
|
|
|
)
|
|
|
|
|
2022-04-06 13:24:02 +00:00
|
|
|
l := executable{
|
2022-12-02 10:38:40 +00:00
|
|
|
file: *f,
|
2022-03-09 15:51:51 +00:00
|
|
|
}
|
2022-11-25 12:59:11 +00:00
|
|
|
|
2022-03-09 15:51:51 +00:00
|
|
|
return &l
|
|
|
|
}
|
|
|
|
|
2022-04-06 13:24:02 +00:00
|
|
|
var _ Locator = (*executable)(nil)
|
2022-03-09 15:51:51 +00:00
|
|
|
|
2022-06-30 09:00:58 +00:00
|
|
|
// Locate finds executable files with the specified pattern in the path.
|
|
|
|
// If a relative or absolute path is specified, the prefix paths are not considered.
|
|
|
|
func (p executable) Locate(pattern string) ([]string, error) {
|
2022-03-09 15:51:51 +00:00
|
|
|
// For absolute paths we ensure that it is executable
|
2022-06-30 09:00:58 +00:00
|
|
|
if strings.Contains(pattern, "/") {
|
|
|
|
err := assertExecutable(pattern)
|
2022-03-09 15:51:51 +00:00
|
|
|
if err != nil {
|
2022-06-30 09:00:58 +00:00
|
|
|
return nil, fmt.Errorf("absolute path %v is not an executable file: %v", pattern, err)
|
2022-03-09 15:51:51 +00:00
|
|
|
}
|
2022-06-30 09:00:58 +00:00
|
|
|
return []string{pattern}, nil
|
2022-03-09 15:51:51 +00:00
|
|
|
}
|
|
|
|
|
2022-06-30 09:00:58 +00:00
|
|
|
return p.file.Locate(pattern)
|
2022-03-09 15:51:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// assertExecutable checks whether the specified path is an execuable file.
|
|
|
|
func assertExecutable(filename string) error {
|
|
|
|
err := assertFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
info, err := os.Stat(filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if info.Mode()&0111 == 0 {
|
|
|
|
return fmt.Errorf("specified file '%v' is not executable", filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|