From f921950b86bbc1f4932f022b3efc8c639d06a93a Mon Sep 17 00:00:00 2001 From: ltacker Date: Tue, 5 Jan 2021 12:12:55 +0100 Subject: [PATCH 1/8] Refactor chaincmd --- starport/pkg/chaincmd/chaincmd.go | 88 ++++++++++++++++-------------- starport/pkg/chaincmd/launchpad.go | 62 ++++++++++----------- starport/pkg/chaincmd/stargate.go | 66 ++++++++++++++++++++++ 3 files changed, 145 insertions(+), 71 deletions(-) create mode 100644 starport/pkg/chaincmd/stargate.go diff --git a/starport/pkg/chaincmd/chaincmd.go b/starport/pkg/chaincmd/chaincmd.go index 15a08be473..34738aa3c3 100644 --- a/starport/pkg/chaincmd/chaincmd.go +++ b/starport/pkg/chaincmd/chaincmd.go @@ -85,8 +85,9 @@ func WithKeyrinBackend(keyringBackend KeyringBackend) Option { } } -// WithLaunchpadCLI provides the name of the CLI application to call Launchpad CLI commands -func WithLaunchpadCLI(cliCmd string) Option { +// WithLaunchpad defines the command as Launchpad application commands +// and provides the name of the CLI application to call Launchpad CLI commands +func WithLaunchpad(cliCmd string) Option { return func(c *ChainCmd) { c.cliCmd = cliCmd } @@ -112,39 +113,32 @@ func (c ChainCmd) InitCommand(moniker string) step.Option { // AddKeyCommand returns the command to add a new key in the chain keyring func (c ChainCmd) AddKeyCommand(accountName string) step.Option { - command := []string{ - commandKeys, - "add", - accountName, - optionOutput, - constJSON, + // Check version + if c.isStargate() { + return c.stargateAddKeyCommand(accountName) + } else { + return c.launchpadAddKeyCommand(accountName) } - command = c.attachKeyringBackend(command) - return step.Exec(c.appCmd, c.attachHome(command)...) } // ImportKeyCommand returns the command to import a key into the chain keyring from a mnemonic func (c ChainCmd) ImportKeyCommand(accountName string) step.Option { - command := []string{ - commandKeys, - "add", - accountName, - optionRecover, + // Check version + if c.isStargate() { + return c.stargateImportKeyCommand(accountName) + } else { + return c.launchpadImportKeyCommand(accountName) } - command = c.attachKeyringBackend(command) - return step.Exec(c.appCmd, c.attachHome(command)...) } // ShowKeyAddressCommand returns the command to print the address of a key in the chain keyring func (c ChainCmd) ShowKeyAddressCommand(accountName string) step.Option { - command := []string{ - commandKeys, - "show", - accountName, - optionAddress, + // Check version + if c.isStargate() { + return c.stargateShowKeyAddressCommand(accountName) + } else { + return c.launchpadShowKeyAddressCommand(accountName) } - command = c.attachKeyringBackend(command) - return step.Exec(c.appCmd, c.attachHome(command)...) } // AddGenesisAccountCommand returns the command to add a new account in the genesis file of the chain @@ -226,23 +220,12 @@ func (c ChainCmd) GentxCommand( selfDelegation string, options ...GentxOption, ) step.Option { - command := []string{ - commandGentx, - validatorName, - optionAmount, - selfDelegation, - } - - // Apply the options provided by the user - for _, applyOption := range options { - command = applyOption(command) + // Check version + if c.isStargate() { + return c.stargateGentxCommand(validatorName, selfDelegation, options...) + } else { + return c.launchpadGentxCommand(validatorName, selfDelegation, options...) } - - // Add necessary flags - command = c.attachChainID(command) - command = c.attachKeyringBackend(command) - - return step.Exec(c.appCmd, c.attachHome(command)...) } // CollectGentxsCommand returns the command to gather the gentxs in /gentx dir into the genesis file of the chain @@ -270,6 +253,26 @@ func (c ChainCmd) ShowNodeIDCommand() step.Option { return step.Exec(c.appCmd, c.attachHome(command)...) } +// SetConfigCommand returns the command to set config value +func (c ChainCmd) SetConfigCommand(name string, value string) step.Option { + // Check version + if c.isStargate() { + return nil // not defined for Stargate + } else { + return c.launchpadSetConfigCommand(name, value) + } +} + +// RestServerCommand returns the command to start the CLI REST server +func (c ChainCmd) RestServerCommand(apiAddress string, rpcAddress string) step.Option { + // Check version + if c.isStargate() { + return nil // not defined for Stargate + } else { + return c.launchpadRestServerCommand(apiAddress, rpcAddress) + } +} + // attachChainID appends the chain ID flag to the provided command func (c ChainCmd) attachChainID(command []string) []string { if c.chainID != "" { @@ -293,3 +296,8 @@ func (c ChainCmd) attachHome(command []string) []string { } return command } + +// isStargate checks if the version for commands is Stargate +func (c ChainCmd) isStargate() bool { + return c.cliCmd == "" +} \ No newline at end of file diff --git a/starport/pkg/chaincmd/launchpad.go b/starport/pkg/chaincmd/launchpad.go index bb641c9451..ca2ce5a015 100644 --- a/starport/pkg/chaincmd/launchpad.go +++ b/starport/pkg/chaincmd/launchpad.go @@ -12,8 +12,8 @@ const ( optionName = "--name" ) -// LaunchpadAddKeyCommand returns the command to add a new key in the chain keyring with Launchpad chains -func (c ChainCmd) LaunchpadAddKeyCommand(accountName string) step.Option { +// launchpadAddKeyCommand returns the command to add a new key in the chain keyring with Launchpad chains +func (c ChainCmd) launchpadAddKeyCommand(accountName string) step.Option { command := []string{ commandKeys, "add", @@ -25,8 +25,8 @@ func (c ChainCmd) LaunchpadAddKeyCommand(accountName string) step.Option { return step.Exec(c.cliCmd, c.attachHome(command)...) } -// LaunchpadImportKeyCommand returns the command to import a key into the chain keyring from a mnemonic with Launchpad chains -func (c ChainCmd) LaunchpadImportKeyCommand(accountName string) step.Option { +// launchpadImportKeyCommand returns the command to import a key into the chain keyring from a mnemonic with Launchpad chains +func (c ChainCmd) launchpadImportKeyCommand(accountName string) step.Option { command := []string{ commandKeys, "add", @@ -37,8 +37,8 @@ func (c ChainCmd) LaunchpadImportKeyCommand(accountName string) step.Option { return step.Exec(c.cliCmd, c.attachHome(command)...) } -// LaunchpadShowKeyAddressCommand returns the command to print the address of a key in the chain keyring with Launchpad chains -func (c ChainCmd) LaunchpadShowKeyAddressCommand(accountName string) step.Option { +// launchpadShowKeyAddressCommand returns the command to print the address of a key in the chain keyring with Launchpad chains +func (c ChainCmd) launchpadShowKeyAddressCommand(accountName string) step.Option { command := []string{ commandKeys, "show", @@ -49,31 +49,8 @@ func (c ChainCmd) LaunchpadShowKeyAddressCommand(accountName string) step.Option return step.Exec(c.cliCmd, c.attachHome(command)...) } -// LaunchpadSetConfigCommand -func (c ChainCmd) LaunchpadSetConfigCommand(name string, value string) step.Option { - command := []string{ - commandConfig, - name, - value, - } - return step.Exec(c.cliCmd, c.attachHome(command)...) -} - -// LaunchpadRestServerCommand -func (c ChainCmd) LaunchpadRestServerCommand(apiAddress string, rpcAddress string) step.Option { - command := []string{ - commandRestServer, - optionUnsafeCors, - optionAPIAddress, - apiAddress, - optionRPCAddress, - rpcAddress, - } - return step.Exec(c.cliCmd, c.attachHome(command)...) -} - -// LaunchpadGentxCommand returns the command to generate a gentx for the chain -func (c ChainCmd) LaunchpadGentxCommand( +// launchpadGentxCommand returns the command to generate a gentx for the chain +func (c ChainCmd) launchpadGentxCommand( validatorName string, selfDelegation string, options ...GentxOption, @@ -94,3 +71,26 @@ func (c ChainCmd) LaunchpadGentxCommand( command = c.attachKeyringBackend(command) return step.Exec(c.appCmd, c.attachHome(command)...) } + +// launchpadSetConfigCommand +func (c ChainCmd) launchpadSetConfigCommand(name string, value string) step.Option { + command := []string{ + commandConfig, + name, + value, + } + return step.Exec(c.cliCmd, c.attachHome(command)...) +} + +// launchpadRestServerCommand +func (c ChainCmd) launchpadRestServerCommand(apiAddress string, rpcAddress string) step.Option { + command := []string{ + commandRestServer, + optionUnsafeCors, + optionAPIAddress, + apiAddress, + optionRPCAddress, + rpcAddress, + } + return step.Exec(c.cliCmd, c.attachHome(command)...) +} \ No newline at end of file diff --git a/starport/pkg/chaincmd/stargate.go b/starport/pkg/chaincmd/stargate.go new file mode 100644 index 0000000000..7467578160 --- /dev/null +++ b/starport/pkg/chaincmd/stargate.go @@ -0,0 +1,66 @@ +package chaincmd + +import "github.com/tendermint/starport/starport/pkg/cmdrunner/step" + + +// stargateAddKeyCommand returns the command to add a new key in the chain keyring +func (c ChainCmd) stargateAddKeyCommand(accountName string) step.Option { + command := []string{ + commandKeys, + "add", + accountName, + optionOutput, + constJSON, + } + command = c.attachKeyringBackend(command) + return step.Exec(c.appCmd, c.attachHome(command)...) +} + +// stargateImportKeyCommand returns the command to import a key into the chain keyring from a mnemonic +func (c ChainCmd) stargateImportKeyCommand(accountName string) step.Option { + command := []string{ + commandKeys, + "add", + accountName, + optionRecover, + } + command = c.attachKeyringBackend(command) + return step.Exec(c.appCmd, c.attachHome(command)...) +} + +// stargateShowKeyAddressCommand returns the command to print the address of a key in the chain keyring +func (c ChainCmd) stargateShowKeyAddressCommand(accountName string) step.Option { + command := []string{ + commandKeys, + "show", + accountName, + optionAddress, + } + command = c.attachKeyringBackend(command) + return step.Exec(c.appCmd, c.attachHome(command)...) +} + +// stargateGentxCommand returns the command to generate a gentx for the chain +func (c ChainCmd) stargateGentxCommand( + validatorName string, + selfDelegation string, + options ...GentxOption, +) step.Option { + command := []string{ + commandGentx, + validatorName, + optionAmount, + selfDelegation, + } + + // Apply the options provided by the user + for _, applyOption := range options { + command = applyOption(command) + } + + // Add necessary flags + command = c.attachChainID(command) + command = c.attachKeyringBackend(command) + + return step.Exec(c.appCmd, c.attachHome(command)...) +} From 9eab6458edfb24e9eed06375462bfda76e720a0c Mon Sep 17 00:00:00 2001 From: ltacker Date: Tue, 5 Jan 2021 12:14:49 +0100 Subject: [PATCH 2/8] Renaming in plugin --- starport/services/chain/plugin-launchpad.go | 22 ++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/starport/services/chain/plugin-launchpad.go b/starport/services/chain/plugin-launchpad.go index 27ed029288..cb4ef04b90 100644 --- a/starport/services/chain/plugin-launchpad.go +++ b/starport/services/chain/plugin-launchpad.go @@ -26,7 +26,7 @@ func newLaunchpadPlugin(app App) *launchpadPlugin { cmd := chaincmd.New( app.D(), chaincmd.WithKeyrinBackend(chaincmd.KeyringBackendTest), - chaincmd.WithLaunchpadCLI(app.CLI()), + chaincmd.WithLaunchpad(app.CLI()), ) return &launchpadPlugin{ @@ -66,33 +66,33 @@ func (p *launchpadPlugin) Binaries() []string { } func (p *launchpadPlugin) AddUserCommand(accountName string) step.Options { - return step.NewOptions().Add(p.cmd.LaunchpadAddKeyCommand(accountName)) + return step.NewOptions().Add(p.cmd.AddKeyCommand(accountName)) } func (p *launchpadPlugin) ImportUserCommand(name, mnemonic string) step.Options { return step.NewOptions(). Add( - p.cmd.LaunchpadImportKeyCommand(name), + p.cmd.ImportKeyCommand(name), step.Write([]byte(mnemonic+"\n")), ) } func (p *launchpadPlugin) ShowAccountCommand(accountName string) step.Option { - return p.cmd.LaunchpadShowKeyAddressCommand(accountName) + return p.cmd.ShowKeyAddressCommand(accountName) } func (p *launchpadPlugin) ConfigCommands(chainID string) []step.Option { return []step.Option{ - p.cmd.LaunchpadSetConfigCommand("keyring-backend", "test"), - p.cmd.LaunchpadSetConfigCommand("chain-id", chainID), - p.cmd.LaunchpadSetConfigCommand("output", "json"), - p.cmd.LaunchpadSetConfigCommand("indent", "true"), - p.cmd.LaunchpadSetConfigCommand("trust-node", "true"), + p.cmd.SetConfigCommand("keyring-backend", "test"), + p.cmd.SetConfigCommand("chain-id", chainID), + p.cmd.SetConfigCommand("output", "json"), + p.cmd.SetConfigCommand("indent", "true"), + p.cmd.SetConfigCommand("trust-node", "true"), } } func (p *launchpadPlugin) GentxCommand(v Validator) step.Option { - return p.cmd.LaunchpadGentxCommand( + return p.cmd.GentxCommand( v.Name, v.StakingAmount, chaincmd.GentxWithMoniker(v.Moniker), @@ -143,7 +143,7 @@ func (p *launchpadPlugin) StartCommands(conf starportconf.Config) [][]step.Optio ), step.NewOptions(). Add( - p.cmd.LaunchpadRestServerCommand(xurl.TCP(conf.Servers.APIAddr), xurl.TCP(conf.Servers.RPCAddr)), + p.cmd.RestServerCommand(xurl.TCP(conf.Servers.APIAddr), xurl.TCP(conf.Servers.RPCAddr)), step.PostExec(func(exitErr error) error { return errors.Wrapf(exitErr, "cannot run %[1]vcli rest-server", p.app.Name) }), From e3ab25648284a2a4d0a82321d5b026b19ded0622 Mon Sep 17 00:00:00 2001 From: ltacker Date: Tue, 5 Jan 2021 12:17:17 +0100 Subject: [PATCH 3/8] Lint --- starport/pkg/chaincmd/chaincmd.go | 24 +++++++++--------------- starport/pkg/chaincmd/launchpad.go | 2 +- starport/pkg/chaincmd/stargate.go | 1 - 3 files changed, 10 insertions(+), 17 deletions(-) diff --git a/starport/pkg/chaincmd/chaincmd.go b/starport/pkg/chaincmd/chaincmd.go index 34738aa3c3..b828876dbc 100644 --- a/starport/pkg/chaincmd/chaincmd.go +++ b/starport/pkg/chaincmd/chaincmd.go @@ -116,9 +116,8 @@ func (c ChainCmd) AddKeyCommand(accountName string) step.Option { // Check version if c.isStargate() { return c.stargateAddKeyCommand(accountName) - } else { - return c.launchpadAddKeyCommand(accountName) } + return c.launchpadAddKeyCommand(accountName) } // ImportKeyCommand returns the command to import a key into the chain keyring from a mnemonic @@ -126,9 +125,8 @@ func (c ChainCmd) ImportKeyCommand(accountName string) step.Option { // Check version if c.isStargate() { return c.stargateImportKeyCommand(accountName) - } else { - return c.launchpadImportKeyCommand(accountName) } + return c.launchpadImportKeyCommand(accountName) } // ShowKeyAddressCommand returns the command to print the address of a key in the chain keyring @@ -136,9 +134,8 @@ func (c ChainCmd) ShowKeyAddressCommand(accountName string) step.Option { // Check version if c.isStargate() { return c.stargateShowKeyAddressCommand(accountName) - } else { - return c.launchpadShowKeyAddressCommand(accountName) } + return c.launchpadShowKeyAddressCommand(accountName) } // AddGenesisAccountCommand returns the command to add a new account in the genesis file of the chain @@ -223,9 +220,8 @@ func (c ChainCmd) GentxCommand( // Check version if c.isStargate() { return c.stargateGentxCommand(validatorName, selfDelegation, options...) - } else { - return c.launchpadGentxCommand(validatorName, selfDelegation, options...) } + return c.launchpadGentxCommand(validatorName, selfDelegation, options...) } // CollectGentxsCommand returns the command to gather the gentxs in /gentx dir into the genesis file of the chain @@ -257,20 +253,18 @@ func (c ChainCmd) ShowNodeIDCommand() step.Option { func (c ChainCmd) SetConfigCommand(name string, value string) step.Option { // Check version if c.isStargate() { - return nil // not defined for Stargate - } else { - return c.launchpadSetConfigCommand(name, value) + return nil // not defined for Stargate } + return c.launchpadSetConfigCommand(name, value) } // RestServerCommand returns the command to start the CLI REST server func (c ChainCmd) RestServerCommand(apiAddress string, rpcAddress string) step.Option { // Check version if c.isStargate() { - return nil // not defined for Stargate - } else { - return c.launchpadRestServerCommand(apiAddress, rpcAddress) + return nil // not defined for Stargate } + return c.launchpadRestServerCommand(apiAddress, rpcAddress) } // attachChainID appends the chain ID flag to the provided command @@ -300,4 +294,4 @@ func (c ChainCmd) attachHome(command []string) []string { // isStargate checks if the version for commands is Stargate func (c ChainCmd) isStargate() bool { return c.cliCmd == "" -} \ No newline at end of file +} diff --git a/starport/pkg/chaincmd/launchpad.go b/starport/pkg/chaincmd/launchpad.go index ca2ce5a015..ce37e486b8 100644 --- a/starport/pkg/chaincmd/launchpad.go +++ b/starport/pkg/chaincmd/launchpad.go @@ -93,4 +93,4 @@ func (c ChainCmd) launchpadRestServerCommand(apiAddress string, rpcAddress strin rpcAddress, } return step.Exec(c.cliCmd, c.attachHome(command)...) -} \ No newline at end of file +} diff --git a/starport/pkg/chaincmd/stargate.go b/starport/pkg/chaincmd/stargate.go index 7467578160..fdc980ae67 100644 --- a/starport/pkg/chaincmd/stargate.go +++ b/starport/pkg/chaincmd/stargate.go @@ -2,7 +2,6 @@ package chaincmd import "github.com/tendermint/starport/starport/pkg/cmdrunner/step" - // stargateAddKeyCommand returns the command to add a new key in the chain keyring func (c ChainCmd) stargateAddKeyCommand(accountName string) step.Option { command := []string{ From 8a22a828e6d9e95b1bb473a0f9d2419c3676ae8d Mon Sep 17 00:00:00 2001 From: ltacker Date: Tue, 5 Jan 2021 12:41:22 +0100 Subject: [PATCH 4/8] Factorize some code --- starport/pkg/chaincmd/chaincmd.go | 37 ++++++++++++++++++++---- starport/pkg/chaincmd/launchpad.go | 46 ++++++++---------------------- starport/pkg/chaincmd/stargate.go | 37 ------------------------ 3 files changed, 43 insertions(+), 77 deletions(-) diff --git a/starport/pkg/chaincmd/chaincmd.go b/starport/pkg/chaincmd/chaincmd.go index b828876dbc..3528fdde61 100644 --- a/starport/pkg/chaincmd/chaincmd.go +++ b/starport/pkg/chaincmd/chaincmd.go @@ -46,6 +46,7 @@ type ChainCmd struct { homeDir string keyringBackend KeyringBackend cliCmd string + cliHome string } // New creates a new ChainCmd to launch command with the chain app @@ -113,29 +114,53 @@ func (c ChainCmd) InitCommand(moniker string) step.Option { // AddKeyCommand returns the command to add a new key in the chain keyring func (c ChainCmd) AddKeyCommand(accountName string) step.Option { + command := []string{ + commandKeys, + "add", + accountName, + optionRecover, + } + command = c.attachKeyringBackend(command) + // Check version if c.isStargate() { - return c.stargateAddKeyCommand(accountName) + return step.Exec(c.appCmd, c.attachHome(command)...) } - return c.launchpadAddKeyCommand(accountName) + return step.Exec(c.cliCmd, c.attachCLIHome(command)...) } // ImportKeyCommand returns the command to import a key into the chain keyring from a mnemonic func (c ChainCmd) ImportKeyCommand(accountName string) step.Option { + command := []string{ + commandKeys, + "add", + accountName, + optionRecover, + } + command = c.attachKeyringBackend(command) + // Check version if c.isStargate() { - return c.stargateImportKeyCommand(accountName) + return step.Exec(c.appCmd, c.attachHome(command)...) } - return c.launchpadImportKeyCommand(accountName) + return step.Exec(c.cliCmd, c.attachCLIHome(command)...) } // ShowKeyAddressCommand returns the command to print the address of a key in the chain keyring func (c ChainCmd) ShowKeyAddressCommand(accountName string) step.Option { + command := []string{ + commandKeys, + "show", + accountName, + optionAddress, + } + command = c.attachKeyringBackend(command) + // Check version if c.isStargate() { - return c.stargateShowKeyAddressCommand(accountName) + return step.Exec(c.appCmd, c.attachHome(command)...) } - return c.launchpadShowKeyAddressCommand(accountName) + return step.Exec(c.cliCmd, c.attachCLIHome(command)...) } // AddGenesisAccountCommand returns the command to add a new account in the genesis file of the chain diff --git a/starport/pkg/chaincmd/launchpad.go b/starport/pkg/chaincmd/launchpad.go index ce37e486b8..f6317f4d90 100644 --- a/starport/pkg/chaincmd/launchpad.go +++ b/starport/pkg/chaincmd/launchpad.go @@ -12,41 +12,11 @@ const ( optionName = "--name" ) -// launchpadAddKeyCommand returns the command to add a new key in the chain keyring with Launchpad chains -func (c ChainCmd) launchpadAddKeyCommand(accountName string) step.Option { - command := []string{ - commandKeys, - "add", - accountName, - optionOutput, - constJSON, +// WithLaunchpadCLIHome replaces the default home used by the Launchpad chain CLI +func WithLaunchpadCLIHome(cliHome string) Option { + return func(c *ChainCmd) { + c.cliHome = cliHome } - command = c.attachKeyringBackend(command) - return step.Exec(c.cliCmd, c.attachHome(command)...) -} - -// launchpadImportKeyCommand returns the command to import a key into the chain keyring from a mnemonic with Launchpad chains -func (c ChainCmd) launchpadImportKeyCommand(accountName string) step.Option { - command := []string{ - commandKeys, - "add", - accountName, - optionRecover, - } - command = c.attachKeyringBackend(command) - return step.Exec(c.cliCmd, c.attachHome(command)...) -} - -// launchpadShowKeyAddressCommand returns the command to print the address of a key in the chain keyring with Launchpad chains -func (c ChainCmd) launchpadShowKeyAddressCommand(accountName string) step.Option { - command := []string{ - commandKeys, - "show", - accountName, - optionAddress, - } - command = c.attachKeyringBackend(command) - return step.Exec(c.cliCmd, c.attachHome(command)...) } // launchpadGentxCommand returns the command to generate a gentx for the chain @@ -94,3 +64,11 @@ func (c ChainCmd) launchpadRestServerCommand(apiAddress string, rpcAddress strin } return step.Exec(c.cliCmd, c.attachHome(command)...) } + +// attachCLIHome appends the home flag to the provided CLI command +func (c ChainCmd) attachCLIHome(command []string) []string { + if c.cliHome != "" { + command = append(command, []string{optionHome, c.cliHome}...) + } + return command +} \ No newline at end of file diff --git a/starport/pkg/chaincmd/stargate.go b/starport/pkg/chaincmd/stargate.go index fdc980ae67..aed3953788 100644 --- a/starport/pkg/chaincmd/stargate.go +++ b/starport/pkg/chaincmd/stargate.go @@ -2,43 +2,6 @@ package chaincmd import "github.com/tendermint/starport/starport/pkg/cmdrunner/step" -// stargateAddKeyCommand returns the command to add a new key in the chain keyring -func (c ChainCmd) stargateAddKeyCommand(accountName string) step.Option { - command := []string{ - commandKeys, - "add", - accountName, - optionOutput, - constJSON, - } - command = c.attachKeyringBackend(command) - return step.Exec(c.appCmd, c.attachHome(command)...) -} - -// stargateImportKeyCommand returns the command to import a key into the chain keyring from a mnemonic -func (c ChainCmd) stargateImportKeyCommand(accountName string) step.Option { - command := []string{ - commandKeys, - "add", - accountName, - optionRecover, - } - command = c.attachKeyringBackend(command) - return step.Exec(c.appCmd, c.attachHome(command)...) -} - -// stargateShowKeyAddressCommand returns the command to print the address of a key in the chain keyring -func (c ChainCmd) stargateShowKeyAddressCommand(accountName string) step.Option { - command := []string{ - commandKeys, - "show", - accountName, - optionAddress, - } - command = c.attachKeyringBackend(command) - return step.Exec(c.appCmd, c.attachHome(command)...) -} - // stargateGentxCommand returns the command to generate a gentx for the chain func (c ChainCmd) stargateGentxCommand( validatorName string, From 6417806d4769e8c0b1de479df95f93720d2df8db Mon Sep 17 00:00:00 2001 From: ltacker Date: Tue, 5 Jan 2021 12:43:53 +0100 Subject: [PATCH 5/8] Fix add key error --- starport/pkg/chaincmd/chaincmd.go | 3 ++- starport/pkg/chaincmd/launchpad.go | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/starport/pkg/chaincmd/chaincmd.go b/starport/pkg/chaincmd/chaincmd.go index 3528fdde61..90a857e18c 100644 --- a/starport/pkg/chaincmd/chaincmd.go +++ b/starport/pkg/chaincmd/chaincmd.go @@ -118,7 +118,8 @@ func (c ChainCmd) AddKeyCommand(accountName string) step.Option { commandKeys, "add", accountName, - optionRecover, + optionOutput, + constJSON, } command = c.attachKeyringBackend(command) diff --git a/starport/pkg/chaincmd/launchpad.go b/starport/pkg/chaincmd/launchpad.go index f6317f4d90..9e152870f0 100644 --- a/starport/pkg/chaincmd/launchpad.go +++ b/starport/pkg/chaincmd/launchpad.go @@ -71,4 +71,4 @@ func (c ChainCmd) attachCLIHome(command []string) []string { command = append(command, []string{optionHome, c.cliHome}...) } return command -} \ No newline at end of file +} From f3819ee5a2e7f3fecfe08087df77943c3a4311d7 Mon Sep 17 00:00:00 2001 From: ltacker Date: Tue, 5 Jan 2021 13:36:07 +0100 Subject: [PATCH 6/8] Panic on non existing command --- starport/pkg/chaincmd/chaincmd.go | 8 ++++---- starport/services/chain/plugin-launchpad.go | 12 ++++++------ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/starport/pkg/chaincmd/chaincmd.go b/starport/pkg/chaincmd/chaincmd.go index 90a857e18c..cfd6f950ba 100644 --- a/starport/pkg/chaincmd/chaincmd.go +++ b/starport/pkg/chaincmd/chaincmd.go @@ -276,19 +276,19 @@ func (c ChainCmd) ShowNodeIDCommand() step.Option { } // SetConfigCommand returns the command to set config value -func (c ChainCmd) SetConfigCommand(name string, value string) step.Option { +func (c ChainCmd) LaunchpadSetConfigCommand(name string, value string) step.Option { // Check version if c.isStargate() { - return nil // not defined for Stargate + panic("config command doesn't exist for Stargate") } return c.launchpadSetConfigCommand(name, value) } // RestServerCommand returns the command to start the CLI REST server -func (c ChainCmd) RestServerCommand(apiAddress string, rpcAddress string) step.Option { +func (c ChainCmd) LaunchpadRestServerCommand(apiAddress string, rpcAddress string) step.Option { // Check version if c.isStargate() { - return nil // not defined for Stargate + panic("rest-server command doesn't exist for Stargate") } return c.launchpadRestServerCommand(apiAddress, rpcAddress) } diff --git a/starport/services/chain/plugin-launchpad.go b/starport/services/chain/plugin-launchpad.go index cb4ef04b90..c953febdb9 100644 --- a/starport/services/chain/plugin-launchpad.go +++ b/starport/services/chain/plugin-launchpad.go @@ -83,11 +83,11 @@ func (p *launchpadPlugin) ShowAccountCommand(accountName string) step.Option { func (p *launchpadPlugin) ConfigCommands(chainID string) []step.Option { return []step.Option{ - p.cmd.SetConfigCommand("keyring-backend", "test"), - p.cmd.SetConfigCommand("chain-id", chainID), - p.cmd.SetConfigCommand("output", "json"), - p.cmd.SetConfigCommand("indent", "true"), - p.cmd.SetConfigCommand("trust-node", "true"), + p.cmd.LaunchpadSetConfigCommand("keyring-backend", "test"), + p.cmd.LaunchpadSetConfigCommand("chain-id", chainID), + p.cmd.LaunchpadSetConfigCommand("output", "json"), + p.cmd.LaunchpadSetConfigCommand("indent", "true"), + p.cmd.LaunchpadSetConfigCommand("trust-node", "true"), } } @@ -143,7 +143,7 @@ func (p *launchpadPlugin) StartCommands(conf starportconf.Config) [][]step.Optio ), step.NewOptions(). Add( - p.cmd.RestServerCommand(xurl.TCP(conf.Servers.APIAddr), xurl.TCP(conf.Servers.RPCAddr)), + p.cmd.LaunchpadRestServerCommand(xurl.TCP(conf.Servers.APIAddr), xurl.TCP(conf.Servers.RPCAddr)), step.PostExec(func(exitErr error) error { return errors.Wrapf(exitErr, "cannot run %[1]vcli rest-server", p.app.Name) }), From 6caf7f0ef15d378ae86d7775e5fb9745399cc875 Mon Sep 17 00:00:00 2001 From: ltacker Date: Tue, 5 Jan 2021 13:56:09 +0100 Subject: [PATCH 7/8] Factorize cli and daemon commands --- starport/pkg/chaincmd/chaincmd.go | 45 ++++++++++++++++-------------- starport/pkg/chaincmd/launchpad.go | 6 ++-- starport/pkg/chaincmd/stargate.go | 2 +- 3 files changed, 28 insertions(+), 25 deletions(-) diff --git a/starport/pkg/chaincmd/chaincmd.go b/starport/pkg/chaincmd/chaincmd.go index cfd6f950ba..efed9cfe44 100644 --- a/starport/pkg/chaincmd/chaincmd.go +++ b/starport/pkg/chaincmd/chaincmd.go @@ -99,7 +99,7 @@ func (c ChainCmd) StartCommand(options ...string) step.Option { command := append([]string{ commandStart, }, options...) - return step.Exec(c.appCmd, c.attachHome(command)...) + return c.daemonCommand(command) } // InitCommand returns the command to initialize the chain @@ -109,7 +109,7 @@ func (c ChainCmd) InitCommand(moniker string) step.Option { moniker, } command = c.attachChainID(command) - return step.Exec(c.appCmd, c.attachHome(command)...) + return c.daemonCommand(command) } // AddKeyCommand returns the command to add a new key in the chain keyring @@ -123,11 +123,7 @@ func (c ChainCmd) AddKeyCommand(accountName string) step.Option { } command = c.attachKeyringBackend(command) - // Check version - if c.isStargate() { - return step.Exec(c.appCmd, c.attachHome(command)...) - } - return step.Exec(c.cliCmd, c.attachCLIHome(command)...) + return c.cliCommand(command) } // ImportKeyCommand returns the command to import a key into the chain keyring from a mnemonic @@ -140,11 +136,7 @@ func (c ChainCmd) ImportKeyCommand(accountName string) step.Option { } command = c.attachKeyringBackend(command) - // Check version - if c.isStargate() { - return step.Exec(c.appCmd, c.attachHome(command)...) - } - return step.Exec(c.cliCmd, c.attachCLIHome(command)...) + return c.cliCommand(command) } // ShowKeyAddressCommand returns the command to print the address of a key in the chain keyring @@ -157,11 +149,7 @@ func (c ChainCmd) ShowKeyAddressCommand(accountName string) step.Option { } command = c.attachKeyringBackend(command) - // Check version - if c.isStargate() { - return step.Exec(c.appCmd, c.attachHome(command)...) - } - return step.Exec(c.cliCmd, c.attachCLIHome(command)...) + return c.cliCommand(command) } // AddGenesisAccountCommand returns the command to add a new account in the genesis file of the chain @@ -171,7 +159,7 @@ func (c ChainCmd) AddGenesisAccountCommand(address string, coins string) step.Op address, coins, } - return step.Exec(c.appCmd, c.attachHome(command)...) + return c.daemonCommand(command) } // Options for the GentxCommand @@ -255,7 +243,7 @@ func (c ChainCmd) CollectGentxsCommand() step.Option { command := []string{ commandCollectGentxs, } - return step.Exec(c.appCmd, c.attachHome(command)...) + return c.daemonCommand(command) } // ValidateGenesisCommand returns the command to check the validity of the chain genesis @@ -263,7 +251,7 @@ func (c ChainCmd) ValidateGenesisCommand() step.Option { command := []string{ commandValidateGenesis, } - return step.Exec(c.appCmd, c.attachHome(command)...) + return c.daemonCommand(command) } // ShowNodeIDCommand returns the command to print the node ID of the node for the chain @@ -272,7 +260,7 @@ func (c ChainCmd) ShowNodeIDCommand() step.Option { constTendermint, commandShowNodeID, } - return step.Exec(c.appCmd, c.attachHome(command)...) + return c.daemonCommand(command) } // SetConfigCommand returns the command to set config value @@ -321,3 +309,18 @@ func (c ChainCmd) attachHome(command []string) []string { func (c ChainCmd) isStargate() bool { return c.cliCmd == "" } + +// daemonCommand returns the daemon command from the provided command +func (c ChainCmd) daemonCommand(command []string) step.Option { + return step.Exec(c.appCmd, c.attachHome(command)...) +} + +// cliCommand returns the cli command from the provided command +// cli is the daemon for Stargate +func (c ChainCmd) cliCommand(command []string) step.Option { + // Check version + if c.isStargate() { + return step.Exec(c.appCmd, c.attachHome(command)...) + } + return step.Exec(c.cliCmd, c.attachCLIHome(command)...) +} diff --git a/starport/pkg/chaincmd/launchpad.go b/starport/pkg/chaincmd/launchpad.go index 9e152870f0..cb81a106dd 100644 --- a/starport/pkg/chaincmd/launchpad.go +++ b/starport/pkg/chaincmd/launchpad.go @@ -39,7 +39,7 @@ func (c ChainCmd) launchpadGentxCommand( } command = c.attachKeyringBackend(command) - return step.Exec(c.appCmd, c.attachHome(command)...) + return c.daemonCommand(command) } // launchpadSetConfigCommand @@ -49,7 +49,7 @@ func (c ChainCmd) launchpadSetConfigCommand(name string, value string) step.Opti name, value, } - return step.Exec(c.cliCmd, c.attachHome(command)...) + return c.cliCommand(command) } // launchpadRestServerCommand @@ -62,7 +62,7 @@ func (c ChainCmd) launchpadRestServerCommand(apiAddress string, rpcAddress strin optionRPCAddress, rpcAddress, } - return step.Exec(c.cliCmd, c.attachHome(command)...) + return c.cliCommand(command) } // attachCLIHome appends the home flag to the provided CLI command diff --git a/starport/pkg/chaincmd/stargate.go b/starport/pkg/chaincmd/stargate.go index aed3953788..db734cefbb 100644 --- a/starport/pkg/chaincmd/stargate.go +++ b/starport/pkg/chaincmd/stargate.go @@ -24,5 +24,5 @@ func (c ChainCmd) stargateGentxCommand( command = c.attachChainID(command) command = c.attachKeyringBackend(command) - return step.Exec(c.appCmd, c.attachHome(command)...) + return c.daemonCommand(command) } From bd2e2fdebe8277a15fc749b790bf4f8502ca4cb0 Mon Sep 17 00:00:00 2001 From: ltacker Date: Tue, 5 Jan 2021 13:57:56 +0100 Subject: [PATCH 8/8] Update comments --- starport/pkg/chaincmd/chaincmd.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/starport/pkg/chaincmd/chaincmd.go b/starport/pkg/chaincmd/chaincmd.go index efed9cfe44..30dc75d6e0 100644 --- a/starport/pkg/chaincmd/chaincmd.go +++ b/starport/pkg/chaincmd/chaincmd.go @@ -263,7 +263,7 @@ func (c ChainCmd) ShowNodeIDCommand() step.Option { return c.daemonCommand(command) } -// SetConfigCommand returns the command to set config value +// LaunchpadSetConfigCommand returns the command to set config value func (c ChainCmd) LaunchpadSetConfigCommand(name string, value string) step.Option { // Check version if c.isStargate() { @@ -272,7 +272,7 @@ func (c ChainCmd) LaunchpadSetConfigCommand(name string, value string) step.Opti return c.launchpadSetConfigCommand(name, value) } -// RestServerCommand returns the command to start the CLI REST server +// LaunchpadRestServerCommand returns the command to start the CLI REST server func (c ChainCmd) LaunchpadRestServerCommand(apiAddress string, rpcAddress string) step.Option { // Check version if c.isStargate() {