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