Skip to content
This repository has been archived by the owner on Dec 4, 2024. It is now read-only.

feature/cli-commands-touchup #62

Merged
merged 22 commits into from
Jun 18, 2021
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
a02fe68
Format the output for some commands
zivkovicmilos Jun 9, 2021
4a392f9
Refactor the peers-add command to support multiple addresses and pret…
zivkovicmilos Jun 14, 2021
171a047
Finish up with the peers commands refactor
zivkovicmilos Jun 14, 2021
2061e33
Finish up the ibft commands touchup
zivkovicmilos Jun 14, 2021
e566ae3
Finish with the txpool touchup
zivkovicmilos Jun 14, 2021
b92544b
Finish up the touchup for remaining commands
zivkovicmilos Jun 14, 2021
108c786
Refactor the way flag descriptors are used, create a usage format
zivkovicmilos Jun 14, 2021
79c8f02
Remove unused func
zivkovicmilos Jun 14, 2021
4188fe3
Merge conflicts
zivkovicmilos Jun 14, 2021
e52baab
Fix typo in flag
zivkovicmilos Jun 14, 2021
a235d8a
Merge branch 'develop' into feature/cli-commands-touchup
zivkovicmilos Jun 15, 2021
fa22157
Merge branch 'develop' into feature/cli-commands-touchup
zivkovicmilos Jun 15, 2021
6a98f43
Refactor the way keys are being read in E2E tests
zivkovicmilos Jun 15, 2021
7c0aa64
Fix some things after the PR
zivkovicmilos Jun 15, 2021
df11546
Clean up the flag descriptor
zivkovicmilos Jun 15, 2021
2e09457
Update ibft propose
zivkovicmilos Jun 16, 2021
975ded0
Revert the commands to have subcommands, add descriptions for top lev…
zivkovicmilos Jun 16, 2021
3e24f01
Merge branch 'develop' into feature/cli-commands-touchup
zivkovicmilos Jun 16, 2021
34d62c0
Fix minor issue with e2e test command reading
zivkovicmilos Jun 16, 2021
a5800c5
Merge develop
zivkovicmilos Jun 16, 2021
010e141
Rename the temporary result field to better reflect the actual field
zivkovicmilos Jun 16, 2021
78e852c
Fix typo in method description
zivkovicmilos Jun 17, 2021
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
129 changes: 42 additions & 87 deletions command/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package command
import (
"flag"
"fmt"
"github.com/0xPolygon/minimal/minimal"
"os"
"strings"

"github.com/0xPolygon/minimal/command/server"
"github.com/0xPolygon/minimal/minimal"
"github.com/0xPolygon/minimal/types"
"github.com/mitchellh/cli"
"github.com/ryanuber/columnize"

"google.golang.org/grpc"
)

Expand All @@ -34,11 +35,12 @@ func Commands() map[string]cli.CommandFactory {
UI: ui,
}, nil
},
"dev": func() (cli.Command, error) {
return &DevCommand{
UI: ui,
}, nil
},
// TODO The task to implement the dev command should start here
//"dev": func() (cli.Command, error) {
// return &DevCommand{
// UI: ui,
// }, nil
//},
"genesis": func() (cli.Command, error) {
return &GenesisCommand{
UI: ui,
Expand All @@ -47,58 +49,58 @@ func Commands() map[string]cli.CommandFactory {

// PEER COMMANDS //

"peers add": func() (cli.Command, error) {
"peers-add": func() (cli.Command, error) {
return &PeersAdd{
Meta: meta,
}, nil
},
"peers status": func() (cli.Command, error) {
"peers-status": func() (cli.Command, error) {
return &PeersStatus{
Meta: meta,
}, nil
},
"peers list": func() (cli.Command, error) {
"peers-list": func() (cli.Command, error) {
return &PeersList{
Meta: meta,
}, nil
},

// IBFT COMMANDS //

"ibft init": func() (cli.Command, error) {
"ibft-init": func() (cli.Command, error) {
return &IbftInit{
Meta: meta,
}, nil
},
"ibft snapshot": func() (cli.Command, error) {
"ibft-snapshot": func() (cli.Command, error) {
return &IbftSnapshot{
Meta: meta,
}, nil
},
"ibft candidates": func() (cli.Command, error) {
"ibft-candidates": func() (cli.Command, error) {
return &IbftCandidates{
Meta: meta,
}, nil
},
"ibft propose": func() (cli.Command, error) {
"ibft-propose": func() (cli.Command, error) {
return &IbftPropose{
Meta: meta,
}, nil
},
"ibft status": func() (cli.Command, error) {
"ibft-status": func() (cli.Command, error) {
return &IbftStatus{
Meta: meta,
}, nil
},

// TXPOOL COMMANDS //

"txpool add": func() (cli.Command, error) {
"txpool-add": func() (cli.Command, error) {
return &TxPoolAdd{
Meta: meta,
}, nil
},
"txpool status": func() (cli.Command, error) {
"txpool-status": func() (cli.Command, error) {
return &TxPoolStatus{
Meta: meta,
}, nil
Expand All @@ -124,11 +126,28 @@ func Commands() map[string]cli.CommandFactory {
}
}

// FlagDescriptor contains the description elements for a command flag
type FlagDescriptor struct {
// MetaFlagDescriptor contains the description elements for a command flag. Implements types.FlagDescriptor
type MetaFlagDescriptor struct {
description string // Flag description
arguments []string // Arguments list
argumentsOptional bool // Flag indicating if flag arguments are optional
flagOptional bool
}

func (m MetaFlagDescriptor) GetDescription() string {
return m.description
}

func (m MetaFlagDescriptor) GetArgumentsList() []string {
return m.arguments
}

func (m MetaFlagDescriptor) GetArgumentsOptional() bool {
return m.argumentsOptional
}

func (m MetaFlagDescriptor) GetFlagOptional() bool {
return m.flagOptional
zivkovicmilos marked this conversation as resolved.
Show resolved Hide resolved
}

type HelpGenerator interface {
Expand All @@ -140,82 +159,22 @@ type Meta struct {
UI cli.Ui
addr string

flagMap map[string]FlagDescriptor
flagMap map[string]types.FlagDescriptor
hasGlobalFlags bool
}

// GenerateHelp is a utility function called by every command's Help() method
func (m *Meta) GenerateHelp(synopsys string, usage string) string {
helpOutput := ""

flagCounter := 0
for flagEl, descriptor := range m.flagMap {
helpOutput += m.GenerateFlagDesc(flagEl, descriptor) + "\n"
flagCounter++

if flagCounter < len(m.flagMap) {
helpOutput += "\n"
}
}

if m.hasGlobalFlags {
if strings.Count(usage, "--") > 1 {
usage = fmt.Sprintf("%s\n\t%s", usage, globalFlagsUsage())
} else {
usage = fmt.Sprintf("%s %s", usage, globalFlagsUsage())
}
}

if len(m.flagMap) > 0 {
return fmt.Sprintf("Description:\n\n%s\n\nUsage:\n\n\t%s\n\nFlags:\n\n%s", synopsys, usage, helpOutput)
} else {
return fmt.Sprintf("Description:\n\n%s\n\nUsage:\n\n\t%s\n", synopsys, usage)
}
}

// GenerateFlagDesc generates the flag descriptions in a readable format
func (m *Meta) GenerateFlagDesc(flagEl string, descriptor FlagDescriptor) string {
// Generate the top row (with various flags)
topRow := fmt.Sprintf("--%s", flagEl)

argLength := len(descriptor.arguments)

if argLength > 0 {
topRow += " "
if descriptor.argumentsOptional {
topRow += "["
}

for argIndx, argument := range descriptor.arguments {
topRow += argument

if argIndx < argLength-1 && argLength > 1 {
topRow += " "
}
}

if descriptor.argumentsOptional {
topRow += "]"
}
}

// Generate the bottom description
bottomRow := fmt.Sprintf("\t%s", descriptor.description)

return fmt.Sprintf("%s\n%s", topRow, bottomRow)
}

// DefineFlags sets global flags used by several commands
func (m *Meta) DefineFlags() {
m.hasGlobalFlags = true
m.flagMap = make(map[string]FlagDescriptor)
m.flagMap = make(map[string]types.FlagDescriptor)

m.flagMap["grpc-address"] = FlagDescriptor{
m.flagMap["grpc-address"] = MetaFlagDescriptor{
description: fmt.Sprintf("Address of the gRPC API. Default: %s:%d", "127.0.0.1", minimal.DefaultGRPCPort),
arguments: []string{
"GRPC_ADDRESS",
},
argumentsOptional: false,
flagOptional: true,
}
}

Expand All @@ -237,10 +196,6 @@ func (m *Meta) Conn() (*grpc.ClientConn, error) {
return conn, nil
}

func globalFlagsUsage() string {
return `[--grpc-address GRPC_ADDRESS]`
}

// OUTPUT FORMATTING //

// formatList formats a list, using a specific blank value replacement
Expand Down
41 changes: 25 additions & 16 deletions command/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import (

const (
genesisFileName = "./genesis.json"
defaultChainName = "example"
defaultChainName = "example"
defaultChainID = 100
defaultPremineBalance = "0x3635C9ADC5DEA00000" // 1000 ETH
defaultConsensus = "pow"
defaultConsensus = "pow"
)

// GenesisCommand is the command to show the version of the agent
Expand All @@ -35,76 +35,84 @@ type GenesisCommand struct {
func (c *GenesisCommand) DefineFlags() {
if c.flagMap == nil {
// Flag map not initialized
c.flagMap = make(map[string]FlagDescriptor)
c.flagMap = make(map[string]types.FlagDescriptor)
}

if len(c.flagMap) > 0 {
// No need to redefine the flags again
return
}

c.flagMap["data-dir"] = FlagDescriptor{
c.flagMap["data-dir"] = MetaFlagDescriptor{
description: fmt.Sprintf("Sets the directory for the Polygon SDK data. Default: %s", genesisFileName),
arguments: []string{
"DATA_DIRECTORY",
},
argumentsOptional: false,
flagOptional: true,
}

c.flagMap["name"] = FlagDescriptor{
c.flagMap["name"] = MetaFlagDescriptor{
description: fmt.Sprintf("Sets the name for the chain. Default: %s", defaultChainName),
arguments: []string{
"NAME",
},
argumentsOptional: false,
flagOptional: true,
}

c.flagMap["premine"] = FlagDescriptor{
c.flagMap["premine"] = MetaFlagDescriptor{
description: fmt.Sprintf("Sets the premined accounts and balances. Default premined balance: %s", defaultPremineBalance),
arguments: []string{
"ADDRESS:VALUE",
},
argumentsOptional: false,
flagOptional: true,
}

c.flagMap["chainid"] = FlagDescriptor{
c.flagMap["chainid"] = MetaFlagDescriptor{
description: fmt.Sprintf("Sets the ID of the chain. Default: %d", defaultChainID),
arguments: []string{
"CHAIN_ID",
},
argumentsOptional: false,
flagOptional: true,
}

c.flagMap["consensus"] = FlagDescriptor{
c.flagMap["consensus"] = MetaFlagDescriptor{
description: fmt.Sprintf("Sets consensus protocol. Default: %s", defaultConsensus),
arguments: []string{
"CONSENSUS_PROTOCOL",
},
argumentsOptional: false,
flagOptional: true,
}

c.flagMap["bootnode"] = FlagDescriptor{
c.flagMap["bootnode"] = MetaFlagDescriptor{
description: "Multiaddr URL for p2p discovery bootstrap. This flag can be used multiple times.",
arguments: []string{
"BOOTNODE_URL",
},
argumentsOptional: false,
flagOptional: true,
}

c.flagMap["ibft-validator"] = FlagDescriptor{
c.flagMap["ibft-validator"] = MetaFlagDescriptor{
description: "Sets passed in addresses as IBFT validators. Needs to be present if ibft-validators-prefix-path is omitted",
arguments: []string{
"IBFT_VALIDATOR_LIST",
},
argumentsOptional: false,
flagOptional: true,
}

c.flagMap["ibft-validators-prefix-path"] = FlagDescriptor{
c.flagMap["ibft-validators-prefix-path"] = MetaFlagDescriptor{
description: "Prefix path for validator folder directory. Needs to be present if ibft-validator is omitted",
arguments: []string{
"IBFT_VALIDATORS_PREFIX_PATH",
},
argumentsOptional: false,
flagOptional: true,
}
}

Expand All @@ -113,14 +121,15 @@ func (c *GenesisCommand) GetHelperText() string {
return "Generates the genesis.json file, with passed in parameters"
}

func (c *GenesisCommand) GetBaseCommand() string {
return "genesis"
}

// Help implements the cli.Command interface
func (c *GenesisCommand) Help() string {
c.DefineFlags()
usage := `genesis [--data-dir DATA_DIRECTORY] [--name NAME] [--chainid CHAIN_ID]
[--premine ADDRESS:VALUE] [--bootnode BOOTNODE_URL] [--consensus CONSENSUS_PROTOCOL]
[--ibft-validator IBFT_VALIDATOR_LIST] [--ibft-validators-prefix-path IBFT_VALIDATORS_PREFIX_PATH]`

return c.GenerateHelp(c.Synopsis(), usage)
return types.GenerateHelp(c.Synopsis(), types.GenerateUsage(c.GetBaseCommand(), c.flagMap), c.flagMap)
}

// Synopsis implements the cli.Command interface
Expand All @@ -130,7 +139,7 @@ func (c *GenesisCommand) Synopsis() string {

// Run implements the cli.Command interface
func (c *GenesisCommand) Run(args []string) int {
flags := flag.NewFlagSet("genesis", flag.ContinueOnError)
flags := flag.NewFlagSet(c.GetBaseCommand(), flag.ContinueOnError)
flags.Usage = func() {}

var dataDir string
Expand Down
Loading