Create creates a new Codespace
(ctx context.Context, opts createOptions)
| 127 | |
| 128 | // Create creates a new Codespace |
| 129 | func (a *App) Create(ctx context.Context, opts createOptions) error { |
| 130 | // Overrides for Codespace developers to target test environments |
| 131 | vscsLocation := os.Getenv("VSCS_LOCATION") |
| 132 | vscsTarget := os.Getenv("VSCS_TARGET") |
| 133 | vscsTargetUrl := os.Getenv("VSCS_TARGET_URL") |
| 134 | |
| 135 | userInputs := struct { |
| 136 | Repository string |
| 137 | Branch string |
| 138 | Location string |
| 139 | }{ |
| 140 | Repository: opts.repo, |
| 141 | Branch: opts.branch, |
| 142 | Location: opts.location, |
| 143 | } |
| 144 | |
| 145 | if opts.useWeb && userInputs.Repository == "" { |
| 146 | return a.browser.Browse(fmt.Sprintf("%s/codespaces/new", a.apiClient.ServerURL())) |
| 147 | } |
| 148 | |
| 149 | prompter := &Prompter{} |
| 150 | promptForRepoAndBranch := userInputs.Repository == "" && !opts.useWeb |
| 151 | if promptForRepoAndBranch { |
| 152 | var defaultRepo string |
| 153 | if remotes, _ := a.remotes(); remotes != nil { |
| 154 | if defaultRemote, _ := remotes.ResolvedRemote(); defaultRemote != nil { |
| 155 | // this is a remote explicitly chosen via `repo set-default` |
| 156 | defaultRepo = ghrepo.FullName(defaultRemote) |
| 157 | } else if len(remotes) > 0 { |
| 158 | // as a fallback, just pick the first remote |
| 159 | defaultRepo = ghrepo.FullName(remotes[0]) |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | repoQuestions := []*survey.Question{ |
| 164 | { |
| 165 | Name: "repository", |
| 166 | Prompt: &survey.Input{ |
| 167 | Message: "Repository:", |
| 168 | Help: "Search for repos by name. To search within an org or user, or to see private repos, enter at least ':user/'.", |
| 169 | Default: defaultRepo, |
| 170 | Suggest: func(toComplete string) []string { |
| 171 | return getRepoSuggestions(ctx, a.apiClient, toComplete) |
| 172 | }, |
| 173 | }, |
| 174 | Validate: survey.Required, |
| 175 | }, |
| 176 | } |
| 177 | if err := prompter.Ask(repoQuestions, &userInputs); err != nil { |
| 178 | return fmt.Errorf("failed to prompt: %w", err) |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | if userInputs.Location == "" && vscsLocation != "" { |
| 183 | userInputs.Location = vscsLocation |
| 184 | } |
| 185 | |
| 186 | var repository *api.Repository |