Code refactoring for bpa operator
[icn.git] / cmd / bpa-operator / vendor / sigs.k8s.io / controller-tools / pkg / crd / util / util.go
1 /*
2 Copyright 2018 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8     http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package util
18
19 import (
20         "bufio"
21         "fmt"
22         gobuild "go/build"
23         "log"
24         "os"
25         "path"
26         "path/filepath"
27         "strings"
28 )
29
30 // IsGoSrcPath validate if given path is of path $GOPATH/src.
31 func IsGoSrcPath(filePath string) bool {
32         for _, gopath := range getGoPaths() {
33                 goSrc := path.Join(gopath, "src")
34                 if filePath == goSrc {
35                         return true
36                 }
37         }
38
39         return false
40 }
41
42 // IsUnderGoSrcPath validate if given path is under path $GOPATH/src.
43 func IsUnderGoSrcPath(filePath string) bool {
44         for _, gopath := range getGoPaths() {
45                 goSrc := path.Join(gopath, "src")
46                 if strings.HasPrefix(filepath.Dir(filePath), goSrc) {
47                         return true
48                 }
49         }
50
51         return false
52 }
53
54 // DirToGoPkg returns the Gopkg for the given directory if it exists
55 // under a GOPATH otherwise returns error. For example,
56 // /Users/x/go/src/github.com/y/z ==> github.com/y/z
57 func DirToGoPkg(dir string) (pkg string, err error) {
58         goPaths := getGoPaths()
59         for _, gopath := range goPaths {
60                 goSrc := path.Join(gopath, "src")
61                 if !strings.HasPrefix(dir, goSrc) {
62                         continue
63                 }
64                 pkg, err := filepath.Rel(goSrc, dir)
65                 if err == nil {
66                         return pkg, err
67                 }
68         }
69
70         return "", fmt.Errorf("dir '%s' does not exist under any GOPATH %v", dir, goPaths)
71 }
72
73 func getGoPaths() []string {
74         gopaths := os.Getenv("GOPATH")
75         if len(gopaths) == 0 {
76                 gopaths = gobuild.Default.GOPATH
77         }
78         return filepath.SplitList(gopaths)
79 }
80
81 // PathHasProjectFile validate if PROJECT file exists under the path.
82 func PathHasProjectFile(filePath string) bool {
83         if _, err := os.Stat(path.Join(filePath, "PROJECT")); os.IsNotExist(err) {
84                 return false
85         }
86
87         return true
88 }
89
90 // GetDomainFromProject get domain information from the PROJECT file under the path.
91 func GetDomainFromProject(rootPath string) string {
92         return GetFieldFromProject("domain", rootPath)
93 }
94
95 // GetRepoFromProject get domain information from the PROJECT file under the path.
96 func GetRepoFromProject(rootPath string) string {
97         return GetFieldFromProject("repo", rootPath)
98 }
99
100 // GetFieldFromProject get field information from the PROJECT file under the path.
101 func GetFieldFromProject(fieldKey string, rootPath string) string {
102         var fieldVal string
103
104         file, err := os.Open(path.Join(rootPath, "PROJECT"))
105         if err != nil {
106                 log.Fatal(err)
107         }
108         defer func() {
109                 if err := file.Close(); err != nil {
110                         log.Fatal(err)
111                 }
112         }()
113
114         scanner := bufio.NewScanner(file)
115         for scanner.Scan() {
116                 if strings.HasPrefix(scanner.Text(), fmt.Sprintf("%s:", fieldKey)) {
117                         fieldInfo := strings.Split(scanner.Text(), ":")
118                         if len(fieldInfo) != 2 {
119                                 log.Fatalf("Unexpected %s info: %s", fieldKey, scanner.Text())
120                         }
121                         fieldVal = strings.Replace(fieldInfo[1], " ", "", -1)
122                         break
123                 }
124         }
125         if len(fieldVal) == 0 {
126                 log.Fatalf("%s/PROJECT file is missing value for '%s'", rootPath, fieldKey)
127         }
128
129         return fieldVal
130 }