Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 1 | // Copyright 2017 Google Inc. All rights reserved. |
| 2 | // |
| 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 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package build |
| 16 | |
| 17 | import ( |
| 18 | "context" |
| 19 | "io" |
| 20 | "os" |
| 21 | |
| 22 | "android/soong/ui/logger" |
| 23 | ) |
| 24 | |
| 25 | type StdioInterface interface { |
| 26 | Stdin() io.Reader |
| 27 | Stdout() io.Writer |
| 28 | Stderr() io.Writer |
| 29 | } |
| 30 | |
| 31 | type StdioImpl struct{} |
| 32 | |
| 33 | func (StdioImpl) Stdin() io.Reader { return os.Stdin } |
| 34 | func (StdioImpl) Stdout() io.Writer { return os.Stdout } |
| 35 | func (StdioImpl) Stderr() io.Writer { return os.Stderr } |
| 36 | |
| 37 | var _ StdioInterface = StdioImpl{} |
| 38 | |
| 39 | type customStdio struct { |
| 40 | stdin io.Reader |
| 41 | stdout io.Writer |
| 42 | stderr io.Writer |
| 43 | } |
| 44 | |
| 45 | func NewCustomStdio(stdin io.Reader, stdout, stderr io.Writer) StdioInterface { |
| 46 | return customStdio{stdin, stdout, stderr} |
| 47 | } |
| 48 | |
| 49 | func (c customStdio) Stdin() io.Reader { return c.stdin } |
| 50 | func (c customStdio) Stdout() io.Writer { return c.stdout } |
| 51 | func (c customStdio) Stderr() io.Writer { return c.stderr } |
| 52 | |
| 53 | var _ StdioInterface = customStdio{} |
| 54 | |
| 55 | // Context combines a context.Context, logger.Logger, and StdIO redirection. |
| 56 | // These all are agnostic of the current build, and may be used for multiple |
| 57 | // builds, while the Config objects contain per-build information. |
| 58 | type Context *ContextImpl |
| 59 | type ContextImpl struct { |
| 60 | context.Context |
| 61 | logger.Logger |
| 62 | |
| 63 | StdioInterface |
| 64 | } |