...

Source file src/sigs.k8s.io/release-utils/editor/tty.go

Documentation: sigs.k8s.io/release-utils/editor

     1  /*
     2  Copyright 2020 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package editor
    18  
    19  import (
    20  	"io"
    21  	"os"
    22  
    23  	"github.com/moby/term"
    24  	"github.com/sirupsen/logrus"
    25  )
    26  
    27  // TTY helps invoke a function and preserve the state of the terminal, even if the process is
    28  // terminated during execution. It also provides support for terminal resizing for remote command
    29  // execution/attachment.
    30  type TTY struct {
    31  	// In is a reader representing stdin. It is a required field.
    32  	In io.Reader
    33  	// Out is a writer representing stdout. It must be set to support terminal resizing. It is an
    34  	// optional field.
    35  	Out io.Writer
    36  	// Raw is true if the terminal should be set raw.
    37  	Raw bool
    38  	// TryDev indicates the TTY should try to open /dev/tty if the provided input
    39  	// is not a file descriptor.
    40  	TryDev bool
    41  }
    42  
    43  // Safe invokes the provided function and will attempt to ensure that when the
    44  // function returns (or a termination signal is sent) that the terminal state
    45  // is reset to the condition it was in prior to the function being invoked. If
    46  // t.Raw is true the terminal will be put into raw mode prior to calling the function.
    47  // If the input file descriptor is not a TTY and TryDev is true, the /dev/tty file
    48  // will be opened (if available).
    49  func (t TTY) Safe(fn func() error) error {
    50  	inFd, isTerminal := term.GetFdInfo(t.In)
    51  
    52  	if !isTerminal && t.TryDev {
    53  		if f, err := os.Open("/dev/tty"); err == nil {
    54  			defer f.Close()
    55  			inFd = f.Fd()
    56  			isTerminal = term.IsTerminal(inFd)
    57  		}
    58  	}
    59  	if !isTerminal {
    60  		return fn()
    61  	}
    62  
    63  	var state *term.State
    64  	var err error
    65  	if t.Raw {
    66  		state, err = term.MakeRaw(inFd)
    67  	} else {
    68  		state, err = term.SaveState(inFd)
    69  	}
    70  	if err != nil {
    71  		return err
    72  	}
    73  
    74  	defer func() {
    75  		if err := term.RestoreTerminal(inFd, state); err != nil {
    76  			logrus.Errorf("Error resetting terminal: %v", err)
    77  		}
    78  	}()
    79  	return fn()
    80  }
    81  

View as plain text