From 898d2763c5430efaf808d7ca8c97b6d792c46c46 Mon Sep 17 00:00:00 2001 From: Sachin Joshi Date: Thu, 5 Mar 2015 16:41:48 -0800 Subject: [PATCH] Move windows console specific implementation in sub package Signed-off-by: Sachin Joshi --- docker/docker.go | 15 +++++---- docker/log.go | 10 +++--- pkg/term/term_windows.go | 33 +++++++++++++------ pkg/term/{ => winconsole}/console_windows.go | 15 ++++++--- .../{ => winconsole}/console_windows_test.go | 2 +- pkg/term/{ => winconsole}/term_emulator.go | 2 +- .../{ => winconsole}/term_emulator_test.go | 2 +- 7 files changed, 50 insertions(+), 29 deletions(-) rename pkg/term/{ => winconsole}/console_windows.go (99%) rename pkg/term/{ => winconsole}/console_windows_test.go (99%) rename pkg/term/{ => winconsole}/term_emulator.go (99%) rename pkg/term/{ => winconsole}/term_emulator_test.go (99%) diff --git a/docker/docker.go b/docker/docker.go index d55e84b8ec..1d2a45c9de 100644 --- a/docker/docker.go +++ b/docker/docker.go @@ -26,6 +26,11 @@ const ( ) func main() { + // Set terminal emulation based on platform as required. + stdout, stderr, stdin := term.StdStreams() + + initLogging(stderr) + if reexec.Init() { return } @@ -43,20 +48,16 @@ func main() { if err != nil { log.Fatalf("Unable to parse logging level: %s", *flLogLevel) } - initLogging(lvl) + setLogLevel(lvl) } else { - initLogging(log.InfoLevel) + setLogLevel(log.InfoLevel) } - // Set terminal emulation based on platform as required. - stdout, stderr, stdin := term.StdStreams() - log.SetOutput(stderr) - // -D, --debug, -l/--log-level=debug processing // When/if -D is removed this block can be deleted if *flDebug { os.Setenv("DEBUG", "1") - initLogging(log.DebugLevel) + setLogLevel(log.DebugLevel) } if len(flHosts) == 0 { diff --git a/docker/log.go b/docker/log.go index cdbbd4408f..0dd9a70eeb 100644 --- a/docker/log.go +++ b/docker/log.go @@ -1,12 +1,14 @@ package main import ( - "os" - log "github.com/Sirupsen/logrus" + "io" ) -func initLogging(lvl log.Level) { - log.SetOutput(os.Stderr) +func setLogLevel(lvl log.Level) { log.SetLevel(lvl) } + +func initLogging(stderr io.Writer) { + log.SetOutput(stderr) +} diff --git a/pkg/term/term_windows.go b/pkg/term/term_windows.go index ea4ba5376e..711f7446d5 100644 --- a/pkg/term/term_windows.go +++ b/pkg/term/term_windows.go @@ -1,7 +1,11 @@ // +build windows - package term +import ( + "github.com/docker/docker/pkg/term/winconsole" + "io" +) + // State holds the console mode for the terminal. type State struct { mode uint32 @@ -18,8 +22,8 @@ type Winsize struct { // GetWinsize gets the window size of the given terminal func GetWinsize(fd uintptr) (*Winsize, error) { ws := &Winsize{} - var info *CONSOLE_SCREEN_BUFFER_INFO - info, err := GetConsoleScreenBufferInfo(fd) + var info *winconsole.CONSOLE_SCREEN_BUFFER_INFO + info, err := winconsole.GetConsoleScreenBufferInfo(fd) if err != nil { return nil, err } @@ -41,19 +45,19 @@ func SetWinsize(fd uintptr, ws *Winsize) error { // IsTerminal returns true if the given file descriptor is a terminal. func IsTerminal(fd uintptr) bool { - _, e := GetConsoleMode(fd) + _, e := winconsole.GetConsoleMode(fd) return e == nil } // RestoreTerminal restores the terminal connected to the given file descriptor to a // previous state. func RestoreTerminal(fd uintptr, state *State) error { - return SetConsoleMode(fd, state.mode) + return winconsole.SetConsoleMode(fd, state.mode) } // SaveState saves the state of the given console func SaveState(fd uintptr) (*State, error) { - mode, e := GetConsoleMode(fd) + mode, e := winconsole.GetConsoleMode(fd) if e != nil { return nil, e } @@ -63,9 +67,9 @@ func SaveState(fd uintptr) (*State, error) { // DisableEcho disbales the echo for given file descriptor and returns previous state // see http://msdn.microsoft.com/en-us/library/windows/desktop/ms683462(v=vs.85).aspx for these flag settings func DisableEcho(fd uintptr, state *State) error { - state.mode &^= (ENABLE_ECHO_INPUT) - state.mode |= (ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT) - return SetConsoleMode(fd, state.mode) + state.mode &^= (winconsole.ENABLE_ECHO_INPUT) + state.mode |= (winconsole.ENABLE_PROCESSED_INPUT | winconsole.ENABLE_LINE_INPUT) + return winconsole.SetConsoleMode(fd, state.mode) } // SetRawTerminal puts the terminal connected to the given file descriptor into raw @@ -96,9 +100,18 @@ func MakeRaw(fd uintptr) (*State, error) { // When all are enabled, the application is said to be in "cooked" mode, which means that most of the processing is handled for the application. // When all are disabled, the application is in "raw" mode, which means that input is unfiltered and any processing is left to the application. state.mode = 0 - err = SetConsoleMode(fd, state.mode) + err = winconsole.SetConsoleMode(fd, state.mode) if err != nil { return nil, err } return state, nil } + +// GetHandleInfo returns file descriptor and bool indicating whether the file is a terminal +func GetHandleInfo(in interface{}) (uintptr, bool) { + return winconsole.GetHandleInfo(in) +} + +func StdStreams() (stdOut io.Writer, stdErr io.Writer, stdIn io.ReadCloser) { + return winconsole.StdStreams() +} diff --git a/pkg/term/console_windows.go b/pkg/term/winconsole/console_windows.go similarity index 99% rename from pkg/term/console_windows.go rename to pkg/term/winconsole/console_windows.go index 53069201c7..8121aee757 100644 --- a/pkg/term/console_windows.go +++ b/pkg/term/winconsole/console_windows.go @@ -1,6 +1,6 @@ // +build windows -package term +package winconsole import ( "bytes" @@ -415,7 +415,7 @@ func getNumberOfChars(fromCoord COORD, toCoord COORD, screenSize COORD) uint32 { return 0 } -func clearDisplayRect(fileDesc uintptr, fillChar byte, attributes WORD, fromCoord COORD, toCoord COORD, windowSize COORD) (bool, uint32, error) { +func clearDisplayRect(fileDesc uintptr, fillChar rune, attributes WORD, fromCoord COORD, toCoord COORD, windowSize COORD) (bool, uint32, error) { var writeRegion SMALL_RECT writeRegion.Top = fromCoord.Y writeRegion.Left = fromCoord.X @@ -429,7 +429,7 @@ func clearDisplayRect(fileDesc uintptr, fillChar byte, attributes WORD, fromCoor if size > 0 { buffer := make([]CHAR_INFO, size) for i := 0; i < len(buffer); i++ { - buffer[i].UnicodeChar = WCHAR(string(fillChar)[0]) + buffer[i].UnicodeChar = WCHAR(fillChar) buffer[i].Attributes = attributes } @@ -445,7 +445,7 @@ func clearDisplayRect(fileDesc uintptr, fillChar byte, attributes WORD, fromCoor return true, uint32(size), nil } -func clearDisplayRange(fileDesc uintptr, fillChar byte, attributes WORD, fromCoord COORD, toCoord COORD, windowSize COORD) (bool, uint32, error) { +func clearDisplayRange(fileDesc uintptr, fillChar rune, attributes WORD, fromCoord COORD, toCoord COORD, windowSize COORD) (bool, uint32, error) { nw := uint32(0) // start and end on same line if fromCoord.Y == toCoord.Y { @@ -531,7 +531,6 @@ func getNumberOfConsoleInputEvents(fileDesc uintptr) (uint16, error) { r, _, err := getNumberOfConsoleInputEventsProc.Call(uintptr(fileDesc), uintptr(unsafe.Pointer(&n))) //If the function succeeds, the return value is nonzero if r != 0 { - //fmt.Printf("################%d #################\n", n) return uint16(n), nil } return 0, err @@ -1103,3 +1102,9 @@ func marshal(c COORD) uint32 { // works only on intel-endian machines return uint32(uint32(uint16(c.Y))<<16 | uint32(uint16(c.X))) } + +// IsTerminal returns true if the given file descriptor is a terminal. +func IsTerminal(fd uintptr) bool { + _, e := GetConsoleMode(fd) + return e == nil +} diff --git a/pkg/term/console_windows_test.go b/pkg/term/winconsole/console_windows_test.go similarity index 99% rename from pkg/term/console_windows_test.go rename to pkg/term/winconsole/console_windows_test.go index 01c25726c8..ee9d96834b 100644 --- a/pkg/term/console_windows_test.go +++ b/pkg/term/winconsole/console_windows_test.go @@ -1,6 +1,6 @@ // +build windows -package term +package winconsole import ( "fmt" diff --git a/pkg/term/term_emulator.go b/pkg/term/winconsole/term_emulator.go similarity index 99% rename from pkg/term/term_emulator.go rename to pkg/term/winconsole/term_emulator.go index 1713f428a9..13bcde81fc 100644 --- a/pkg/term/term_emulator.go +++ b/pkg/term/winconsole/term_emulator.go @@ -1,4 +1,4 @@ -package term +package winconsole import ( "io" diff --git a/pkg/term/term_emulator_test.go b/pkg/term/winconsole/term_emulator_test.go similarity index 99% rename from pkg/term/term_emulator_test.go rename to pkg/term/winconsole/term_emulator_test.go index 7a9e1abc90..7017d426fe 100644 --- a/pkg/term/term_emulator_test.go +++ b/pkg/term/winconsole/term_emulator_test.go @@ -1,4 +1,4 @@ -package term +package winconsole import ( "bytes"