From 403f8bddf3c0698234d45e2a67210ca1c2348e2f Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Mon, 27 Jul 2020 18:23:35 -0700 Subject: [PATCH 1/2] Current: allow stdin to be redirected In case we have stdin redirected, we still have a terminal which can be obtained from stdout or stderr. This is what this commit does. This should allow something like this to work: > ctr run --rm --tty docker.io/library/hello-world:latest xxx < /dev/null NB: in case all three are redirected, but we still have a controlling tty, we can easily get it by opening /dev/tty, but then it should be closed, and it's not quite clear by whom and when, so this is left as a home exersize for the reader. Signed-off-by: Kir Kolyshkin --- console.go | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/console.go b/console.go index 6a36d14..dd09233 100644 --- a/console.go +++ b/console.go @@ -61,15 +61,20 @@ type WinSize struct { y uint16 } -// Current returns the current processes console -func Current() Console { - c, err := ConsoleFromFile(os.Stdin) - if err != nil { - // stdin should always be a console for the design - // of this function - panic(err) +// Current returns the current process' console +func Current() (c Console) { + var err error + // Usually all three streams (stdin, stdout, and stderr) + // are open to the same console, but some might be redirected, + // so try all three. + for _, s := range []*os.File{os.Stderr, os.Stdout, os.Stdin} { + if c, err = ConsoleFromFile(s); err == nil { + return c + } } - return c + // One of the std streams should always be a console + // for the design of this function. + panic(err) } // ConsoleFromFile returns a console using the provided file From a8d476471efb3c345d775ab2870b6d6f0359048f Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Mon, 27 Jul 2020 18:57:07 -0700 Subject: [PATCH 2/2] travis: update to supported golang versions Signed-off-by: Kir Kolyshkin --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 16827ec..ff86c46 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ language: go go: - - "1.12.x" - "1.13.x" + - "1.14.x" go_import_path: github.com/containerd/console