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 #67 from dty1er/krew
Browse files Browse the repository at this point in the history
Avoid "krew version" crash
  • Loading branch information
Hidetatsu Yaginuma authored May 5, 2021
2 parents 6aeab40 + 1b12b5d commit 330bb02
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ When you don't set `KUBECTL_COMMAND`, then `kubectl` is used by default.
Because kubecolor internally calls `kubectl` command, if you are using unsupported kubectl version, it's also not supported by kubecolor.
Kubernetes version support policy can be found in [official doc](https://kubernetes.io/docs/setup/release/version-skew-policy/).

## Krew

[Krew](https://krew.sigs.k8s.io/) is unsupported for now.

## Contributions

Always welcome. Just opening an issue should be also greatful.
Expand Down
3 changes: 2 additions & 1 deletion command/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ func Run(args []string, version string) error {
cmd.Stdin = os.Stdin

// when should not colorize, just run command and return
if !shouldColorize {
// TODO: right now, krew is unsupported by kubecolor but it should be.
if !shouldColorize || subcommandInfo.IsKrew {
cmd.Stdout = Stdout
cmd.Stderr = Stderr
if err := cmd.Start(); err != nil {
Expand Down
4 changes: 4 additions & 0 deletions command/subcommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ 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
16 changes: 16 additions & 0 deletions kubectl/subcommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ type SubcommandInfo struct {
Help bool
Recursive bool
Short bool

IsKrew bool
}

type FormatOption int
Expand Down Expand Up @@ -194,7 +196,21 @@ func CollectCommandlineOptions(args []string, info *SubcommandInfo) {
}

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: 2 additions & 0 deletions kubectl/subcommand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ 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 330bb02

Please sign in to comment.