Skip to content

Commit

Permalink
fix: don't print to stdout when initializing console fails on Windows
Browse files Browse the repository at this point in the history
When stdin/err/out are redirected, console mode can't be changed.
That's often expected & acceptable however, so we shouldn't print
errors to stdout.
  • Loading branch information
muesli committed Oct 21, 2022
1 parent f6c071f commit e4a9c91
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions console_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ var (
ErrNotImplemented = errors.New("not implemented")
)

func (m *master) initStdios() {
func (m *master) initStdios() []error {
var errs []error

m.in = windows.Handle(os.Stdin.Fd())
if err := windows.GetConsoleMode(m.in, &m.inMode); err == nil {
// Validate that windows.ENABLE_VIRTUAL_TERMINAL_INPUT is supported, but do not set it.
Expand All @@ -40,7 +42,7 @@ func (m *master) initStdios() {
// remembers invalid bits on input handles.
windows.SetConsoleMode(m.in, m.inMode)
} else {
fmt.Printf("failed to get console mode for stdin: %v\n", err)
errs = append(errs, fmt.Errorf("unable to get console mode for stdin: %w", err))
}

m.out = windows.Handle(os.Stdout.Fd())
Expand All @@ -51,7 +53,7 @@ func (m *master) initStdios() {
windows.SetConsoleMode(m.out, m.outMode)
}
} else {
fmt.Printf("failed to get console mode for stdout: %v\n", err)
errs = append(errs, fmt.Errorf("unable to get console mode for stdout: %w", err))
}

m.err = windows.Handle(os.Stderr.Fd())
Expand All @@ -62,8 +64,10 @@ func (m *master) initStdios() {
windows.SetConsoleMode(m.err, m.errMode)
}
} else {
fmt.Printf("failed to get console mode for stderr: %v\n", err)
errs = append(errs, fmt.Errorf("unable to get console mode for stderr: %w", err))
}

return errs
}

type master struct {
Expand Down Expand Up @@ -211,6 +215,8 @@ func newMaster(f File) (Console, error) {
return nil, errors.New("creating a console from a file is not supported on windows")
}
m := &master{}
m.initStdios()

// ignore console mode warnings when in/out is redirected
_ = m.initStdios()
return m, nil
}

0 comments on commit e4a9c91

Please sign in to comment.