Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update cmd.go #11

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 41 additions & 73 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}