term.go 3.3 KB

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