Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor to use new SkaffoldWriter type #5894

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/skaffold/app/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func NewSkaffoldCommand(out, errOut io.Writer) *cobra.Command {

opts.Command = cmd.Use
instrumentation.SetCommand(cmd.Use)
out := output.SetupColors(out, defaultColor, forceColors)
out := output.SetupOutput(out, defaultColor, forceColors)
if timestamps {
l := logrus.New()
l.SetOutput(out)
Expand Down
2 changes: 1 addition & 1 deletion cmd/skaffold/skaffold.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func main() {
// As we allow some color setup using CLI flags for the main run, we can't run SetupColors()
// for the entire skaffold run here. It's possible SetupColors() was never called, so call it again
// before we print an error to get the right coloring.
errOut := output.SetupColors(os.Stderr, output.DefaultColorCode, false)
errOut := output.SetupOutput(os.Stderr, output.DefaultColorCode, false)
output.Red.Fprintln(errOut, err)
code = exitCode(err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/skaffold/build/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (l *logAggregatorImpl) GetWriter() (io.Writer, func(), error) {

writer := io.Writer(w)
if output.IsColorable(l.out) {
writer = output.NewWriter(writer)
writer = output.NewColorWriter(writer)
}
ch := make(chan string, buffSize)
l.messages <- ch
Expand Down
26 changes: 5 additions & 21 deletions pkg/skaffold/output/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package output
import (
"fmt"
"io"
"os"
"strings"

colors "github.com/heroku/color"
Expand Down Expand Up @@ -71,7 +70,7 @@ func SetupColors(out io.Writer, defaultColor int, forceColors bool) io.Writer {
}[defaultColor]

if useColors {
return NewWriter(out)
return NewColorWriter(out)
}
return out
}
Expand Down Expand Up @@ -137,32 +136,17 @@ func (c Color) Fprintf(out io.Writer, format string, a ...interface{}) {
fmt.Fprint(out, c.color.Sprintf(format, a...))
}

func NewWriter(out io.Writer) io.Writer {
func NewColorWriter(out io.Writer) io.Writer {
return colorableWriter{out}
}

func IsColorable(out io.Writer) bool {
switch out.(type) {
switch w := out.(type) {
case colorableWriter:
return true
case SkaffoldWriter:
return IsColorable(w.MainWriter)
default:
return false
}
}

func IsStdout(out io.Writer) bool {
o, ok := out.(colorableWriter)
if ok {
return o.Writer == os.Stdout
}
return out == os.Stdout
}

// GetWriter returns the underlying writer if out is a colorableWriter
func GetWriter(out io.Writer) io.Writer {
o, ok := out.(colorableWriter)
if ok {
return o.Writer
}
return out
}
60 changes: 0 additions & 60 deletions pkg/skaffold/output/color_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ package output

import (
"bytes"
"io"
"os"
"testing"

"github.com/GoogleContainerTools/skaffold/testutil"
)

func compareText(t *testing.T, expected, actual string) {
Expand Down Expand Up @@ -93,59 +89,3 @@ 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))
})
}
}

func TestGetWriter(t *testing.T) {
tests := []struct {
description string
out io.Writer
expected io.Writer
}{
{
description: "colorable os.Stdout returns os.Stdout",
out: colorableWriter{os.Stdout},
expected: os.Stdout,
},
{
description: "GetWriter returns original writer if not colorable",
out: os.Stdout,
expected: os.Stdout,
},
}
for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
t.CheckDeepEqual(true, test.expected == GetWriter(test.out))
})
}
}
75 changes: 75 additions & 0 deletions pkg/skaffold/output/output.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
Copyright 2021 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.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package output

import (
"io"
"io/ioutil"
"os"
)

type SkaffoldWriter struct {
MarlonGamez marked this conversation as resolved.
Show resolved Hide resolved
MainWriter io.Writer
EventWriter io.Writer
}

func (s SkaffoldWriter) Write(p []byte) (int, error) {
n, err := s.MainWriter.Write(p)
if err != nil {
return n, err
}
if n != len(p) {
return n, io.ErrShortWrite
}

s.EventWriter.Write(p)

return len(p), nil
}

func SetupOutput(out io.Writer, defaultColor int, forceColors bool) io.Writer {
MarlonGamez marked this conversation as resolved.
Show resolved Hide resolved
return SkaffoldWriter{
MainWriter: SetupColors(out, defaultColor, forceColors),
// TODO(marlongamez): Replace this once event writer is implemented
EventWriter: ioutil.Discard,
}
}

func IsStdout(out io.Writer) bool {
sw, isSW := out.(SkaffoldWriter)
MarlonGamez marked this conversation as resolved.
Show resolved Hide resolved
if isSW {
out = sw.MainWriter
}
cw, isCW := out.(colorableWriter)
if isCW {
out = cw.Writer
}
return out == os.Stdout
}

// GetWriter returns the underlying writer if out is a colorableWriter
func GetWriter(out io.Writer) io.Writer {
MarlonGamez marked this conversation as resolved.
Show resolved Hide resolved
sw, isSW := out.(SkaffoldWriter)
if isSW {
out = sw.MainWriter
}
cw, isCW := out.(colorableWriter)
if isCW {
out = cw.Writer
}
return out
}
118 changes: 118 additions & 0 deletions pkg/skaffold/output/output_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
Copyright 2021 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.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package output

import (
"bytes"
"io"
"io/ioutil"
"os"
"testing"

"github.com/GoogleContainerTools/skaffold/testutil"
)

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",
MarlonGamez marked this conversation as resolved.
Show resolved Hide resolved
out: SkaffoldWriter{
MainWriter: NewColorWriter(os.Stdout),
},
expected: true,
},
{
description: "colorableWriter passed",
out: NewColorWriter(os.Stdout),
expected: true,
},
{
description: "invalid colorableWriter passed",
out: SkaffoldWriter{
MainWriter: NewColorWriter(ioutil.Discard),
},
expected: false,
},
}
for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
t.CheckDeepEqual(test.expected, IsStdout(test.out))
})
}
}

func TestGetWriter(t *testing.T) {
tests := []struct {
description string
out io.Writer
expected io.Writer
}{
{
MarlonGamez marked this conversation as resolved.
Show resolved Hide resolved
description: "colorable os.Stdout returns os.Stdout",
out: SkaffoldWriter{
MainWriter: colorableWriter{os.Stdout},
},
expected: os.Stdout,
},
{
description: "skaffold writer returns os.Stdout without colorableWriter",
out: SkaffoldWriter{
MainWriter: os.Stdout,
},
expected: os.Stdout,
},
{
description: "return ioutil.Discard from SkaffoldWriter",
out: SkaffoldWriter{
MainWriter: NewColorWriter(ioutil.Discard),
},
expected: ioutil.Discard,
},
{
description: "os.Stdout returned from colorableWriter",
out: NewColorWriter(os.Stdout),
expected: os.Stdout,
},
{
description: "GetWriter returns original writer if not colorable",
out: os.Stdout,
expected: os.Stdout,
},
}
for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
t.CheckDeepEqual(true, test.expected == GetWriter(test.out))
})
}
}