find.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Copyright 2015 CNI authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package invoke
  15. import (
  16. "fmt"
  17. "os"
  18. "path/filepath"
  19. )
  20. // FindInPath returns the full path of the plugin by searching in the provided path
  21. func FindInPath(plugin string, paths []string) (string, error) {
  22. if plugin == "" {
  23. return "", fmt.Errorf("no plugin name provided")
  24. }
  25. if len(paths) == 0 {
  26. return "", fmt.Errorf("no paths provided")
  27. }
  28. var fullpath string
  29. for _, path := range paths {
  30. full := filepath.Join(path, plugin)
  31. if fi, err := os.Stat(full); err == nil && fi.Mode().IsRegular() {
  32. fullpath = full
  33. break
  34. }
  35. }
  36. if fullpath == "" {
  37. return "", fmt.Errorf("failed to find plugin %q in path %s", plugin, paths)
  38. }
  39. return fullpath, nil
  40. }