From 5541b7833182d126e6368f9f5c861ae9d96126e5 Mon Sep 17 00:00:00 2001 From: Gabe Cook Date: Wed, 25 Oct 2023 14:02:17 -0500 Subject: [PATCH] chore(config): Simplify Kubeconfig flag default value --- internal/config/flags/kubernetes.go | 11 +++++------ internal/generate/docs/main.go | 5 ----- internal/generate/manpages/main.go | 5 ----- 3 files changed, 5 insertions(+), 16 deletions(-) diff --git a/internal/config/flags/kubernetes.go b/internal/config/flags/kubernetes.go index 85b6e724..36bf35d1 100644 --- a/internal/config/flags/kubernetes.go +++ b/internal/config/flags/kubernetes.go @@ -16,18 +16,17 @@ import ( const KubeconfigEnv = "KUBECONFIG" func Kubeconfig(cmd *cobra.Command) { - kubeconfigEnv := os.Getenv(KubeconfigEnv) - if kubeconfigEnv == "" { - kubeconfigEnv = filepath.Join("$HOME", ".kube", "config") - } - - cmd.PersistentFlags().String(consts.KubeconfigFlag, kubeconfigEnv, "Paths to the kubeconfig file") + cmd.PersistentFlags().String(consts.KubeconfigFlag, filepath.Join("$HOME", ".kube", "config"), "Paths to the kubeconfig file") } func BindKubeconfig(cmd *cobra.Command) { if err := viper.BindPFlag(consts.KubeconfigKey, cmd.Flags().Lookup(consts.KubeconfigFlag)); err != nil { panic(err) } + + if env := os.Getenv(KubeconfigEnv); env != "" { + viper.SetDefault(consts.KubeconfigKey, env) + } } func Context(cmd *cobra.Command) { diff --git a/internal/generate/docs/main.go b/internal/generate/docs/main.go index f0973087..1c78b1d1 100644 --- a/internal/generate/docs/main.go +++ b/internal/generate/docs/main.go @@ -6,7 +6,6 @@ import ( "os" "github.com/clevyr/kubedb/cmd" - "github.com/clevyr/kubedb/internal/config/flags" "github.com/spf13/cobra/doc" ) @@ -24,10 +23,6 @@ func main() { log.Fatal(fmt.Errorf("failed to mkdir: %w", err)) } - if err := os.Setenv(flags.KubeconfigEnv, ""); err != nil { - log.Fatal(err) - } - rootCmd := cmd.NewCommand() err = doc.GenMarkdownTree(rootCmd, output) diff --git a/internal/generate/manpages/main.go b/internal/generate/manpages/main.go index 2422e712..7b76f0c4 100644 --- a/internal/generate/manpages/main.go +++ b/internal/generate/manpages/main.go @@ -9,7 +9,6 @@ import ( "time" "github.com/clevyr/kubedb/cmd" - kubedbFlags "github.com/clevyr/kubedb/internal/config/flags" gzip "github.com/klauspost/pgzip" "github.com/spf13/cobra/doc" flag "github.com/spf13/pflag" @@ -36,10 +35,6 @@ func main() { panic(err) } - if err := os.Setenv(kubedbFlags.KubeconfigEnv, ""); err != nil { - panic(err) - } - rootCmd := cmd.NewCommand() rootName := rootCmd.Name()