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

Commit 330bb02

Browse files
author
Hidetatsu Yaginuma
authored
Merge pull request #67 from dty1er/krew
Avoid "krew version" crash
2 parents 6aeab40 + 1b12b5d commit 330bb02

File tree

5 files changed

+28
-1
lines changed

5 files changed

+28
-1
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ When you don't set `KUBECTL_COMMAND`, then `kubectl` is used by default.
183183
Because kubecolor internally calls `kubectl` command, if you are using unsupported kubectl version, it's also not supported by kubecolor.
184184
Kubernetes version support policy can be found in [official doc](https://kubernetes.io/docs/setup/release/version-skew-policy/).
185185

186+
## Krew
187+
188+
[Krew](https://krew.sigs.k8s.io/) is unsupported for now.
189+
186190
## Contributions
187191

188192
Always welcome. Just opening an issue should be also greatful.

command/runner.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ func Run(args []string, version string) error {
5656
cmd.Stdin = os.Stdin
5757

5858
// when should not colorize, just run command and return
59-
if !shouldColorize {
59+
// TODO: right now, krew is unsupported by kubecolor but it should be.
60+
if !shouldColorize || subcommandInfo.IsKrew {
6061
cmd.Stdout = Stdout
6162
cmd.Stderr = Stderr
6263
if err := cmd.Start(); err != nil {

command/subcommand.go

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ func ResolveSubcommand(args []string, config *KubecolorConfig) (bool, *kubectl.S
1616
// subcommandFound becomes false when subcommand is not found; e.g. "kubecolor --help"
1717
subcommandInfo, subcommandFound := kubectl.InspectSubcommandInfo(args)
1818

19+
if subcommandInfo.IsKrew {
20+
return false, subcommandInfo
21+
}
22+
1923
// if --plain found, it does not colorize
2024
if config.Plain {
2125
return false, subcommandInfo

kubectl/subcommand.go

+16
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ type SubcommandInfo struct {
1212
Help bool
1313
Recursive bool
1414
Short bool
15+
16+
IsKrew bool
1517
}
1618

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

196198
func InspectSubcommandInfo(args []string) (*SubcommandInfo, bool) {
199+
// TODO: support krew
200+
contains := func(s []string, e string) bool {
201+
for _, a := range s {
202+
if a == e {
203+
return true
204+
}
205+
}
206+
return false
207+
}
197208
ret := &SubcommandInfo{}
209+
210+
if contains(args, "krew") {
211+
return &SubcommandInfo{IsKrew: true}, false
212+
}
213+
198214
CollectCommandlineOptions(args, ret)
199215

200216
for i := range args {

kubectl/subcommand_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ func TestInspectSubcommandInfo(t *testing.T) {
6262

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

65+
{"krew version", &SubcommandInfo{IsKrew: true}, false},
66+
6567
{"", &SubcommandInfo{}, false},
6668
}
6769
for _, tt := range tests {

0 commit comments

Comments
 (0)