8000 refactor: set chaincmd commands as version agnostic by lumtis · Pull Request #614 · ignite/cli · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

refactor: set chaincmd commands as version agnostic #614

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

Merged
merged 8 commits into from
Jan 5, 2021
Merged
Show file tree
Hide file tree
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
85 changes: 58 additions & 27 deletions starport/pkg/chaincmd/chaincmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -85,8 +86,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
}
Expand All @@ -97,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
Expand All @@ -107,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
Expand All @@ -120,7 +122,8 @@ func (c ChainCmd) AddKeyCommand(accountName string) step.Option {
constJSON,
}
command = c.attachKeyringBackend(command)
return step.Exec(c.appCmd, c.attachHome(command)...)

return c.cliCommand(command)
}

// ImportKeyCommand returns the command to import a key into the chain keyring from a mnemonic
Expand All @@ -132,7 +135,8 @@ func (c ChainCmd) ImportKeyCommand(accountName string) step.Option {
optionRecover,
}
command = c.attachKeyringBackend(command)
return step.Exec(c.appCmd, c.attachHome(command)...)

return c.cliCommand(command)
}

// ShowKeyAddressCommand returns the command to print the address of a key in the chain keyring
Expand All @@ -144,7 +148,8 @@ func (c ChainCmd) ShowKeyAddressCommand(accountName string) step.Option {
optionAddress,
}
command = c.attachKeyringBackend(command)
return step.Exec(c.appCmd, c.attachHome(command)...)

return c.cliCommand(command)
}

// AddGenesisAccountCommand returns the command to add a new account in the genesis file of the chain
Expand All @@ -154,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
Expand Down Expand Up @@ -226,39 +231,27 @@ 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...)
}

// Add necessary flags
command = c.attachChainID(command)
command = c.attachKeyringBackend(command)

return step.Exec(c.appCmd, c.attachHome(command)...)
return c.launchpadGentxCommand(validatorName, selfDelegation, options...)
}

// CollectGentxsCommand returns the command to gather the gentxs in /gentx dir into the genesis file of the chain
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
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
Expand All @@ -267,7 +260,25 @@ func (c ChainCmd) ShowNodeIDCommand() step.Option {
constTendermint,
commandShowNodeID,
}
return step.Exec(c.appCmd, c.attachHome(command)...)
return c.daemonCommand(command)
}

// LaunchpadSetConfigCommand returns the command to set config value
func (c ChainCmd) LaunchpadSetConfigCommand(name string, value string) step.Option {
// Check version
if c.isStargate() {
panic("config command doesn't exist for Stargate")
}
return c.launchpadSetConfigCommand(name, value)
}

// 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() {
panic("rest-server command doesn't exist for Stargate")
}
return c.launchpadRestServerCommand(apiAddress, rpcAddress)
}

// attachChainID appends the chain ID flag to the provided command
Expand All @@ -293,3 +304,23 @@ 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 == ""
}

// 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)...)
}
84 changes: 31 additions & 53 deletions starport/pkg/chaincmd/launchpad.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,55 +12,48 @@ const (
optionName = "--name"
)

// LaunchpadAddKeyCommand returns the command to add a new key in the chain keyring with Launchpad chain EDBE s
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 {
// launchpadGentxCommand returns the command to generate a gentx for the chain
func (c ChainCmd) launchpadGentxCommand(
validatorName string,
selfDelegation string,
options ...GentxOption,
) step.Option {
command := []string{
commandKeys,
"add",
accountName,
optionRecover,
commandGentx,
optionName,
validatorName,
optionAmount,
selfDelegation,
}
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,
// Apply the options provided by the user
for _, applyOption := range options {
command = applyOption(command)
}

command = c.attachKeyringBackend(command)
return step.Exec(c.cliCmd, c.attachHome(command)...)
return c.daemonCommand(command)
}

// LaunchpadSetConfigCommand
func (c ChainCmd) LaunchpadSetConfigCommand(name string, value string) step.Option {
// launchpadSetConfigCommand
func (c ChainCmd) launchpadSetConfigCommand(name string, value string) step.Option {
command := []string{
commandConfig,
name,
value,
}
return step.Exec(c.cliCmd, c.attachHome(command)...)
return c.cliCommand(command)
}

// LaunchpadRestServerCommand
func (c ChainCmd) LaunchpadRestServerCommand(apiAddress string, rpcAddress string) step.Option {
// launchpadRestServerCommand
func (c ChainCmd) launchpadRestServerCommand(apiAddress string, rpcAddress string) step.Option {
command := []string{
commandRestServer,
optionUnsafeCors,
Expand All @@ -69,28 +62,13 @@ func (c ChainCmd) LaunchpadRestServerCommand(apiAddress string, rpcAddress strin
optionRPCAddress,
rpcAddress,
}
return step.Exec(c.cliCmd, c.attachHome(command)...)
return c.cliCommand(command)
}

// LaunchpadGentxCommand returns the command to generate a gentx for the chain
func (c ChainCmd) LaunchpadGentxCommand(
validatorName string,
selfDelegation string,
options ...GentxOption,
) step.Option {
command := []string{
commandGentx,
optionName,
validatorName,
optionAmount,
selfDelegation,
}

// Apply the options provided by the user
for _, applyOption := range options {
command = applyOption(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}...)
}

command = c.attachKeyringBackend(command)
return step.Exec(c.appCmd, c.attachHome(command)...)
return command
}
28 changes: 28 additions & 0 deletions starport/pkg/chaincmd/stargate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package chaincmd

import "github.com/tendermint/starport/starport/pkg/cmdrunner/step"

// 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 c.daemonCommand(command)
}
10 changes: 5 additions & 5 deletions starport/services/chain/plugin-launchpad.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -66,19 +66,19 @@ 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 {
Expand All @@ -92,7 +92,7 @@ func (p *launchpadPlugin) ConfigCommands(chainID string) []step.Option {
}

func (p *launchpadPlugin) GentxCommand(v Validator) step.Option {
return p.cmd.LaunchpadGentxCommand(
return p.cmd.GentxCommand(
v.Name,
v.StakingAmount,
chaincmd.GentxWithMoniker(v.Moniker),
Expand Down
0