use prompter in secret set

This commit is contained in:
Nate Smith 2023-08-16 21:26:57 -05:00
parent f2e5ad6dcd
commit fc73c16fe8
2 changed files with 14 additions and 12 deletions

View file

@ -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
}

View file

@ -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")