ensureDevcontainerConfig creates or updates devcontainer.json If .devcontainer/devcontainer.json exists, it updates it with gh-aw configuration. If it doesn't exist, it creates it at the default location.
(verbose bool, additionalRepos []string)
| 65 | // If .devcontainer/devcontainer.json exists, it updates it with gh-aw configuration. |
| 66 | // If it doesn't exist, it creates it at the default location. |
| 67 | func ensureDevcontainerConfig(verbose bool, additionalRepos []string) error { |
| 68 | devcontainerLog.Printf("Creating or updating devcontainer.json with additional repos: %v", additionalRepos) |
| 69 | |
| 70 | // Check for existing devcontainer at default location first |
| 71 | defaultDevcontainerPath := filepath.Join(".devcontainer", "devcontainer.json") |
| 72 | devcontainerPath := defaultDevcontainerPath |
| 73 | |
| 74 | devcontainerDir := ".devcontainer" |
| 75 | if err := fileutil.EnsureParentDir(devcontainerPath, constants.DirPermPublic); err != nil { |
| 76 | return fmt.Errorf("failed to create .devcontainer directory: %w", err) |
| 77 | } |
| 78 | devcontainerLog.Printf("Ensured directory exists: %s", devcontainerDir) |
| 79 | |
| 80 | // Check if file already exists at default location |
| 81 | var existingConfig *DevcontainerConfig |
| 82 | if _, err := os.Stat(devcontainerPath); err == nil { |
| 83 | devcontainerLog.Printf("File already exists: %s", devcontainerPath) |
| 84 | |
| 85 | // Read existing config to update it |
| 86 | existingData, err := os.ReadFile(devcontainerPath) |
| 87 | if err != nil { |
| 88 | devcontainerLog.Printf("Failed to read existing config: %v", err) |
| 89 | return fmt.Errorf("failed to read existing devcontainer.json: %w", err) |
| 90 | } |
| 91 | |
| 92 | var config DevcontainerConfig |
| 93 | if err := json.Unmarshal(existingData, &config); err != nil { |
| 94 | devcontainerLog.Printf("Failed to parse existing config: %v", err) |
| 95 | return fmt.Errorf("failed to parse existing devcontainer.json: %w", err) |
| 96 | } |
| 97 | existingConfig = &config |
| 98 | devcontainerLog.Printf("Successfully parsed existing devcontainer.json") |
| 99 | } |
| 100 | |
| 101 | // Get current repository name from git remote |
| 102 | repoName := getCurrentRepoName() |
| 103 | if repoName == "" { |
| 104 | repoName = "current-repo" |
| 105 | } |
| 106 | |
| 107 | // Get the owner from the current repository |
| 108 | owner := getRepoOwner() |
| 109 | |
| 110 | // Prepare gh-aw specific configuration |
| 111 | ghAwRepositories := buildRepositoryPermissions(repoName, owner, additionalRepos) |
| 112 | |
| 113 | var config DevcontainerConfig |
| 114 | |
| 115 | if existingConfig != nil { |
| 116 | // Update existing configuration |
| 117 | devcontainerLog.Printf("Updating existing devcontainer.json") |
| 118 | config = *existingConfig |
| 119 | |
| 120 | // Ensure customizations exists |
| 121 | if config.Customizations == nil { |
| 122 | config.Customizations = &DevcontainerCustomizations{} |
| 123 | } |
| 124 |