From 6bff2387e3d99d1201134043343c7d191f21c27d Mon Sep 17 00:00:00 2001 From: Simon Bein Date: Sun, 29 Oct 2023 11:54:00 +0100 Subject: [PATCH] refactor: remove init() logic during startup This commit gets rid of the complicated and error-prone init() logic that was used during startup. --- cmd/cleanup.go | 4 ---- cmd/completion.go | 4 ---- cmd/delete.go | 4 ---- cmd/import.go | 4 ---- cmd/namespace.go | 4 ---- cmd/root.go | 47 ++++++++++++++++++++++++++++++++++----------- cmd/set.go | 4 ---- cmd/shellwrapper.go | 4 ---- cmd/version.go | 4 ---- main.go | 12 ++++++++++-- 10 files changed, 46 insertions(+), 45 deletions(-) diff --git a/cmd/cleanup.go b/cmd/cleanup.go index fc9e8bc..b4b05cf 100644 --- a/cmd/cleanup.go +++ b/cmd/cleanup.go @@ -95,7 +95,3 @@ func cleanLeftOvers(f afero.Fs) error { return nil } - -func init() { - rootCmd.AddCommand(cleanupCmd) -} diff --git a/cmd/completion.go b/cmd/completion.go index 2b2be23..5c97860 100644 --- a/cmd/completion.go +++ b/cmd/completion.go @@ -94,7 +94,3 @@ func (c *completionCmd) completion(cmd *cobra.Command, args []string) error { return nil } - -func init() { - rootCmd.AddCommand(newCompletionCmd().cmd) -} diff --git a/cmd/delete.go b/cmd/delete.go index 1b4a99f..ea36231 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -124,7 +124,3 @@ func (c *deleteCmd) completeDelete(cmd *cobra.Command, args []string, toComplete return sug, cobra.ShellCompDirectiveNoFileComp } - -func init() { - rootCmd.AddCommand(newDeleteCommand().cmd) -} diff --git a/cmd/import.go b/cmd/import.go index 0b74a58..1e440eb 100644 --- a/cmd/import.go +++ b/cmd/import.go @@ -168,7 +168,3 @@ func filesForDir(sm *store.Storemanager, path string) ([]*FileWithPath, error) { return files, nil } - -func init() { - rootCmd.AddCommand(newImportCmd().cmd) -} diff --git a/cmd/namespace.go b/cmd/namespace.go index fd16344..961d11b 100644 --- a/cmd/namespace.go +++ b/cmd/namespace.go @@ -228,10 +228,6 @@ func setNamespace(fs afero.Fs, ns string) error { return nil } -func init() { - rootCmd.AddCommand(newNamespaceCmd().cmd) -} - func kubeconfigEnv() (string, error) { kPath := os.Getenv("KUBECONFIG") if kPath == "" { diff --git a/cmd/root.go b/cmd/root.go index cad87e3..5ee38dc 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -29,22 +29,38 @@ Afterwards switch between different kubeconfigs via 'konf set' // Execute adds all child commands to the root command and sets flags appropriately. // This is called by main.main(). It only needs to happen once to the rootCmd. -func Execute() { - cobra.CheckErr(rootCmd.Execute()) -} +func Execute() error { + initPersistentFlags() + + if err := initConfig(); err != nil { + return err + } -func init() { - cobra.OnInitialize(wrapInit) + // addCommands needs to be run after the config has been initialized! + initCommands() + // make sure the default directories exist for the sub-commands + if err := utils.EnsureDir(afero.NewOsFs()); err != nil { + return err + } + + if err := rootCmd.Execute(); err != nil { + return err + } + return nil +} + +// initialize flags that are valid for all commands +func initPersistentFlags() { rootCmd.PersistentFlags().StringVar(&konfDir, "konf-dir", "", "konfs directory for kubeconfigs and tracking active konfs (default is $HOME/.kube/konfs)") rootCmd.PersistentFlags().BoolVar(&silent, "silent", false, "suppress log output if set to true (default is false)") - } -// wrapInit is required as cobra.OnInitialize only accepts func() as interface -func wrapInit() { +func initConfig() error { conf, err := config.ConfFromHomeDir() - cobra.CheckErr(err) + if err != nil { + return err + } if konfDir != "" { conf.KonfDir = konfDir @@ -55,7 +71,16 @@ func wrapInit() { } config.InitWithOverrides(conf) + return nil +} - err = utils.EnsureDir(afero.NewOsFs()) - cobra.CheckErr(err) +func initCommands() { + rootCmd.AddCommand(cleanupCmd) + rootCmd.AddCommand(newCompletionCmd().cmd) + rootCmd.AddCommand(newDeleteCommand().cmd) + rootCmd.AddCommand(newImportCmd().cmd) + rootCmd.AddCommand(newNamespaceCmd().cmd) + rootCmd.AddCommand(newSetCommand().cmd) + rootCmd.AddCommand(newShellwrapperCmd().cmd) + rootCmd.AddCommand(newVersionCommand().cmd) } diff --git a/cmd/set.go b/cmd/set.go index 547f8bb..1d1f3f2 100644 --- a/cmd/set.go +++ b/cmd/set.go @@ -195,7 +195,3 @@ func createSetPrompt(options []*store.Metadata) *promptui.Select { } return &prompt } - -func init() { - rootCmd.AddCommand(newSetCommand().cmd) -} diff --git a/cmd/shellwrapper.go b/cmd/shellwrapper.go index 1ac1dc9..17314c2 100644 --- a/cmd/shellwrapper.go +++ b/cmd/shellwrapper.go @@ -83,7 +83,3 @@ trap konf_cleanup EXIT return nil } - -func init() { - rootCmd.AddCommand(newShellwrapperCmd().cmd) -} diff --git a/cmd/version.go b/cmd/version.go index a0296d8..569b507 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -71,7 +71,3 @@ func versionStringWithOverrides(gitversion string, gitcommit string, builddate s } return fmt.Sprintf(`{"GitVersion":"%s","GitCommit":"%s","BuildDate":"%s","GoVersion":"%s","Platform":"%s","Compiler":"%s"}`, v.GitVersion, v.GitCommit, v.BuildDate, v.GoVersion, v.Platform, v.Compiler) } - -func init() { - rootCmd.AddCommand(newVersionCommand().cmd) -} diff --git a/main.go b/main.go index c40fbd2..a4d20ba 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,15 @@ package main -import "github.com/simontheleg/konf-go/cmd" +import ( + "fmt" + "os" + + "github.com/simontheleg/konf-go/cmd" +) func main() { - cmd.Execute() + if err := cmd.Execute(); err != nil { + fmt.Fprintf(os.Stderr, "konf execution has failed: %q\n", err) + os.Exit(1) + } }