Package subcommand code

This commit is contained in:
2024-12-30 15:50:31 -05:00
parent 96378d047e
commit b9d7d5a4f2
51 changed files with 357 additions and 295 deletions

View File

@@ -12,6 +12,10 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"gitea.libretechconsulting.com/rmcguire/git-project-manager/cmd/alias"
"gitea.libretechconsulting.com/rmcguire/git-project-manager/cmd/cache"
conf "gitea.libretechconsulting.com/rmcguire/git-project-manager/cmd/config"
"gitea.libretechconsulting.com/rmcguire/git-project-manager/cmd/project"
"gitea.libretechconsulting.com/rmcguire/git-project-manager/cmd/util"
"gitea.libretechconsulting.com/rmcguire/git-project-manager/internal/config"
)
@@ -32,6 +36,7 @@ var (
// Hook traversal is enabled, so this will be run for all
// sub-commands regardless of their registered pre-hooks
func initRootCmd(cmd *cobra.Command, args []string) {
cmd.SetContext(util.AddToCtx(cmd.Context(), utils))
utils.InitProjectPath(cmd, args)
}
@@ -41,9 +46,6 @@ func Execute() {
ctx, cncl := signal.NotifyContext(context.Background(), os.Kill, os.Interrupt)
defer cncl()
// Store provided util.Utils in context
ctx = util.AddToCtx(ctx, utils)
err := rootCmd.ExecuteContext(ctx)
if err != nil {
pterm.Error.Printfln("%s", pterm.LightYellow("Command failed, "+err.Error()))
@@ -67,11 +69,17 @@ func init() {
rootCmd.PersistentFlags().StringSlice(util.FlagRemote, []string{},
"Specify remotes by host for any sub-command. Provide multiple times or comma delimited.")
viper.BindPFlags(rootCmd.PersistentFlags())
// Flag autocompletion
rootCmd.RegisterFlagCompletionFunc(util.FlagLogLevel, util.ValidLogLevelsFunc)
rootCmd.RegisterFlagCompletionFunc(util.FlagRemote, utils.ValidRemotesFunc)
rootCmd.RegisterFlagCompletionFunc(util.FlagRemote, util.ValidRemotesFunc)
viper.BindPFlags(rootCmd.PersistentFlags())
// Subcommands
rootCmd.AddCommand(alias.AliasCmd)
rootCmd.AddCommand(cache.CacheCmd)
rootCmd.AddCommand(conf.ConfigCmd)
rootCmd.AddCommand(project.ProjectCmd)
}
// initConfig reads in config file and ENV variables if set.
@@ -112,6 +120,8 @@ func getInitConfigFunc(utils *util.Utils) func() {
plog.Error("Failed loading config", plog.Args("err", err))
}
utils.SetConfig(conf)
if len(os.Args) > 0 && configExemptCommands.Match([]byte(os.Args[1])) {
plog.Debug("Permitting missing config for config sub-command")
return