diff --git a/cmd/cmd.go b/cmd/cmd.go index 7a6e6f0c..7eb417b1 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -11,6 +11,7 @@ import ( log "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/spf13/viper" + "os" "strings" ) @@ -45,6 +46,22 @@ Dynamic Env Var Variables: } func preRun(cmd *cobra.Command, args []string) error { + kubeconfig, err := cmd.Flags().GetString("kubeconfig") + if err != nil { + panic(err) + } + if kubeconfig == "$HOME" || strings.HasPrefix(kubeconfig, "$HOME"+string(os.PathSeparator)) { + home, err := os.UserHomeDir() + if err != nil { + return err + } + kubeconfig = home + kubeconfig[5:] + err = cmd.Flags().Set("kubeconfig", kubeconfig) + if err != nil { + panic(err) + } + } + grammarFlag, err := cmd.Flags().GetString("grammar") if err != nil { panic(err) diff --git a/internal/config/flags/kubernetes.go b/internal/config/flags/kubernetes.go index eb5e804f..434b53ff 100644 --- a/internal/config/flags/kubernetes.go +++ b/internal/config/flags/kubernetes.go @@ -6,16 +6,11 @@ import ( "github.com/clevyr/kubedb/internal/kubernetes" "github.com/spf13/cobra" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/client-go/util/homedir" "path/filepath" ) func Kubeconfig(cmd *cobra.Command) { - var kubeconfigDefault string - if home := homedir.HomeDir(); home != "" { - kubeconfigDefault = filepath.Join(home, ".kube", "config") - } - cmd.PersistentFlags().String("kubeconfig", kubeconfigDefault, "absolute path to the kubeconfig file") + cmd.PersistentFlags().String("kubeconfig", filepath.Join("$HOME", ".kube", "config"), "absolute path to the kubeconfig file") } func Context(cmd *cobra.Command) {