From 49d8784de0a5073408a122591cb43fbf4fac5d2f Mon Sep 17 00:00:00 2001 From: Isaac Duarte Date: Fri, 13 Nov 2020 14:24:35 -0500 Subject: [PATCH 1/2] Show survey prompt with colors --- pkg/skaffold/color/formatter.go | 11 ++++++++- pkg/skaffold/color/formatter_test.go | 37 +++++++++++++++++++++++++++- pkg/skaffold/survey/survey.go | 7 +----- pkg/skaffold/survey/survey_test.go | 28 --------------------- 4 files changed, 47 insertions(+), 36 deletions(-) diff --git a/pkg/skaffold/color/formatter.go b/pkg/skaffold/color/formatter.go index 47feb4b3c85..fd65ca487af 100644 --- a/pkg/skaffold/color/formatter.go +++ b/pkg/skaffold/color/formatter.go @@ -1,5 +1,5 @@ /* -Copyright 2019 The Skaffold Authors +Copyright 2020 The Skaffold Authors Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -19,6 +19,7 @@ package color import ( "fmt" "io" + "os" "strings" colors "github.com/heroku/color" @@ -142,3 +143,11 @@ func IsColorable(out io.Writer) bool { return false } } + +func IsStdout(out io.Writer) bool { + o, ok := out.(colorableWriter) + if ok { + return o.Writer == os.Stdout + } + return out == os.Stdout +} diff --git a/pkg/skaffold/color/formatter_test.go b/pkg/skaffold/color/formatter_test.go index 4bf9585dd33..cf68122a8f0 100644 --- a/pkg/skaffold/color/formatter_test.go +++ b/pkg/skaffold/color/formatter_test.go @@ -1,5 +1,5 @@ /* -Copyright 2019 The Skaffold Authors +Copyright 2020 The Skaffold Authors Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -18,6 +18,9 @@ package color import ( "bytes" + "github.com/GoogleContainerTools/skaffold/testutil" + "io" + "os" "testing" ) @@ -89,3 +92,35 @@ func TestFprintlnChangeDefaultToUnknown(t *testing.T) { Default.Fprintln(cw, "2", "less", "chars!") compareText(t, "2 less chars!\n", b.String()) } + +func TestIsStdOut(t *testing.T) { + tests := []struct { + description string + out io.Writer + expected bool + }{ + { + description: "std out passed", + out: os.Stdout, + expected: true, + }, + { + description: "out nil", + out: nil, + }, + { + description: "out bytes buffer", + out: new(bytes.Buffer), + }, + { + description: "colorable std out passed", + out: NewWriter(os.Stdout), + expected: true, + }, + } + for _, test := range tests { + testutil.Run(t, test.description, func(t *testutil.T) { + t.CheckDeepEqual(test.expected, IsStdout(test.out)) + }) + } +} diff --git a/pkg/skaffold/survey/survey.go b/pkg/skaffold/survey/survey.go index 7d13213c963..def4c9c558a 100644 --- a/pkg/skaffold/survey/survey.go +++ b/pkg/skaffold/survey/survey.go @@ -20,7 +20,6 @@ import ( "context" "fmt" "io" - "os" "github.com/pkg/browser" "github.com/sirupsen/logrus" @@ -47,7 +46,7 @@ Tip: To permanently disable the survey prompt, run: skaffold config set --survey --global disable-prompt true`, URL) // for testing - isStdOut = stdOut + isStdOut = color.IsStdout open = browser.OpenURL updateConfig = config.UpdateGlobalSurveyPrompted ) @@ -82,7 +81,3 @@ func (s *Runner) OpenSurveyForm(_ context.Context, out io.Writer) error { // When prompting for the survey, we need to use the same field. return config.UpdateGlobalSurveyTaken(s.configFile) } - -func stdOut(out io.Writer) bool { - return out == os.Stdout -} diff --git a/pkg/skaffold/survey/survey_test.go b/pkg/skaffold/survey/survey_test.go index e3e03632389..58b83bc3fc9 100644 --- a/pkg/skaffold/survey/survey_test.go +++ b/pkg/skaffold/survey/survey_test.go @@ -19,7 +19,6 @@ package survey import ( "bytes" "io" - "os" "testing" "github.com/GoogleContainerTools/skaffold/testutil" @@ -53,30 +52,3 @@ func TestDisplaySurveyForm(t *testing.T) { }) } } - -func TestIsStdOut(t *testing.T) { - tests := []struct { - description string - out io.Writer - expected bool - }{ - { - description: "std out passed", - out: os.Stdout, - expected: true, - }, - { - description: "out nil", - out: nil, - }, - { - description: "out bytes buffer", - out: new(bytes.Buffer), - }, - } - for _, test := range tests { - testutil.Run(t, test.description, func(t *testutil.T) { - t.CheckDeepEqual(test.expected, isStdOut(test.out)) - }) - } -} From f13b0fb9b2a7df51e8efd2d818a00b06d61e1c46 Mon Sep 17 00:00:00 2001 From: Isaac Duarte Date: Fri, 13 Nov 2020 16:13:02 -0500 Subject: [PATCH 2/2] fix order of imports to make linter happy --- pkg/skaffold/color/formatter_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/skaffold/color/formatter_test.go b/pkg/skaffold/color/formatter_test.go index cf68122a8f0..175e486dc2b 100644 --- a/pkg/skaffold/color/formatter_test.go +++ b/pkg/skaffold/color/formatter_test.go @@ -18,10 +18,11 @@ package color import ( "bytes" - "github.com/GoogleContainerTools/skaffold/testutil" "io" "os" "testing" + + "github.com/GoogleContainerTools/skaffold/testutil" ) func compareText(t *testing.T, expected, actual string) {