/
find_file.go
60 lines (52 loc) · 1.59 KB
/
find_file.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
57
58
59
60
/*---------------------------------------------------------------------------------------------
* Copyright (c) Peter Bjorklund. All rights reserved.
* Licensed under the MIT License. See LICENSE in the project root for license information.
*--------------------------------------------------------------------------------------------*/
package depslib
import (
"fmt"
"os"
"path"
"path/filepath"
)
func find(startPath string) ([]string, error) {
_, wdErr := os.Getwd()
if wdErr != nil {
return nil, wdErr
}
directory, directoryErr := filepath.Abs(startPath)
if directoryErr != nil {
return nil, directoryErr
}
var foundRoots []string
for {
foundDirectory, foundDirectoryErr := os.Lstat(directory)
if foundDirectoryErr != nil || !foundDirectory.IsDir() {
return foundRoots, nil
}
configurationFilename := path.Join(directory, "deps.toml")
foundConfiguration, foundConfigurationErr := os.Lstat(configurationFilename)
if foundConfigurationErr == nil && !foundConfiguration.IsDir() {
foundRoots = append(foundRoots, configurationFilename)
}
foundGitDir, foundGitErr := os.Lstat(path.Join(directory, ".git"))
if foundGitErr == nil && foundGitDir.IsDir() {
return foundRoots, nil
}
if directory == "." || directory == "/" {
return foundRoots, nil
}
next := filepath.Dir(directory)
if next == directory {
return foundRoots, nil
}
directory = next
}
}
func FindClosestConfigurationFiles(startPath string) ([]string, error) {
roots, rootsErr := find(startPath)
if len(roots) == 0 {
return nil, fmt.Errorf("no deps.toml file found")
}
return roots, rootsErr
}