Skip to content

Commit

Permalink
Merge pull request cli#6313 from cli/context-prompter
Browse files Browse the repository at this point in the history
use Prompter in context
  • Loading branch information
Nate Smith authored Sep 22, 2022
2 parents d4e9b1f + 7b21a3e commit 6664707
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
15 changes: 7 additions & 8 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ import (
"errors"
"sort"

"github.com/AlecAivazis/survey/v2"
"github.com/cli/cli/v2/api"
"github.com/cli/cli/v2/git"
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/cli/cli/v2/pkg/iostreams"
"github.com/cli/cli/v2/pkg/prompt"
)

// cap the number of git remotes looked up, since the user might have an
Expand Down Expand Up @@ -59,7 +57,11 @@ type ResolvedRemotes struct {
apiClient *api.Client
}

func (r *ResolvedRemotes) BaseRepo(io *iostreams.IOStreams) (ghrepo.Interface, error) {
type iprompter interface {
Select(string, string, []string) (int, error)
}

func (r *ResolvedRemotes) BaseRepo(io *iostreams.IOStreams, p iprompter) (ghrepo.Interface, error) {
if r.baseOverride != nil {
return r.baseOverride, nil
}
Expand Down Expand Up @@ -119,14 +121,11 @@ func (r *ResolvedRemotes) BaseRepo(io *iostreams.IOStreams) (ghrepo.Interface, e
// hide the spinner in case a command started the progress indicator before base repo was fully
// resolved, e.g. in `gh issue view`
io.StopProgressIndicator()
//nolint:staticcheck // SA1019: prompt.SurveyAskOne is deprecated: use Prompter
err := prompt.SurveyAskOne(&survey.Select{
Message: "Which should be the base repository (used for e.g. querying issues) for this directory?",
Options: repoNames,
}, &baseName)
selected, err := p.Select("Which should be the base repository (used for e.g. querying issues) for this directory?", "", repoNames)
if err != nil {
return nil, err
}
baseName = repoNames[selected]
}

// determine corresponding git remote
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/factory/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func SmartBaseRepoFunc(f *cmdutil.Factory) func() (ghrepo.Interface, error) {
if err != nil {
return nil, err
}
baseRepo, err := repoContext.BaseRepo(f.IOStreams)
baseRepo, err := repoContext.BaseRepo(f.IOStreams, f.Prompter)
if err != nil {
return nil, err
}
Expand Down
8 changes: 7 additions & 1 deletion pkg/cmd/pr/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ import (
"github.com/spf13/cobra"
)

type iprompter interface {
Select(string, string, []string) (int, error)
}

type CreateOptions struct {
// This struct stores user input and factory functions
HttpClient func() (*http.Client, error)
Expand All @@ -33,6 +37,7 @@ type CreateOptions struct {
Remotes func() (context.Remotes, error)
Branch func() (string, error)
Browser browser.Browser
Prompter iprompter
Finder shared.PRFinder

TitleProvided bool
Expand Down Expand Up @@ -83,6 +88,7 @@ func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co
Remotes: f.Remotes,
Branch: f.Branch,
Browser: f.Browser,
Prompter: f.Prompter,
}

var bodyFile string
Expand Down Expand Up @@ -480,7 +486,7 @@ func NewCreateContext(opts *CreateOptions) (*CreateContext, error) {
}

var baseRepo *api.Repository
if br, err := repoContext.BaseRepo(opts.IO); err == nil {
if br, err := repoContext.BaseRepo(opts.IO, opts.Prompter); err == nil {
if r, ok := br.(*api.Repository); ok {
baseRepo = r
} else {
Expand Down

0 comments on commit 6664707

Please sign in to comment.