From ee4e376fc74a0117ab03577c6e04c09fe21a946d Mon Sep 17 00:00:00 2001 From: gentele Date: Fri, 26 Oct 2018 16:04:38 +0200 Subject: [PATCH 1/3] fix issue with helm repo file --- pkg/devspace/helm/client.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/devspace/helm/client.go b/pkg/devspace/helm/client.go index c51f417bdb..ed3c905d8d 100644 --- a/pkg/devspace/helm/client.go +++ b/pkg/devspace/helm/client.go @@ -132,8 +132,8 @@ func createNewClient(kubectlClient *kubernetes.Clientset, log log.Logger, upgrad os.MkdirAll(repoPath, os.ModePerm) os.MkdirAll(filepath.Dir(stableRepoCachePathAbs), os.ModePerm) - _, repoFileNotFound := os.Stat(repoFile) - if repoFileNotFound != nil { + repoFileStat, repoFileNotFound := os.Stat(repoFile) + if repoFileNotFound != nil || repoFileStat.Size() == 0 { err = fsutil.WriteToFile([]byte(defaultRepositories), repoFile) if err != nil { return nil, err From 011e97d170836c69d9bd7017381d8b7ef270aa84 Mon Sep 17 00:00:00 2001 From: gentele Date: Fri, 26 Oct 2018 16:18:01 +0200 Subject: [PATCH 2/3] trim loading text for small terminals --- pkg/devspace/kubectl/client.go | 28 ++-------------------------- pkg/util/log/loading_text.go | 25 +++++++++++++++++++++++-- pkg/util/terminal/tty.go | 30 ++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 28 deletions(-) create mode 100644 pkg/util/terminal/tty.go diff --git a/pkg/devspace/kubectl/client.go b/pkg/devspace/kubectl/client.go index 1545307ca5..6966afb16c 100644 --- a/pkg/devspace/kubectl/client.go +++ b/pkg/devspace/kubectl/client.go @@ -1,6 +1,7 @@ package kubectl import ( + "github.com/covexo/devspace/pkg/util/terminal" "bytes" "errors" "fmt" @@ -16,7 +17,6 @@ import ( "github.com/covexo/devspace/pkg/devspace/config/configutil" "github.com/covexo/devspace/pkg/devspace/config/v1" "github.com/covexo/devspace/pkg/util/log" - dockerterm "github.com/docker/docker/pkg/term" k8sv1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" @@ -413,7 +413,7 @@ func Exec(kubectlClient *kubernetes.Clientset, pod *k8sv1.Pod, container string, SubResource("exec") if tty { - t = setupTTY() + t = terminal.SetupTTY() } execRequest.VersionedParams(&k8sapi.PodExecOptions{ @@ -475,30 +475,6 @@ func Exec(kubectlClient *kubernetes.Clientset, pod *k8sv1.Pod, container string, return stdinWriter, stdoutReader, stderrReader, nil } -func setupTTY() term.TTY { - t := term.TTY{ - Out: os.Stdout, - In: os.Stdin, - } - - if !t.IsTerminalIn() { - log.Info("Unable to use a TTY - input is not a terminal or the right kind of file") - - return t - } - - // if we get to here, the user wants to attach stdin, wants a TTY, and In is a terminal, so we - // can safely set t.Raw to true - t.Raw = true - - stdin, stdout, _ := dockerterm.StdStreams() - - t.In = stdin - t.Out = stdout - - return t -} - //ExecBuffered executes a command for kubernetes and returns the output and error buffers func ExecBuffered(kubectlClient *kubernetes.Clientset, pod *k8sv1.Pod, container string, command []string) ([]byte, []byte, error) { _, stdout, stderr, execErr := Exec(kubectlClient, pod, container, command, false, nil) diff --git a/pkg/util/log/loading_text.go b/pkg/util/log/loading_text.go index 116602953f..7af77be143 100644 --- a/pkg/util/log/loading_text.go +++ b/pkg/util/log/loading_text.go @@ -5,11 +5,15 @@ import ( "io" "time" + "github.com/covexo/devspace/pkg/util/terminal" + "github.com/daviddengcn/go-colortext" ) const waitInterval = time.Millisecond * 150 +var tty = terminal.SetupTTY() + type loadingText struct { Stream io.Writer Message string @@ -71,13 +75,30 @@ func (l *loadingText) render() { } else { l.Stream.Write([]byte("\r")) } + messagePrefix := []byte("[WAIT] ") ct.Foreground(ct.Red, false) - l.Stream.Write([]byte("[WAIT] ")) + l.Stream.Write(messagePrefix) ct.ResetColor() timeElapsed := fmt.Sprintf("%d", (time.Now().UnixNano()-l.startTimestamp)/int64(time.Second)) - l.Stream.Write([]byte(l.getLoadingChar() + " " + l.Message + " (" + timeElapsed + "s)")) + message := []byte(l.getLoadingChar() + " " + l.Message) + messageSuffix := " (" + timeElapsed + "s)" + terminalSize := tty.GetSize() + prefixLength := len(messagePrefix) + suffixLength := len(messageSuffix) + + if uint16(prefixLength+len(message)+suffixLength) > terminalSize.Width { + dots := []byte("...") + maxMessageLength := terminalSize.Width - uint16(prefixLength+suffixLength+len(dots)) + + if maxMessageLength > 0 { + message = append(message[:maxMessageLength], dots...) + } + } + message = append(message, messageSuffix...) + + l.Stream.Write(message) } func (l *loadingText) Stop() { diff --git a/pkg/util/terminal/tty.go b/pkg/util/terminal/tty.go new file mode 100644 index 0000000000..614dd49de4 --- /dev/null +++ b/pkg/util/terminal/tty.go @@ -0,0 +1,30 @@ +package terminal + +import ( + "os" + + dockerterm "github.com/docker/docker/pkg/term" + "k8s.io/kubernetes/pkg/kubectl/util/term" +) + +func SetupTTY() term.TTY { + t := term.TTY{ + Out: os.Stdout, + In: os.Stdin, + } + + if !t.IsTerminalIn() { + return t + } + + // if we get to here, the user wants to attach stdin, wants a TTY, and In is a terminal, so we + // can safely set t.Raw to true + t.Raw = true + + stdin, stdout, _ := dockerterm.StdStreams() + + t.In = stdin + t.Out = stdout + + return t +} From 1a877d2b0c8cb0b427794ed01efe915edba75147 Mon Sep 17 00:00:00 2001 From: gentele Date: Fri, 26 Oct 2018 16:26:38 +0200 Subject: [PATCH 3/3] fix codeclimate --- pkg/devspace/kubectl/client.go | 2 +- pkg/util/terminal/tty.go | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/devspace/kubectl/client.go b/pkg/devspace/kubectl/client.go index 6966afb16c..ac62dcf178 100644 --- a/pkg/devspace/kubectl/client.go +++ b/pkg/devspace/kubectl/client.go @@ -1,10 +1,10 @@ package kubectl import ( - "github.com/covexo/devspace/pkg/util/terminal" "bytes" "errors" "fmt" + "github.com/covexo/devspace/pkg/util/terminal" "io" "net/http" "os" diff --git a/pkg/util/terminal/tty.go b/pkg/util/terminal/tty.go index 614dd49de4..600c0b819c 100644 --- a/pkg/util/terminal/tty.go +++ b/pkg/util/terminal/tty.go @@ -7,6 +7,7 @@ import ( "k8s.io/kubernetes/pkg/kubectl/util/term" ) +// SetupTTY creates a term.TTY (docker) func SetupTTY() term.TTY { t := term.TTY{ Out: os.Stdout,