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