From 35a384039f9e01649ffca5e8d48816507cd429b8 Mon Sep 17 00:00:00 2001 From: Dmitry <98899785+mdqst@users.noreply.github.com> Date: Wed, 30 Oct 2024 10:34:21 +0300 Subject: [PATCH] Update cmd.go Code optimization. Now the code works faster and more accurately. --- cmd.go | 114 +++++++++++++++++++++------------------------------------ 1 file changed, 41 insertions(+), 73 deletions(-) diff --git a/cmd.go b/cmd.go index de20c65..e0f6c3e 100644 --- a/cmd.go +++ b/cmd.go @@ -11,40 +11,31 @@ import ( "github.com/G7DAO/safes/bindings/SafeProxyFactory" ) -var SAFES_VERSION string = "0.0.1" +var SAFES_VERSION = "0.0.1" + +func main() { + if err := CreateRootCommand().Execute(); err != nil { + os.Exit(1) + } +} func CreateRootCommand() *cobra.Command { - // rootCmd represents the base command when called without any subcommands rootCmd := &cobra.Command{ Use: "game7", Short: "game7: CLI to the Game7 protocol", - Run: func(cmd *cobra.Command, args []string) { - cmd.Help() - }, + Run: func(cmd *cobra.Command, args []string) { cmd.Help() }, } - completionCmd := CreateCompletionCommand(rootCmd) - versionCmd := CreateVersionCommand() - - singletonCmd := Safe.CreateSafeCommand() - singletonCmd.Use = "singleton" - - singletonL2Cmd := SafeL2.CreateSafeL2Command() - singletonL2Cmd.Use = "singleton-l2" - - proxyCmd := SafeProxy.CreateSafeProxyCommand() - proxyCmd.Use = "proxy" - - factoryCmd := SafeProxyFactory.CreateSafeProxyFactoryCommand() - factoryCmd.Use = "factory" - - delegateCmd := CreateDelegateCmd() - - rootCmd.AddCommand(completionCmd, versionCmd, singletonCmd, singletonL2Cmd, proxyCmd, factoryCmd, delegateCmd) - - // By default, cobra Command objects write to stderr. We have to forcibly set them to output to - // stdout. rootCmd.SetOut(os.Stdout) + rootCmd.AddCommand( + CreateCompletionCommand(rootCmd), + CreateVersionCommand(), + createSafeCommand("singleton", Safe.CreateSafeCommand), + createSafeCommand("singleton-l2", SafeL2.CreateSafeL2Command), + createSafeCommand("proxy", SafeProxy.CreateSafeProxyCommand), + createSafeCommand("factory", SafeProxyFactory.CreateSafeProxyFactoryCommand), + CreateDelegateCmd(), + ) return rootCmd } @@ -53,64 +44,41 @@ func CreateCompletionCommand(rootCmd *cobra.Command) *cobra.Command { completionCmd := &cobra.Command{ Use: "completion", Short: "Generate shell completion scripts for game7", - Long: `Generate shell completion scripts for game7. - -The command for each shell will print a completion script to stdout. You can source this script to get -completions in your current shell session. You can add this script to the completion directory for your -shell to get completions for all future sessions. - -For example, to activate bash completions in your current shell: - $ . <(game7 completion bash) - -To add game7 completions for all bash sessions: - $ game7 completion bash > /etc/bash_completion.d/game7_completions`, - } - - bashCompletionCmd := &cobra.Command{ - Use: "bash", - Short: "bash completions for game7", - Run: func(cmd *cobra.Command, args []string) { - rootCmd.GenBashCompletion(cmd.OutOrStdout()) - }, + Long: `Generate shell completion scripts for game7. +Source the script to get completions in your shell session. For example, for bash: + $ . <(game7 completion bash)`, } - zshCompletionCmd := &cobra.Command{ - Use: "zsh", - Short: "zsh completions for game7", - Run: func(cmd *cobra.Command, args []string) { - rootCmd.GenZshCompletion(cmd.OutOrStdout()) - }, - } + addSubCommands(completionCmd, rootCmd) + return completionCmd +} - fishCompletionCmd := &cobra.Command{ - Use: "fish", - Short: "fish completions for game7", - Run: func(cmd *cobra.Command, args []string) { - rootCmd.GenFishCompletion(cmd.OutOrStdout(), true) - }, +func addSubCommands(cmd *cobra.Command, rootCmd *cobra.Command) { + subCommands := []struct { + use, short string + run func(cmd *cobra.Command, args []string) + }{ + {"bash", "bash completions for game7", func(cmd *cobra.Command, args []string) { rootCmd.GenBashCompletion(cmd.OutOrStdout()) }}, + {"zsh", "zsh completions for game7", func(cmd *cobra.Command, args []string) { rootCmd.GenZshCompletion(cmd.OutOrStdout()) }}, + {"fish", "fish completions for game7", func(cmd *cobra.Command, args []string) { rootCmd.GenFishCompletion(cmd.OutOrStdout(), true) }}, + {"powershell", "powershell completions for game7", func(cmd *cobra.Command, args []string) { rootCmd.GenPowerShellCompletion(cmd.OutOrStdout()) }}, } - powershellCompletionCmd := &cobra.Command{ - Use: "powershell", - Short: "powershell completions for game7", - Run: func(cmd *cobra.Command, args []string) { - rootCmd.GenPowerShellCompletion(cmd.OutOrStdout()) - }, + for _, sub := range subCommands { + cmd.AddCommand(&cobra.Command{Use: sub.use, Short: sub.short, Run: sub.run}) } - - completionCmd.AddCommand(bashCompletionCmd, zshCompletionCmd, fishCompletionCmd, powershellCompletionCmd) - - return completionCmd } func CreateVersionCommand() *cobra.Command { - versionCmd := &cobra.Command{ + return &cobra.Command{ Use: "version", Short: "Print the version of game7 that you are currently using", - Run: func(cmd *cobra.Command, args []string) { - cmd.Println(SAFES_VERSION) - }, + Run: func(cmd *cobra.Command, args []string) { cmd.Println(SAFES_VERSION) }, } +} - return versionCmd +func createSafeCommand(name string, factory func() *cobra.Command) *cobra.Command { + cmd := factory() + cmd.Use = name + return cmd }