Skip to content

Commit

Permalink
Merge pull request #884 from jkinkead/survey_to_log_stream
Browse files Browse the repository at this point in the history
Use the same output for surveys as for logs.
  • Loading branch information
mapkon authored Nov 1, 2023
2 parents 7cc41b5 + 959bc67 commit 4ac5dca
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
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.
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
}

0 comments on commit 4ac5dca

Please sign in to comment.