forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocator.go
56 lines (46 loc) · 1.12 KB
/
locator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package manifestparser
import (
"os"
"path/filepath"
)
type Locator struct {
FilesToCheckFor []string
}
func NewLocator() *Locator {
return &Locator{
FilesToCheckFor: []string{
"manifest.yml",
"manifest.yaml",
},
}
}
func (loc Locator) Path(filepathOrDirectory string) (string, bool, error) {
info, err := os.Stat(filepathOrDirectory)
if os.IsNotExist(err) {
return "", false, nil
} else if err != nil {
return "", false, err
}
resolvedFilepathOrDirectory, err := filepath.EvalSymlinks(filepathOrDirectory)
if err != nil {
return "", false, err
}
if info.IsDir() {
return loc.handleDir(resolvedFilepathOrDirectory)
}
return loc.handleFilepath(resolvedFilepathOrDirectory)
}
func (loc Locator) handleDir(dir string) (string, bool, error) {
for _, filename := range loc.FilesToCheckFor {
fullPath := filepath.Join(dir, filename)
if _, err := os.Stat(fullPath); err == nil {
return fullPath, true, nil
} else if !os.IsNotExist(err) {
return "", false, err
}
}
return "", false, nil
}
func (Locator) handleFilepath(filepath string) (string, bool, error) {
return filepath, true, nil
}