You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

path.go 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright 2015 go-swagger maintainers
  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 swag
  15. import (
  16. "os"
  17. "path/filepath"
  18. "runtime"
  19. "strings"
  20. )
  21. const (
  22. // GOPATHKey represents the env key for gopath
  23. GOPATHKey = "GOPATH"
  24. )
  25. // FindInSearchPath finds a package in a provided lists of paths
  26. func FindInSearchPath(searchPath, pkg string) string {
  27. pathsList := filepath.SplitList(searchPath)
  28. for _, path := range pathsList {
  29. if evaluatedPath, err := filepath.EvalSymlinks(filepath.Join(path, "src", pkg)); err == nil {
  30. if _, err := os.Stat(evaluatedPath); err == nil {
  31. return evaluatedPath
  32. }
  33. }
  34. }
  35. return ""
  36. }
  37. // FindInGoSearchPath finds a package in the $GOPATH:$GOROOT
  38. func FindInGoSearchPath(pkg string) string {
  39. return FindInSearchPath(FullGoSearchPath(), pkg)
  40. }
  41. // FullGoSearchPath gets the search paths for finding packages
  42. func FullGoSearchPath() string {
  43. allPaths := os.Getenv(GOPATHKey)
  44. if allPaths == "" {
  45. allPaths = filepath.Join(os.Getenv("HOME"), "go")
  46. }
  47. if allPaths != "" {
  48. allPaths = strings.Join([]string{allPaths, runtime.GOROOT()}, ":")
  49. } else {
  50. allPaths = runtime.GOROOT()
  51. }
  52. return allPaths
  53. }