From 8f3032af3657458f9a4a633b377bc0662acc84e9 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Mon, 12 Sep 2022 15:08:37 -0700 Subject: [PATCH] use Prompter in auth commands --- pkg/cmd/auth/login/login.go | 3 +-- pkg/cmd/auth/logout/logout.go | 13 +++++------ pkg/cmd/auth/logout/logout_test.go | 30 +++++++++++++------------ pkg/cmd/auth/refresh/refresh.go | 13 +++-------- pkg/cmd/auth/refresh/refresh_test.go | 32 ++++++++++++++------------- pkg/cmd/auth/shared/git_credential.go | 3 +-- pkg/cmd/auth/shared/login_flow.go | 3 +-- pkg/cmd/auth/shared/prompt.go | 10 +++++++++ 8 files changed, 54 insertions(+), 53 deletions(-) create mode 100644 pkg/cmd/auth/shared/prompt.go diff --git a/pkg/cmd/auth/login/login.go b/pkg/cmd/auth/login/login.go index b0d0e02cf..48cb73bca 100644 --- a/pkg/cmd/auth/login/login.go +++ b/pkg/cmd/auth/login/login.go @@ -9,7 +9,6 @@ import ( "github.com/MakeNowJust/heredoc" "github.com/cli/cli/v2/internal/config" "github.com/cli/cli/v2/internal/ghinstance" - "github.com/cli/cli/v2/internal/prompter" "github.com/cli/cli/v2/pkg/cmd/auth/shared" "github.com/cli/cli/v2/pkg/cmdutil" "github.com/cli/cli/v2/pkg/iostreams" @@ -20,7 +19,7 @@ type LoginOptions struct { IO *iostreams.IOStreams Config func() (config.Config, error) HttpClient func() (*http.Client, error) - Prompter prompter.Prompter + Prompter shared.Prompt MainExecutable string diff --git a/pkg/cmd/auth/logout/logout.go b/pkg/cmd/auth/logout/logout.go index 029d5971a..98735b2e6 100644 --- a/pkg/cmd/auth/logout/logout.go +++ b/pkg/cmd/auth/logout/logout.go @@ -4,14 +4,12 @@ import ( "fmt" "net/http" - "github.com/AlecAivazis/survey/v2" "github.com/MakeNowJust/heredoc" "github.com/cli/cli/v2/api" "github.com/cli/cli/v2/internal/config" "github.com/cli/cli/v2/pkg/cmd/auth/shared" "github.com/cli/cli/v2/pkg/cmdutil" "github.com/cli/cli/v2/pkg/iostreams" - "github.com/cli/cli/v2/pkg/prompt" "github.com/spf13/cobra" ) @@ -19,6 +17,7 @@ type LogoutOptions struct { HttpClient func() (*http.Client, error) IO *iostreams.IOStreams Config func() (config.Config, error) + Prompter shared.Prompt Hostname string } @@ -27,6 +26,7 @@ func NewCmdLogout(f *cmdutil.Factory, runF func(*LogoutOptions) error) *cobra.Co HttpClient: f.HttpClient, IO: f.IOStreams, Config: f.Config, + Prompter: f.Prompter, } cmd := &cobra.Command{ @@ -79,15 +79,12 @@ func logoutRun(opts *LogoutOptions) error { if len(candidates) == 1 { hostname = candidates[0] } else { - //nolint:staticcheck // SA1019: prompt.SurveyAskOne is deprecated: use Prompter - err = prompt.SurveyAskOne(&survey.Select{ - Message: "What account do you want to log out of?", - Options: candidates, - }, &hostname) - + selected, err := opts.Prompter.Select( + "What account to you want to log out of?", "", candidates) if err != nil { return fmt.Errorf("could not prompt: %w", err) } + hostname = candidates[selected] } } else { var found bool diff --git a/pkg/cmd/auth/logout/logout_test.go b/pkg/cmd/auth/logout/logout_test.go index 71bca8cd7..0a5e32f7c 100644 --- a/pkg/cmd/auth/logout/logout_test.go +++ b/pkg/cmd/auth/logout/logout_test.go @@ -7,10 +7,10 @@ import ( "testing" "github.com/cli/cli/v2/internal/config" + "github.com/cli/cli/v2/internal/prompter" "github.com/cli/cli/v2/pkg/cmdutil" "github.com/cli/cli/v2/pkg/httpmock" "github.com/cli/cli/v2/pkg/iostreams" - "github.com/cli/cli/v2/pkg/prompt" "github.com/google/shlex" "github.com/stretchr/testify/assert" ) @@ -92,21 +92,23 @@ func Test_NewCmdLogout(t *testing.T) { func Test_logoutRun_tty(t *testing.T) { tests := []struct { - name string - opts *LogoutOptions - askStubs func(*prompt.AskStubber) - cfgHosts []string - wantHosts string - wantErrOut *regexp.Regexp - wantErr string + name string + opts *LogoutOptions + prompterStubs func(*prompter.PrompterMock) + cfgHosts []string + wantHosts string + wantErrOut *regexp.Regexp + wantErr string }{ { name: "no arguments, multiple hosts", opts: &LogoutOptions{}, cfgHosts: []string{"cheryl.mason", "github.com"}, wantHosts: "cheryl.mason:\n oauth_token: abc123\n", - askStubs: func(as *prompt.AskStubber) { - as.StubPrompt("What account do you want to log out of?").AnswerWith("github.com") + prompterStubs: func(pm *prompter.PrompterMock) { + pm.SelectFunc = func(_, _ string, opts []string) (int, error) { + return prompter.IndexFor(opts, "github.com") + } }, wantErrOut: regexp.MustCompile(`Logged out of github.com account 'cybilb'`), }, @@ -158,11 +160,11 @@ func Test_logoutRun_tty(t *testing.T) { return &http.Client{Transport: reg}, nil } - //nolint:staticcheck // SA1019: prompt.NewAskStubber is deprecated: use PrompterMock - as := prompt.NewAskStubber(t) - if tt.askStubs != nil { - tt.askStubs(as) + pm := &prompter.PrompterMock{} + if tt.prompterStubs != nil { + tt.prompterStubs(pm) } + tt.opts.Prompter = pm err := logoutRun(tt.opts) if tt.wantErr != "" { diff --git a/pkg/cmd/auth/refresh/refresh.go b/pkg/cmd/auth/refresh/refresh.go index 31737e976..7b1142e7e 100644 --- a/pkg/cmd/auth/refresh/refresh.go +++ b/pkg/cmd/auth/refresh/refresh.go @@ -5,15 +5,12 @@ import ( "net/http" "strings" - "github.com/AlecAivazis/survey/v2" "github.com/MakeNowJust/heredoc" "github.com/cli/cli/v2/internal/authflow" "github.com/cli/cli/v2/internal/config" - "github.com/cli/cli/v2/internal/prompter" "github.com/cli/cli/v2/pkg/cmd/auth/shared" "github.com/cli/cli/v2/pkg/cmdutil" "github.com/cli/cli/v2/pkg/iostreams" - "github.com/cli/cli/v2/pkg/prompt" "github.com/spf13/cobra" ) @@ -21,7 +18,7 @@ type RefreshOptions struct { IO *iostreams.IOStreams Config func() (config.Config, error) httpClient *http.Client - Prompter prompter.Prompter + Prompter shared.Prompt MainExecutable string @@ -97,15 +94,11 @@ func refreshRun(opts *RefreshOptions) error { if len(candidates) == 1 { hostname = candidates[0] } else { - //nolint:staticcheck // SA1019: prompt.SurveyAskOne is deprecated: use Prompter - err := prompt.SurveyAskOne(&survey.Select{ - Message: "What account do you want to refresh auth for?", - Options: candidates, - }, &hostname) - + selected, err := opts.Prompter.Select("What account do you want to refresh auth for?", "", candidates) if err != nil { return fmt.Errorf("could not prompt: %w", err) } + hostname = candidates[selected] } } else { var found bool diff --git a/pkg/cmd/auth/refresh/refresh_test.go b/pkg/cmd/auth/refresh/refresh_test.go index 0cb754117..ffacb22d9 100644 --- a/pkg/cmd/auth/refresh/refresh_test.go +++ b/pkg/cmd/auth/refresh/refresh_test.go @@ -8,10 +8,10 @@ import ( "testing" "github.com/cli/cli/v2/internal/config" + "github.com/cli/cli/v2/internal/prompter" "github.com/cli/cli/v2/pkg/cmdutil" "github.com/cli/cli/v2/pkg/httpmock" "github.com/cli/cli/v2/pkg/iostreams" - "github.com/cli/cli/v2/pkg/prompt" "github.com/google/shlex" "github.com/stretchr/testify/assert" ) @@ -132,14 +132,14 @@ type authArgs struct { func Test_refreshRun(t *testing.T) { tests := []struct { - name string - opts *RefreshOptions - askStubs func(*prompt.AskStubber) - cfgHosts []string - oldScopes string - wantErr string - nontty bool - wantAuthArgs authArgs + name string + opts *RefreshOptions + prompterStubs func(*prompter.PrompterMock) + cfgHosts []string + oldScopes string + wantErr string + nontty bool + wantAuthArgs authArgs }{ { name: "no hosts configured", @@ -193,8 +193,10 @@ func Test_refreshRun(t *testing.T) { opts: &RefreshOptions{ Hostname: "", }, - askStubs: func(as *prompt.AskStubber) { - as.StubPrompt("What account do you want to refresh auth for?").AnswerWith("github.com") + prompterStubs: func(pm *prompter.PrompterMock) { + pm.SelectFunc = func(_, _ string, opts []string) (int, error) { + return prompter.IndexFor(opts, "github.com") + } }, wantAuthArgs: authArgs{ hostname: "github.com", @@ -272,11 +274,11 @@ func Test_refreshRun(t *testing.T) { ) tt.opts.httpClient = &http.Client{Transport: httpReg} - //nolint:staticcheck // SA1019: prompt.NewAskStubber is deprecated: use PrompterMock - as := prompt.NewAskStubber(t) - if tt.askStubs != nil { - tt.askStubs(as) + pm := &prompter.PrompterMock{} + if tt.prompterStubs != nil { + tt.prompterStubs(pm) } + tt.opts.Prompter = pm err := refreshRun(tt.opts) if tt.wantErr != "" { diff --git a/pkg/cmd/auth/shared/git_credential.go b/pkg/cmd/auth/shared/git_credential.go index e6cbe3c29..9c9a1cf6b 100644 --- a/pkg/cmd/auth/shared/git_credential.go +++ b/pkg/cmd/auth/shared/git_credential.go @@ -10,14 +10,13 @@ import ( "github.com/MakeNowJust/heredoc" "github.com/cli/cli/v2/git" "github.com/cli/cli/v2/internal/ghinstance" - "github.com/cli/cli/v2/internal/prompter" "github.com/cli/cli/v2/internal/run" "github.com/google/shlex" ) type GitCredentialFlow struct { Executable string - Prompter prompter.Prompter + Prompter Prompt shouldSetup bool helper string diff --git a/pkg/cmd/auth/shared/login_flow.go b/pkg/cmd/auth/shared/login_flow.go index 16386c20c..9cc9d9865 100644 --- a/pkg/cmd/auth/shared/login_flow.go +++ b/pkg/cmd/auth/shared/login_flow.go @@ -10,7 +10,6 @@ import ( "github.com/cli/cli/v2/api" "github.com/cli/cli/v2/internal/authflow" "github.com/cli/cli/v2/internal/ghinstance" - "github.com/cli/cli/v2/internal/prompter" "github.com/cli/cli/v2/pkg/cmd/ssh-key/add" "github.com/cli/cli/v2/pkg/iostreams" "github.com/cli/cli/v2/pkg/ssh" @@ -34,7 +33,7 @@ type LoginOptions struct { Scopes []string Executable string GitProtocol string - Prompter prompter.Prompter + Prompter Prompt sshContext ssh.Context } diff --git a/pkg/cmd/auth/shared/prompt.go b/pkg/cmd/auth/shared/prompt.go new file mode 100644 index 000000000..c0d47372c --- /dev/null +++ b/pkg/cmd/auth/shared/prompt.go @@ -0,0 +1,10 @@ +package shared + +type Prompt interface { + Select(string, string, []string) (int, error) + Confirm(string, bool) (bool, error) + InputHostname() (string, error) + AuthToken() (string, error) + Input(string, string) (string, error) + Password(string) (string, error) +}