MCPcopy Create free account
hub / github.com/coder/envbuilder / findDevcontainerJSON

Function findDevcontainerJSON

envbuilder.go:1501–1570  ·  view source on GitHub ↗
(workspaceFolder string, options options.Options)

Source from the content-addressed store, hash-verified

1499}
1500
1501func findDevcontainerJSON(workspaceFolder string, options options.Options) (string, string, error) {
1502 if workspaceFolder == "" {
1503 workspaceFolder = options.WorkspaceFolder
1504 }
1505
1506 // 0. Check if custom devcontainer directory or path is provided.
1507 if options.DevcontainerDir != "" || options.DevcontainerJSONPath != "" {
1508 devcontainerDir := options.DevcontainerDir
1509 if devcontainerDir == "" {
1510 devcontainerDir = ".devcontainer"
1511 }
1512
1513 // If `devcontainerDir` is not an absolute path, assume it is relative to the workspace folder.
1514 if !filepath.IsAbs(devcontainerDir) {
1515 devcontainerDir = filepath.Join(workspaceFolder, devcontainerDir)
1516 }
1517
1518 // An absolute location always takes a precedence.
1519 devcontainerPath := options.DevcontainerJSONPath
1520 if filepath.IsAbs(devcontainerPath) {
1521 return options.DevcontainerJSONPath, devcontainerDir, nil
1522 }
1523 // If an override is not provided, assume it is just `devcontainer.json`.
1524 if devcontainerPath == "" {
1525 devcontainerPath = "devcontainer.json"
1526 }
1527
1528 if !filepath.IsAbs(devcontainerPath) {
1529 devcontainerPath = filepath.Join(devcontainerDir, devcontainerPath)
1530 }
1531 return devcontainerPath, devcontainerDir, nil
1532 }
1533
1534 // 1. Check `workspaceFolder`/.devcontainer/devcontainer.json.
1535 location := filepath.Join(workspaceFolder, ".devcontainer", "devcontainer.json")
1536 if _, err := options.Filesystem.Stat(location); err == nil {
1537 return location, filepath.Dir(location), nil
1538 }
1539
1540 // 2. Check `workspaceFolder`/devcontainer.json.
1541 location = filepath.Join(workspaceFolder, "devcontainer.json")
1542 if _, err := options.Filesystem.Stat(location); err == nil {
1543 return location, filepath.Dir(location), nil
1544 }
1545
1546 // 3. Check every folder: `workspaceFolder`/.devcontainer/<folder>/devcontainer.json.
1547 devcontainerDir := filepath.Join(workspaceFolder, ".devcontainer")
1548
1549 fileInfos, err := options.Filesystem.ReadDir(devcontainerDir)
1550 if err != nil {
1551 return "", "", err
1552 }
1553
1554 for _, fileInfo := range fileInfos {
1555 if !fileInfo.IsDir() {
1556 options.Logger(log.LevelDebug, `%s is a file`, fileInfo.Name())
1557 continue
1558 }

Callers 3

runFunction · 0.85
RunCacheProbeFunction · 0.85
TestFindDevcontainerJSONFunction · 0.85

Calls 6

JoinMethod · 0.80
ErrorMethod · 0.80
StatMethod · 0.65
ReadDirMethod · 0.65
IsDirMethod · 0.45
NameMethod · 0.45

Tested by 1

TestFindDevcontainerJSONFunction · 0.68