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