-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdevcontainer.go
More file actions
57 lines (46 loc) · 1.54 KB
/
devcontainer.go
File metadata and controls
57 lines (46 loc) · 1.54 KB
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
package devcontainers
import (
"fmt"
"os"
"path/filepath"
"strings"
)
func getDevContainerJsonPath(folderPath string) (string, error) {
pathsToTest := []string{".devcontainer/devcontainer.json", ".devcontainer.json"}
for _, path := range pathsToTest {
devcontainerJsonPath := filepath.Join(folderPath, path)
devContainerJsonInfo, err := os.Stat(devcontainerJsonPath)
if err == nil && !devContainerJsonInfo.IsDir() {
return devcontainerJsonPath, nil
}
}
return "", fmt.Errorf("devcontainer.json not found. Looked for %s", strings.Join(pathsToTest, ","))
}
func FindDevContainerInAncestorPaths(folderPath string) (string, error) {
currentPath, err := filepath.Abs(folderPath)
if err != nil {
return "", fmt.Errorf("error getting absolute path: %w", err)
}
for {
// Check if devcontainer.json exists in current path
_, err := getDevContainerJsonPath(currentPath)
if err == nil {
return currentPath, nil
}
// Check if this is a git repository root
gitPath := filepath.Join(currentPath, ".git")
gitInfo, gitErr := os.Stat(gitPath)
isGitRoot := gitErr == nil && gitInfo.IsDir()
// If we're at a git root, stop searching (we already checked this folder)
if isGitRoot {
return "", fmt.Errorf("devcontainer.json not found in ancestor paths")
}
// Move to parent directory
parentPath := filepath.Dir(currentPath)
// Check if we've reached the root (parent is same as current)
if parentPath == currentPath {
return "", fmt.Errorf("devcontainer.json not found in ancestor paths")
}
currentPath = parentPath
}
}