Skip to content
This repository has been archived by the owner on Jul 30, 2023. It is now read-only.

Commit

Permalink
Merge pull request #69 from dty1er/dty1er/handle_panic
Browse files Browse the repository at this point in the history
handle panic
  • Loading branch information
Hidetatsu Yaginuma authored May 7, 2021
2 parents 330bb02 + 1db9c95 commit d00a659
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 24 deletions.
25 changes: 20 additions & 5 deletions command/runner.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package command

import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"strings"
Expand Down Expand Up @@ -57,7 +59,7 @@ func Run(args []string, version string) error {

// when should not colorize, just run command and return
// TODO: right now, krew is unsupported by kubecolor but it should be.
if !shouldColorize || subcommandInfo.IsKrew {
if !shouldColorize {
cmd.Stdout = Stdout
cmd.Stderr = Stderr
if err := cmd.Start(); err != nil {
Expand All @@ -72,16 +74,21 @@ func Run(args []string, version string) error {
}

// when colorize, capture stdout and err then colorize it
outReader, err := cmd.StdoutPipe()
cmdOut, err := cmd.StdoutPipe()
if err != nil {
return err
}

errReader, err := cmd.StderrPipe()
cmdErr, err := cmd.StderrPipe()
if err != nil {
return err
}

// make buffer to be used in defer recover()
buff := new(bytes.Buffer)
outReader := io.TeeReader(cmdOut, buff)
errReader := io.TeeReader(cmdErr, buff)

if err := cmd.Start(); err != nil {
return err
}
Expand All @@ -92,14 +99,22 @@ func Run(args []string, version string) error {

wg.Add(1)
go func() {
defer wg.Done()
defer func() {
if r := recover(); r != nil {
fmt.Fprintf(os.Stdout, buff.String())
}
}()

// This can panic when kubecolor has bug, so recover in defer
printers.FullColoredPrinter.Print(outReader, Stdout)
wg.Done()
}()

wg.Add(1)
go func() {
defer wg.Done()
// This will unlikely panic
printers.ErrorPrinter.Print(errReader, Stderr)
wg.Done()
}()

wg.Wait()
Expand Down
4 changes: 0 additions & 4 deletions command/subcommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ func ResolveSubcommand(args []string, config *KubecolorConfig) (bool, *kubectl.S
// subcommandFound becomes false when subcommand is not found; e.g. "kubecolor --help"
subcommandInfo, subcommandFound := kubectl.InspectSubcommandInfo(args)

if subcommandInfo.IsKrew {
return false, subcommandInfo
}

// if --plain found, it does not colorize
if config.Plain {
return false, subcommandInfo
Expand Down
14 changes: 1 addition & 13 deletions kubectl/subcommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,22 +195,10 @@ func CollectCommandlineOptions(args []string, info *SubcommandInfo) {
}
}

// TODO: return shouldColorize = false when the given args is for plugin
func InspectSubcommandInfo(args []string) (*SubcommandInfo, bool) {
// TODO: support krew
contains := func(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
ret := &SubcommandInfo{}

if contains(args, "krew") {
return &SubcommandInfo{IsKrew: true}, false
}

CollectCommandlineOptions(args, ret)

for i := range args {
Expand Down
2 changes: 0 additions & 2 deletions kubectl/subcommand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ func TestInspectSubcommandInfo(t *testing.T) {

{"apply", &SubcommandInfo{Subcommand: Apply}, true},

{"krew version", &SubcommandInfo{IsKrew: true}, false},

{"", &SubcommandInfo{}, false},
}
for _, tt := range tests {
Expand Down

0 comments on commit d00a659

Please sign in to comment.