|
| 1 | +// |
| 2 | +// Copyright 2022 The Sigstore 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 | +package streams |
| 17 | + |
| 18 | +import ( |
| 19 | + "fmt" |
| 20 | + "io" |
| 21 | + "os" |
| 22 | + |
| 23 | + "github.com/mattn/go-tty" |
| 24 | +) |
| 25 | + |
| 26 | +type Streams struct { |
| 27 | + In io.Reader |
| 28 | + Out io.Writer |
| 29 | + Err io.Writer |
| 30 | + |
| 31 | + TTYIn io.Reader |
| 32 | + TTYOut io.Writer |
| 33 | + |
| 34 | + close []func() error |
| 35 | +} |
| 36 | + |
| 37 | +func New(logPath string) *Streams { |
| 38 | + s := &Streams{ |
| 39 | + In: os.Stdin, |
| 40 | + Out: os.Stdout, |
| 41 | + Err: os.Stderr, |
| 42 | + } |
| 43 | + |
| 44 | + if logPath != "" { |
| 45 | + // Since Git eats both stdout and stderr, we don't have a good way of |
| 46 | + // getting error information back from clients if things go wrong. |
| 47 | + // As a janky way to preserve error message, tee stderr to |
| 48 | + // a temp file. |
| 49 | + if f, err := os.Create(logPath); err == nil { |
| 50 | + s.close = append(s.close, f.Close) |
| 51 | + s.Err = io.MultiWriter(s.Err, f) |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + // A TTY may not be available in all environments (e.g. in CI), so only |
| 56 | + // set the input/output if we can actually open it. |
| 57 | + tty, err := tty.Open() |
| 58 | + if err == nil { |
| 59 | + s.close = append(s.close, tty.Close) |
| 60 | + s.TTYIn = tty.Input() |
| 61 | + s.TTYOut = tty.Output() |
| 62 | + } else { |
| 63 | + // If we can't connect to a TTY, fall back to stderr for output (which |
| 64 | + // will also log to file if GITSIGN_LOG is set). |
| 65 | + s.TTYOut = s.Err |
| 66 | + } |
| 67 | + return s |
| 68 | +} |
| 69 | + |
| 70 | +func (s *Streams) Wrap(fn func() error) error { |
| 71 | + // Log any panics to ttyout, since otherwise they will be lost to os.Stderr. |
| 72 | + defer func() { |
| 73 | + if r := recover(); r != nil { |
| 74 | + fmt.Fprintln(s.TTYOut, r) |
| 75 | + } |
| 76 | + }() |
| 77 | + |
| 78 | + if err := fn(); err != nil { |
| 79 | + fmt.Fprintln(s.TTYOut, err) |
| 80 | + return err |
| 81 | + } |
| 82 | + return nil |
| 83 | +} |
| 84 | + |
| 85 | +func (s *Streams) Close() error { |
| 86 | + for _, fn := range s.close { |
| 87 | + if err := fn(); err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + } |
| 91 | + return nil |
| 92 | +} |
0 commit comments