Skip to content

Commit

Permalink
♻️ Build kubeconfig path later during runtime to clean up path in hel…
Browse files Browse the repository at this point in the history
…p text
  • Loading branch information
gabe565 committed Mar 28, 2022
1 parent 8606c33 commit c8dbe64
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
17 changes: 17 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"os"
"strings"
)

Expand Down Expand Up @@ -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)
Expand Down
7 changes: 1 addition & 6 deletions internal/config/flags/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit c8dbe64

Please sign in to comment.