func loadEnvCommandsReturnNewRoot(osArgs []string, rootCmd *cobra.Command) *cobra.Command { logs.WithField("path", ggn.Home.Config.WorkPath).Debug("Loading envs") work := work.NewWork(ggn.Home.Config.WorkPath) newRootCmd := &cobra.Command{ Use: "ggn", } for _, envNames := range work.ListEnvs() { env := work.LoadEnv(envNames) envCmd := &cobra.Command{ Use: env.GetName(), Short: "Run command for " + env.GetName(), Run: func(cmd *cobra.Command, args []string) { newRootCmd.AddCommand(prepareEnvCommands(env)) newRootCmd.SetArgs(osArgs[1:]) newRootCmd.Execute() }, } rootCmd.AddCommand(envCmd) } return newRootCmd }
or just source them in directly: $ . /etc/bash_completion`, Run: func(cmd *cobra.Command, args []string) { if autocompleteTarget == "" { autocompleteTarget = ggn.Home.Path + "/ggn_completion.sh" } if autocompleteType != "bash" { logs.WithField("type", autocompleteType).Fatal("Only Bash is supported for now") } work := work.NewWork(ggn.Home.Config.WorkPath) for _, command := range cmd.Root().Commands() { if command.Use != "genautocomplete" && command.Use != "version" && command.Use != "help [command]" { // TODO sux command.AddCommand(prepareEnvCommands(work.LoadEnv(command.Use))) } } err := cmd.Root().GenBashCompletionFile(autocompleteTarget) if err != nil { logs.WithE(err).Fatal("Failed to generate shell completion file") } else { logs.WithField("path", autocompleteTarget).Info("Bash completion saved") } os.Exit(0) }, } func init() { genautocompleteCmd.PersistentFlags().StringVarP(&autocompleteTarget, "completionfile", "", "", "Autocompletion file") genautocompleteCmd.PersistentFlags().StringVarP(&autocompleteType, "type", "", "bash", "Autocompletion type (currently only bash supported)")