Skip to content

Commit

Permalink
feat: intercept and output any error from kubectl calls
Browse files Browse the repository at this point in the history
Better error reporting, yay! 🎉
  • Loading branch information
metacosm committed Oct 9, 2019
1 parent ea3039d commit 8a3cd12
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
5 changes: 5 additions & 0 deletions pkg/k8s/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,13 @@ func Apply(path, namespace string) error {

func runKubectl(args ...string) error {
command := exec.Command(kubectl, args...)
interceptor := log.GetErrorInterceptor()
command.Stderr = interceptor
err := command.Run()
if err != nil {
if len(interceptor.ErrorMsg) > 0 {
return fmt.Errorf("%v: %s", err, interceptor.ErrorMsg)
}
return err
}
return nil
Expand Down
39 changes: 25 additions & 14 deletions pkg/log/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,22 @@ import (
const suffixSpacing = " "
const prefixSpacing = " "

var (
stdErr = colorable.NewColorableStderr()
stdOut = colorable.NewColorableStdout()
errInterceptor = &ErrorInterceptor{stdErr: stdErr}
)

type ErrorInterceptor struct {
stdErr io.Writer
ErrorMsg string
}

func (l *ErrorInterceptor) Write(p []byte) (n int, err error) {
l.ErrorMsg = string(p)
return len(p), nil
}

// Status is used to track ongoing status in a CLI, with a nice loading spinner
// when attached to a terminal
type Status struct {
Expand All @@ -55,6 +71,7 @@ func NewStatus(w io.Writer) *Status {
spinner: spin,
writer: w,
}
s.MaybeWrapWriter(w)
return s
}

Expand Down Expand Up @@ -238,24 +255,18 @@ func IsDebug() bool {
return false
}

// GetStdout gets the appropriate stdout from the OS. If it's Linux, it will use
// the go-colorable library in order to fix any and all color ASCII issues.
// TODO: Test needs to be added once we get Windows testing available on TravisCI / CI platform.
// GetStdout gets the appropriate stdout from the OS.
func GetStdout() io.Writer {
if runtime.GOOS == "windows" {
return colorable.NewColorableStdout()
}
return os.Stdout
return stdOut
}

// GetStderr gets the appropriate stderrfrom the OS. If it's Linux, it will use
// the go-colorable library in order to fix any and all color ASCII issues.
// TODO: Test needs to be added once we get Windows testing available on TravisCI / CI platform.
// GetStderr gets the appropriate stderr from the OS.
func GetStderr() io.Writer {
if runtime.GOOS == "windows" {
return colorable.NewColorableStderr()
}
return os.Stderr
return stdErr
}

func GetErrorInterceptor() *ErrorInterceptor {
return errInterceptor
}

// getErrString returns a certain string based upon the OS.
Expand Down

0 comments on commit 8a3cd12

Please sign in to comment.