package cmd import ( "github.com/spf13/cobra" ) // osrsCmd and rs3Cmd act as game-scoped parents. // All real subcommands are registered under both via factory functions. var osrsCmd = &cobra.Command{ Use: "osrs", Short: "Query the Old School RuneScape Wiki", PersistentPreRun: func(cmd *cobra.Command, args []string) { game = "osrs" }, } var rs3Cmd = &cobra.Command{ Use: "rs3", Short: "Query the RuneScape 3 Wiki", PersistentPreRun: func(cmd *cobra.Command, args []string) { game = "rs3" }, } func init() { rootCmd.AddCommand(osrsCmd) rootCmd.AddCommand(rs3Cmd) } // commandFactories holds functions that create fresh command instances. // Each subcommand registers a factory at init time. var commandFactories []func() *cobra.Command // RegisterCommand adds a command factory. Both osrs and rs3 will get // independent instances of the command. func RegisterCommand(factory func() *cobra.Command) { commandFactories = append(commandFactories, factory) } // wireCommands creates and attaches all subcommands to both game parents. // Called from root.go's init after all subcommand init()s have run. func wireCommands() { for _, factory := range commandFactories { osrsCmd.AddCommand(factory()) rs3Cmd.AddCommand(factory()) } }