Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions main.go
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,21 @@ func log2exit(retval int, msg string) {
/*
Reference: https://golangcode.com/check-if-a-file-exists/
FIXME: The source article was down.

Changed: fileExists now returns (bool, error).
For any os.Stat error (including file-not-exist), the function returns
false plus an error with the message "file non-exist or error: ...".
This avoids dereferencing a nil FileInfo when os.Stat returns a non-nil error.
*/
func fileExists(filename string) bool {
func fileExists(filename string) (bool, error) {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
if err != nil {
// Combine all errors (not found, permission, I/O, etc.) into a single case
// and return a descriptive error as requested.
return false, fmt.Errorf("file non-exist or error: %v", err)
}
/* FIXME: The file may be regular ot not */
return !info.IsDir()
/* FIXME: The file may be regular or not */
return !info.IsDir(), nil
}

func args2cmd(args []string) (string, []string) {
Expand Down Expand Up @@ -100,7 +107,7 @@ func main() {
log2exit(1, ":: Error: Cluster name must be something like :foo.\n")
}
if cluster_name[0:1] != ":" {
log2exit(1, fmt.Sprintf(":: Error: Cluster name (context) must be prefixed with `:', e.g., gk8s :%s.\n", cluster_name))
log2exit(1, fmt.Sprintf(":: Error: Cluster name (context) must be prefixed with `:' , e.g., gk8s :%s.\n", cluster_name))
}
cluster_name = cluster_name[1:]

Expand Down Expand Up @@ -130,13 +137,15 @@ func main() {
kubecfg = filepath.Join(home, ".kube/config")
}

if !fileExists(kubecfg) {
log2exit(127, fmt.Sprintf(":: KUBECONFIG file not found: %s\n", kubecfg))
// Use new fileExists signature
exists, err := fileExists(kubecfg)
if err != nil || !exists {
log2exit(127, fmt.Sprintf(":: KUBECONFIG file not found or stat error: %v\n", err))
}

os.Setenv("KUBECONFIG", kubecfg)
log2(fmt.Sprintf(":: Executing '%s', args: %v, KUBECONFIG: %s\n", binary, args, kubecfg))
err := syscall.Exec(binary, args, syscall.Environ())
err = syscall.Exec(binary, args, syscall.Environ())
if err != nil {
log2exit(1, fmt.Sprintf(":: Error: %v.\n", err))
}
Expand Down
4 changes: 2 additions & 2 deletions tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,11 @@ default() {
"The noop command just does nothing"

_test _fail_with_cluster_config_not_found \
"KUBECONFIG file not found:" \
"KUBECONFIG file not found or stat error:" \
"Return immediately when kubecfg file not found."

_test _fail_with_local_and_cluster_config_not_found \
"(Command not found)|(KUBECONFIG file not found:)|(The connection to the server localhost:8080 was refused)" \
"(Command not found)|(KUBECONFIG file not found or stat error:)|(The connection to the server localhost:8080 was refused)" \
"Return when cluster configuration doesn't work"

_test _ok_with_fake_kubectl_get_pods \
Expand Down