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