Skip to content

Commit

Permalink
refactor: remove init() logic during startup
Browse files Browse the repository at this point in the history
This commit gets rid of the complicated and error-prone init() logic
that was used during startup.
  • Loading branch information
SimonTheLeg committed Oct 29, 2023
1 parent db87065 commit 6bff238
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 45 deletions.
4 changes: 0 additions & 4 deletions cmd/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,3 @@ func cleanLeftOvers(f afero.Fs) error {

return nil
}

func init() {
rootCmd.AddCommand(cleanupCmd)
}
4 changes: 0 additions & 4 deletions cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,3 @@ func (c *completionCmd) completion(cmd *cobra.Command, args []string) error {

return nil
}

func init() {
rootCmd.AddCommand(newCompletionCmd().cmd)
}
4 changes: 0 additions & 4 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,3 @@ func (c *deleteCmd) completeDelete(cmd *cobra.Command, args []string, toComplete

return sug, cobra.ShellCompDirectiveNoFileComp
}

func init() {
rootCmd.AddCommand(newDeleteCommand().cmd)
}
4 changes: 0 additions & 4 deletions cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,3 @@ func filesForDir(sm *store.Storemanager, path string) ([]*FileWithPath, error) {

return files, nil
}

func init() {
rootCmd.AddCommand(newImportCmd().cmd)
}
4 changes: 0 additions & 4 deletions cmd/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand Down
47 changes: 36 additions & 11 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
4 changes: 0 additions & 4 deletions cmd/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,3 @@ func createSetPrompt(options []*store.Metadata) *promptui.Select {
}
return &prompt
}

func init() {
rootCmd.AddCommand(newSetCommand().cmd)
}
4 changes: 0 additions & 4 deletions cmd/shellwrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,3 @@ trap konf_cleanup EXIT

return nil
}

func init() {
rootCmd.AddCommand(newShellwrapperCmd().cmd)
}
4 changes: 0 additions & 4 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
12 changes: 10 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 6bff238

Please sign in to comment.