Selaa lähdekoodia

Move windows console specific implementation in sub package

Signed-off-by: Sachin Joshi <sachin_jayant_joshi@hotmail.com>
Sachin Joshi 10 vuotta sitten
vanhempi
commit
898d2763c5

+ 8 - 7
docker/docker.go

@@ -26,6 +26,11 @@ const (
 )
 )
 
 
 func main() {
 func main() {
+	// Set terminal emulation based on platform as required.
+	stdout, stderr, stdin := term.StdStreams()
+
+	initLogging(stderr)
+
 	if reexec.Init() {
 	if reexec.Init() {
 		return
 		return
 	}
 	}
@@ -43,20 +48,16 @@ func main() {
 		if err != nil {
 		if err != nil {
 			log.Fatalf("Unable to parse logging level: %s", *flLogLevel)
 			log.Fatalf("Unable to parse logging level: %s", *flLogLevel)
 		}
 		}
-		initLogging(lvl)
+		setLogLevel(lvl)
 	} else {
 	} 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
 	// -D, --debug, -l/--log-level=debug processing
 	// When/if -D is removed this block can be deleted
 	// When/if -D is removed this block can be deleted
 	if *flDebug {
 	if *flDebug {
 		os.Setenv("DEBUG", "1")
 		os.Setenv("DEBUG", "1")
-		initLogging(log.DebugLevel)
+		setLogLevel(log.DebugLevel)
 	}
 	}
 
 
 	if len(flHosts) == 0 {
 	if len(flHosts) == 0 {

+ 6 - 4
docker/log.go

@@ -1,12 +1,14 @@
 package main
 package main
 
 
 import (
 import (
-	"os"
-
 	log "github.com/Sirupsen/logrus"
 	log "github.com/Sirupsen/logrus"
+	"io"
 )
 )
 
 
-func initLogging(lvl log.Level) {
-	log.SetOutput(os.Stderr)
+func setLogLevel(lvl log.Level) {
 	log.SetLevel(lvl)
 	log.SetLevel(lvl)
 }
 }
+
+func initLogging(stderr io.Writer) {
+	log.SetOutput(stderr)
+}

+ 23 - 10
pkg/term/term_windows.go

@@ -1,7 +1,11 @@
 // +build windows
 // +build windows
-
 package term
 package term
 
 
+import (
+	"github.com/docker/docker/pkg/term/winconsole"
+	"io"
+)
+
 // State holds the console mode for the terminal.
 // State holds the console mode for the terminal.
 type State struct {
 type State struct {
 	mode uint32
 	mode uint32
@@ -18,8 +22,8 @@ type Winsize struct {
 // GetWinsize gets the window size of the given terminal
 // GetWinsize gets the window size of the given terminal
 func GetWinsize(fd uintptr) (*Winsize, error) {
 func GetWinsize(fd uintptr) (*Winsize, error) {
 	ws := &Winsize{}
 	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 {
 	if err != nil {
 		return nil, err
 		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.
 // IsTerminal returns true if the given file descriptor is a terminal.
 func IsTerminal(fd uintptr) bool {
 func IsTerminal(fd uintptr) bool {
-	_, e := GetConsoleMode(fd)
+	_, e := winconsole.GetConsoleMode(fd)
 	return e == nil
 	return e == nil
 }
 }
 
 
 // RestoreTerminal restores the terminal connected to the given file descriptor to a
 // RestoreTerminal restores the terminal connected to the given file descriptor to a
 // previous state.
 // previous state.
 func RestoreTerminal(fd uintptr, state *State) error {
 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
 // SaveState saves the state of the given console
 func SaveState(fd uintptr) (*State, error) {
 func SaveState(fd uintptr) (*State, error) {
-	mode, e := GetConsoleMode(fd)
+	mode, e := winconsole.GetConsoleMode(fd)
 	if e != nil {
 	if e != nil {
 		return nil, e
 		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
 // 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
 // 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 {
 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
 // 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 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.
 	// 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
 	state.mode = 0
-	err = SetConsoleMode(fd, state.mode)
+	err = winconsole.SetConsoleMode(fd, state.mode)
 	if err != nil {
 	if err != nil {
 		return nil, err
 		return nil, err
 	}
 	}
 	return state, nil
 	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()
+}

+ 10 - 5
pkg/term/console_windows.go → pkg/term/winconsole/console_windows.go

@@ -1,6 +1,6 @@
 // +build windows
 // +build windows
 
 
-package term
+package winconsole
 
 
 import (
 import (
 	"bytes"
 	"bytes"
@@ -415,7 +415,7 @@ func getNumberOfChars(fromCoord COORD, toCoord COORD, screenSize COORD) uint32 {
 	return 0
 	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
 	var writeRegion SMALL_RECT
 	writeRegion.Top = fromCoord.Y
 	writeRegion.Top = fromCoord.Y
 	writeRegion.Left = fromCoord.X
 	writeRegion.Left = fromCoord.X
@@ -429,7 +429,7 @@ func clearDisplayRect(fileDesc uintptr, fillChar byte, attributes WORD, fromCoor
 	if size > 0 {
 	if size > 0 {
 		buffer := make([]CHAR_INFO, size)
 		buffer := make([]CHAR_INFO, size)
 		for i := 0; i < len(buffer); i++ {
 		for i := 0; i < len(buffer); i++ {
-			buffer[i].UnicodeChar = WCHAR(string(fillChar)[0])
+			buffer[i].UnicodeChar = WCHAR(fillChar)
 			buffer[i].Attributes = attributes
 			buffer[i].Attributes = attributes
 		}
 		}
 
 
@@ -445,7 +445,7 @@ func clearDisplayRect(fileDesc uintptr, fillChar byte, attributes WORD, fromCoor
 	return true, uint32(size), nil
 	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)
 	nw := uint32(0)
 	// start and end on same line
 	// start and end on same line
 	if fromCoord.Y == toCoord.Y {
 	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)))
 	r, _, err := getNumberOfConsoleInputEventsProc.Call(uintptr(fileDesc), uintptr(unsafe.Pointer(&n)))
 	//If the function succeeds, the return value is nonzero
 	//If the function succeeds, the return value is nonzero
 	if r != 0 {
 	if r != 0 {
-		//fmt.Printf("################%d #################\n", n)
 		return uint16(n), nil
 		return uint16(n), nil
 	}
 	}
 	return 0, err
 	return 0, err
@@ -1103,3 +1102,9 @@ func marshal(c COORD) uint32 {
 	// works only on intel-endian machines
 	// works only on intel-endian machines
 	return uint32(uint32(uint16(c.Y))<<16 | uint32(uint16(c.X)))
 	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
+}

+ 1 - 1
pkg/term/console_windows_test.go → pkg/term/winconsole/console_windows_test.go

@@ -1,6 +1,6 @@
 // +build windows
 // +build windows
 
 
-package term
+package winconsole
 
 
 import (
 import (
 	"fmt"
 	"fmt"

+ 1 - 1
pkg/term/term_emulator.go → pkg/term/winconsole/term_emulator.go

@@ -1,4 +1,4 @@
-package term
+package winconsole
 
 
 import (
 import (
 	"io"
 	"io"

+ 1 - 1
pkg/term/term_emulator_test.go → pkg/term/winconsole/term_emulator_test.go

@@ -1,4 +1,4 @@
-package term
+package winconsole
 
 
 import (
 import (
 	"bytes"
 	"bytes"