Fix closing IOStreams.Out after finishing writing to the pager (#6210)

This commit is contained in:
Mislav Marohnić 2022-09-06 19:22:31 +02:00 committed by GitHub
parent a6636994bf
commit e2e8d697db
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 3 deletions

View file

@ -214,9 +214,9 @@ func (s *IOStreams) StartPager() error {
if err != nil {
return err
}
s.Out = &fdWriter{
fd: s.Out.Fd(),
Writer: &pagerWriter{pagedOut},
s.Out = &fdWriteCloser{
fd: s.Out.Fd(),
WriteCloser: &pagerWriter{pagedOut},
}
err = pagerCmd.Start()
if err != nil {
@ -231,6 +231,7 @@ func (s *IOStreams) StopPager() {
return
}
// if a pager was started, we're guaranteed to have a WriteCloser
_ = s.Out.(io.WriteCloser).Close()
_, _ = s.pagerProcess.Wait()
s.pagerProcess = nil
@ -521,6 +522,16 @@ func (w *fdWriter) Fd() uintptr {
return w.fd
}
// fdWriteCloser represents a wrapped stdout Writer that preserves the original file descriptor
type fdWriteCloser struct {
io.WriteCloser
fd uintptr
}
func (w *fdWriteCloser) Fd() uintptr {
return w.fd
}
// fdWriter represents a wrapped stdin ReadCloser that preserves the original file descriptor
type fdReader struct {
io.ReadCloser