Skip to content

Commit

Permalink
codespace create: make the branch input optional
Browse files Browse the repository at this point in the history
When blank, the branch name will default to the default branch for the
repository.
  • Loading branch information
mislav committed Oct 22, 2021
1 parent 3492109 commit 436762d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 43 deletions.
5 changes: 3 additions & 2 deletions internal/codespaces/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@ func (a *API) GetUser(ctx context.Context) (*User, error) {

// Repository represents a GitHub repository.
type Repository struct {
ID int `json:"id"`
FullName string `json:"full_name"`
ID int `json:"id"`
FullName string `json:"full_name"`
DefaultBranch string `json:"default_branch"`
}

// GetRepository returns the repository associated with the given owner and name.
Expand Down
76 changes: 35 additions & 41 deletions pkg/cmd/codespace/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,50 @@ func newCreateCmd(app *App) *cobra.Command {
func (a *App) Create(ctx context.Context, opts createOptions) error {
locationCh := getLocation(ctx, a.apiClient)

repo, err := getRepoName(opts.repo)
if err != nil {
return fmt.Errorf("error getting repository name: %w", err)
userInputs := struct {
Repository string
Branch string
}{
Repository: opts.repo,
Branch: opts.branch,
}
branch, err := getBranchName(opts.branch)
if err != nil {
return fmt.Errorf("error getting branch name: %w", err)

if userInputs.Repository == "" {
branchPrompt := "Branch (leave blank for default branch):"
if userInputs.Branch != "" {
branchPrompt = "Branch:"
}
questions := []*survey.Question{
{
Name: "repository",
Prompt: &survey.Input{Message: "Repository:"},
Validate: survey.Required,
},
{
Name: "branch",
Prompt: &survey.Input{
Message: branchPrompt,
Default: userInputs.Branch,
},
},
}
if err := ask(questions, &userInputs); err != nil {
return fmt.Errorf("failed to prompt: %w", err)
}
}

a.StartProgressIndicatorWithLabel("Fetching repository")
repository, err := a.apiClient.GetRepository(ctx, repo)
repository, err := a.apiClient.GetRepository(ctx, userInputs.Repository)
a.StopProgressIndicator()
if err != nil {
return fmt.Errorf("error getting repository: %w", err)
}

branch := userInputs.Branch
if branch == "" {
branch = repository.DefaultBranch
}

locationResult := <-locationCh
if locationResult.Err != nil {
return fmt.Errorf("error getting codespace region location: %w", locationResult.Err)
Expand Down Expand Up @@ -172,40 +200,6 @@ func getLocation(ctx context.Context, apiClient apiClient) <-chan locationResult
return ch
}

// getRepoName prompts the user for the name of the repository, or returns the repository if non-empty.
func getRepoName(repo string) (string, error) {
if repo != "" {
return repo, nil
}

repoSurvey := []*survey.Question{
{
Name: "repository",
Prompt: &survey.Input{Message: "Repository:"},
Validate: survey.Required,
},
}
err := ask(repoSurvey, &repo)
return repo, err
}

// getBranchName prompts the user for the name of the branch, or returns the branch if non-empty.
func getBranchName(branch string) (string, error) {
if branch != "" {
return branch, nil
}

branchSurvey := []*survey.Question{
{
Name: "branch",
Prompt: &survey.Input{Message: "Branch:"},
Validate: survey.Required,
},
}
err := ask(branchSurvey, &branch)
return branch, err
}

// getMachineName prompts the user to select the machine type, or validates the machine if non-empty.
func getMachineName(ctx context.Context, apiClient apiClient, repoID int, machine, branch, location string) (string, error) {
machines, err := apiClient.GetCodespacesMachines(ctx, repoID, branch, location)
Expand Down

0 comments on commit 436762d

Please sign in to comment.