Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the same output for surveys as for logs. #884

Merged
merged 5 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/saml2aws/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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)

Expand Down
27 changes: 21 additions & 6 deletions pkg/prompter/survey.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: typo

Suggested change
// 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
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{}
Expand All @@ -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
}

Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
}

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

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