From 6c7191355aaf2fd63999318ae2a5b8d358cba4bc Mon Sep 17 00:00:00 2001 From: Jesse Kinkead Date: Mon, 5 Sep 2022 15:33:09 -0700 Subject: [PATCH 1/2] Use the same output for surveys as for logs. Add a package variable to hold the survey output writer and a setter for it. Add a helper to generate a survey option using the writer for stdout. Initialize the survey output to be the same as for the log module. --- cmd/saml2aws/main.go | 2 ++ pkg/prompter/survey.go | 27 +++++++++++++++++++++------ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/cmd/saml2aws/main.go b/cmd/saml2aws/main.go index d6ee181e3..bc15a7594 100644 --- a/cmd/saml2aws/main.go +++ b/cmd/saml2aws/main.go @@ -12,6 +12,7 @@ import ( "github.com/sirupsen/logrus" "github.com/versent/saml2aws/v2/cmd/saml2aws/commands" "github.com/versent/saml2aws/v2/pkg/flags" + "github.com/versent/saml2aws/v2/pkg/prompter" ) var ( @@ -46,6 +47,7 @@ func buildCmdList(s kingpin.Settings) (target *[]string) { func main() { log.SetOutput(os.Stderr) + prompter.SetOutputWriter(os.Stderr) log.SetFlags(0) logrus.SetOutput(os.Stderr) diff --git a/pkg/prompter/survey.go b/pkg/prompter/survey.go index 686a835fb..35485d539 100644 --- a/pkg/prompter/survey.go +++ b/pkg/prompter/survey.go @@ -3,14 +3,29 @@ package prompter import ( "errors" "fmt" + "os" survey "github.com/AlecAivazis/survey/v2" + survey_terminal "github.com/AlecAivazis/survey/v2/terminal" ) +// outputWriter is where for all prompts will be printed. Defaults to os.Stder. +var outputWriter survey_terminal.FileWriter = os.Stderr + // CliPrompter used to prompt for cli input type CliPrompter struct { } +// SetOutputWriter sets the output writer to use for all survey operations +func SetOutputWriter(writer survey_terminal.FileWriter) { + outputWriter = writer +} + +// stdioOption returns the IO option to use for survey functions +func stdioOption() survey.AskOpt { + return survey.WithStdio(os.Stdin, outputWriter, os.Stderr) +} + // NewCli builds a new cli prompter func NewCli() *CliPrompter { return &CliPrompter{} @@ -22,7 +37,7 @@ func (cli *CliPrompter) RequestSecurityCode(pattern string) string { prompt := &survey.Input{ Message: fmt.Sprintf("Security Token [%s]", pattern), } - _ = survey.AskOne(prompt, &token, survey.WithValidator(survey.Required)) + _ = survey.AskOne(prompt, &token, survey.WithValidator(survey.Required), stdioOption()) return token } @@ -34,7 +49,7 @@ func (cli *CliPrompter) ChooseWithDefault(pr string, defaultValue string, option Options: options, Default: defaultValue, } - _ = survey.AskOne(prompt, &selected, survey.WithValidator(survey.Required)) + _ = survey.AskOne(prompt, &selected, survey.WithValidator(survey.Required), stdioOption()) // return the selected element index for i, option := range options { @@ -52,7 +67,7 @@ func (cli *CliPrompter) Choose(pr string, options []string) int { Message: pr, Options: options, } - _ = survey.AskOne(prompt, &selected, survey.WithValidator(survey.Required)) + _ = survey.AskOne(prompt, &selected, survey.WithValidator(survey.Required), stdioOption()) // return the selected element index for i, option := range options { @@ -70,7 +85,7 @@ func (cli *CliPrompter) String(pr string, defaultValue string) string { Message: pr, Default: defaultValue, } - _ = survey.AskOne(prompt, &val) + _ = survey.AskOne(prompt, &val, stdioOption()) return val } @@ -80,7 +95,7 @@ func (cli *CliPrompter) StringRequired(pr string) string { prompt := &survey.Input{ Message: pr, } - _ = survey.AskOne(prompt, &val, survey.WithValidator(survey.Required)) + _ = survey.AskOne(prompt, &val, survey.WithValidator(survey.Required), stdioOption()) return val } @@ -90,6 +105,6 @@ func (cli *CliPrompter) Password(pr string) string { prompt := &survey.Password{ Message: pr, } - _ = survey.AskOne(prompt, &val) + _ = survey.AskOne(prompt, &val, stdioOption()) return val } From 9b333aabe52ac28cb2d5b26bad597e88283ac168 Mon Sep 17 00:00:00 2001 From: Mark Lee Date: Wed, 30 Aug 2023 16:06:50 -0700 Subject: [PATCH 2/2] chore: fix typo --- pkg/prompter/survey.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/prompter/survey.go b/pkg/prompter/survey.go index 30d485806..8b1b816bb 100644 --- a/pkg/prompter/survey.go +++ b/pkg/prompter/survey.go @@ -9,7 +9,7 @@ import ( survey_terminal "github.com/AlecAivazis/survey/v2/terminal" ) -// outputWriter is where for all prompts will be printed. Defaults to os.Stder. +// outputWriter is where for all prompts will be printed. Defaults to os.Stderr. var outputWriter survey_terminal.FileWriter = os.Stderr // CliPrompter used to prompt for cli input