1 /* 2 Copyright 2016 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 term 18 19 import ( 20 "io" 21 "os" 22 23 "k8s.io/cli-runtime/pkg/printers" 24 25 "github.com/moby/term" 26 27 "k8s.io/kubectl/pkg/util/interrupt" 28 ) 29 30 // SafeFunc is a function to be invoked by TTY. 31 type SafeFunc func() error 32 33 // TTY helps invoke a function and preserve the state of the terminal, even if the process is 34 // terminated during execution. It also provides support for terminal resizing for remote command 35 // execution/attachment. 36 type TTY struct { 37 // In is a reader representing stdin. It is a required field. 38 In io.Reader 39 // Out is a writer representing stdout. It must be set to support terminal resizing. It is an 40 // optional field. 41 Out io.Writer 42 // Raw is true if the terminal should be set raw. 43 Raw bool 44 // TryDev indicates the TTY should try to open /dev/tty if the provided input 45 // is not a file descriptor. 46 TryDev bool 47 // Parent is an optional interrupt handler provided to this function - if provided 48 // it will be invoked after the terminal state is restored. If it is not provided, 49 // a signal received during the TTY will result in os.Exit(0) being invoked. 50 Parent *interrupt.Handler 51 52 // sizeQueue is set after a call to MonitorSize() and is used to monitor SIGWINCH signals when the 53 // user's terminal resizes. 54 sizeQueue *sizeQueue 55 } 56 57 // IsTerminalIn returns true if t.In is a terminal. Does not check /dev/tty 58 // even if TryDev is set. 59 func (t TTY) IsTerminalIn() bool { 60 return printers.IsTerminal(t.In) 61 } 62 63 // IsTerminalOut returns true if t.Out is a terminal. Does not check /dev/tty 64 // even if TryDev is set. 65 func (t TTY) IsTerminalOut() bool { 66 return printers.IsTerminal(t.Out) 67 } 68 69 // IsTerminal returns whether the passed object is a terminal or not. 70 // Deprecated: use printers.IsTerminal instead. 71 var IsTerminal = printers.IsTerminal 72 73 // AllowsColorOutput returns true if the specified writer is a terminal and 74 // the process environment indicates color output is supported and desired. 75 // Deprecated: use printers.AllowsColorOutput instead. 76 var AllowsColorOutput = printers.AllowsColorOutput 77 78 // Safe invokes the provided function and will attempt to ensure that when the 79 // function returns (or a termination signal is sent) that the terminal state 80 // is reset to the condition it was in prior to the function being invoked. If 81 // t.Raw is true the terminal will be put into raw mode prior to calling the function. 82 // If the input file descriptor is not a TTY and TryDev is true, the /dev/tty file 83 // will be opened (if available). 84 func (t TTY) Safe(fn SafeFunc) error { 85 inFd, isTerminal := term.GetFdInfo(t.In) 86 87 if !isTerminal && t.TryDev { 88 if f, err := os.Open("/dev/tty"); err == nil { 89 defer f.Close() 90 inFd = f.Fd() 91 isTerminal = term.IsTerminal(inFd) 92 } 93 } 94 if !isTerminal { 95 return fn() 96 } 97 98 var state *term.State 99 var err error 100 if t.Raw { 101 state, err = term.MakeRaw(inFd) 102 } else { 103 state, err = term.SaveState(inFd) 104 } 105 if err != nil { 106 return err 107 } 108 return interrupt.Chain(t.Parent, func() { 109 if t.sizeQueue != nil { 110 t.sizeQueue.stop() 111 } 112 113 term.RestoreTerminal(inFd, state) 114 }).Run(fn) 115 } 116