From fc73c16fe82c925e72b8f017172315456922a9ce Mon Sep 17 00:00:00 2001 From: Nate Smith Date: Wed, 16 Aug 2023 21:26:57 -0500 Subject: [PATCH] use prompter in secret set --- pkg/cmd/secret/set/set.go | 14 +++++++------- pkg/cmd/secret/set/set_test.go | 12 +++++++----- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/pkg/cmd/secret/set/set.go b/pkg/cmd/secret/set/set.go index 86dd35258..95f80f8b1 100644 --- a/pkg/cmd/secret/set/set.go +++ b/pkg/cmd/secret/set/set.go @@ -9,7 +9,6 @@ import ( "os" "strings" - "github.com/AlecAivazis/survey/v2" "github.com/MakeNowJust/heredoc" "github.com/cli/cli/v2/api" "github.com/cli/cli/v2/internal/config" @@ -17,7 +16,6 @@ import ( "github.com/cli/cli/v2/pkg/cmd/secret/shared" "github.com/cli/cli/v2/pkg/cmdutil" "github.com/cli/cli/v2/pkg/iostreams" - "github.com/cli/cli/v2/pkg/prompt" "github.com/hashicorp/go-multierror" "github.com/joho/godotenv" "github.com/spf13/cobra" @@ -29,6 +27,7 @@ type SetOptions struct { IO *iostreams.IOStreams Config func() (config.Config, error) BaseRepo func() (ghrepo.Interface, error) + Prompter iprompter RandomOverride func() io.Reader @@ -44,11 +43,16 @@ type SetOptions struct { Application string } +type iprompter interface { + Password(string) (string, error) +} + func NewCmdSet(f *cmdutil.Factory, runF func(*SetOptions) error) *cobra.Command { opts := &SetOptions{ IO: f.IOStreams, Config: f.Config, HttpClient: f.HttpClient, + Prompter: f.Prompter, } cmd := &cobra.Command{ @@ -375,11 +379,7 @@ func getBody(opts *SetOptions) ([]byte, error) { } if opts.IO.CanPrompt() { - var bodyInput string - //nolint:staticcheck // SA1019: prompt.SurveyAskOne is deprecated: use Prompter - err := prompt.SurveyAskOne(&survey.Password{ - Message: "Paste your secret", - }, &bodyInput) + bodyInput, err := opts.Prompter.Password("Paste your secret") if err != nil { return nil, err } diff --git a/pkg/cmd/secret/set/set_test.go b/pkg/cmd/secret/set/set_test.go index 592527c5f..376ef2d53 100644 --- a/pkg/cmd/secret/set/set_test.go +++ b/pkg/cmd/secret/set/set_test.go @@ -12,11 +12,11 @@ import ( "github.com/MakeNowJust/heredoc" "github.com/cli/cli/v2/internal/config" "github.com/cli/cli/v2/internal/ghrepo" + "github.com/cli/cli/v2/internal/prompter" "github.com/cli/cli/v2/pkg/cmd/secret/shared" "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" ) @@ -595,12 +595,14 @@ func Test_getBodyPrompt(t *testing.T) { ios.SetStdinTTY(true) ios.SetStdoutTTY(true) - //nolint:staticcheck // SA1019: prompt.NewAskStubber is deprecated: use PrompterMock - as := prompt.NewAskStubber(t) - as.StubPrompt("Paste your secret").AnswerWith("cool secret") + pm := prompter.NewMockPrompter(t) + pm.RegisterPassword("Paste your secret", func(_ string) (string, error) { + return "cool secret", nil + }) body, err := getBody(&SetOptions{ - IO: ios, + IO: ios, + Prompter: pm, }) assert.NoError(t, err) assert.Equal(t, string(body), "cool secret")