fix: Show meaningful error message when kubectl exec fails (#1966)

`exec.LookPath()` and `exec.Command()` aren't supposed to work with
binaries that are located in the current working directory:

fd208c8850:src/os/exec/exec.go

If `kubectl` is found in the current working directory, `LookPath()`
will return `ErrDot` even if it is also found in any other directory in
the `PATH`.

This patch detects this condition and shows a meaningful error message
in the log so that the user knows the reason why `kubectl` couldn't be
executed.

Related: https://github.com/derailed/k9s/issues/1787
mine
Antonio Niño Díaz 2023-02-11 18:51:11 +00:00 committed by GitHub
parent 0d7678babc
commit 2d2baa884a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 0 deletions

View File

@ -41,6 +41,10 @@ type shellOpts struct {
func runK(a *App, opts shellOpts) bool {
bin, err := exec.LookPath("kubectl")
if errors.Is(err, exec.ErrDot) {
log.Error().Err(err).Msgf("kubectl command must not be in the current working directory")
return false
}
if err != nil {
log.Error().Err(err).Msgf("kubectl command is not in your path")
return false
@ -137,6 +141,10 @@ func execute(opts shellOpts) error {
func runKu(a *App, opts shellOpts) (string, error) {
bin, err := exec.LookPath("kubectl")
if errors.Is(err, exec.ErrDot) {
log.Error().Err(err).Msgf("kubectl command must not be in the current working directory")
return "", err
}
if err != nil {
log.Error().Err(err).Msgf("kubectl command is not in your path")
return "", err