cmd: fix --progress crash under Windows Jenkins - fixes #2846

This commit is contained in:
Nick Craig-Wood 2018-12-19 12:36:29 +00:00
parent df1faa9a8f
commit 9012d7c6c1
3 changed files with 25 additions and 7 deletions

View File

@ -27,6 +27,11 @@ const (
// //
// It returns a func which should be called to stop the stats. // It returns a func which should be called to stop the stats.
func startProgress() func() { func startProgress() func() {
err := initTerminal()
if err != nil {
fs.Errorf(nil, "Failed to start progress: %v", err)
return func() {}
}
stopStats := make(chan struct{}) stopStats := make(chan struct{})
oldLogPrint := fs.LogPrint oldLogPrint := fs.LogPrint
if !log.Redirected() { if !log.Redirected() {

View File

@ -4,6 +4,10 @@ package cmd
import "os" import "os"
func initTerminal() error {
return nil
}
func writeToTerminal(b []byte) { func writeToTerminal(b []byte) {
_, _ = os.Stdout.Write(b) _, _ = os.Stdout.Write(b)
} }

View File

@ -5,22 +5,31 @@ package cmd
import ( import (
"fmt" "fmt"
"os" "os"
"sync" "syscall"
ansiterm "github.com/Azure/go-ansiterm" ansiterm "github.com/Azure/go-ansiterm"
"github.com/Azure/go-ansiterm/winterm" "github.com/Azure/go-ansiterm/winterm"
"github.com/pkg/errors"
) )
var ( var (
initAnsiParser sync.Once
ansiParser *ansiterm.AnsiParser ansiParser *ansiterm.AnsiParser
) )
func writeToTerminal(b []byte) { func initTerminal() error {
initAnsiParser.Do(func() {
winEventHandler := winterm.CreateWinEventHandler(os.Stdout.Fd(), os.Stdout) winEventHandler := winterm.CreateWinEventHandler(os.Stdout.Fd(), os.Stdout)
if winEventHandler == nil {
err := syscall.GetLastError()
if err == nil {
err = errors.New("initialization failed")
}
return errors.Wrap(err, "windows terminal")
}
ansiParser = ansiterm.CreateParser("Ground", winEventHandler) ansiParser = ansiterm.CreateParser("Ground", winEventHandler)
}) return nil
}
func writeToTerminal(b []byte) {
// Remove all non-ASCII characters until this is fixed // Remove all non-ASCII characters until this is fixed
// https://github.com/Azure/go-ansiterm/issues/26 // https://github.com/Azure/go-ansiterm/issues/26
r := []rune(string(b)) r := []rune(string(b))