Skip to content

Commit

Permalink
Make cli methods consistent, remove init funcs. fix test bleed due to…
Browse files Browse the repository at this point in the history
… HOME env var not being respected in tests. (#132)
  • Loading branch information
agouin committed Apr 7, 2023
1 parent 9d6f95a commit 759b05a
Show file tree
Hide file tree
Showing 11 changed files with 252 additions and 219 deletions.
67 changes: 40 additions & 27 deletions cmd/horcrux/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,18 @@ import (
"gopkg.in/yaml.v2"
)

func init() {
nodesCmd.AddCommand(addNodesCmd())
nodesCmd.AddCommand(removeNodesCmd())
configCmd.AddCommand(nodesCmd)

peersCmd.AddCommand(addPeersCmd())
peersCmd.AddCommand(removePeersCmd())
peersCmd.AddCommand(setSharesCmd())
configCmd.AddCommand(peersCmd)

chainIDCmd.AddCommand(setChainIDCmd())
configCmd.AddCommand(chainIDCmd)
func configCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "config",
Short: "Commands to configure the horcrux signer",
}

configCmd.AddCommand(initCmd())
rootCmd.AddCommand(configCmd)
}
cmd.AddCommand(nodesCmd())
cmd.AddCommand(peersCmd())
cmd.AddCommand(chainIDCmd())
cmd.AddCommand(initCmd())

var configCmd = &cobra.Command{
Use: "config",
Short: "Commands to configure the horcrux signer",
return cmd
}

func initCmd() *cobra.Command {
Expand Down Expand Up @@ -222,9 +214,16 @@ func validateCosignerConfig(cfg DiskConfig) error {
return nil
}

var nodesCmd = &cobra.Command{
Use: "nodes",
Short: "Commands to configure the chain nodes",
func nodesCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "nodes",
Short: "Commands to configure the chain nodes",
}

cmd.AddCommand(addNodesCmd())
cmd.AddCommand(removeNodesCmd())

return cmd
}

func addNodesCmd() *cobra.Command {
Expand Down Expand Up @@ -315,9 +314,17 @@ func diffSetChainNode(setA, setB []ChainNode) (diff []ChainNode) {
return
}

var peersCmd = &cobra.Command{
Use: "peers",
Short: "Commands to configure the peer nodes",
func peersCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "peers",
Short: "Commands to configure the peer nodes",
}

cmd.AddCommand(addPeersCmd())
cmd.AddCommand(removePeersCmd())
cmd.AddCommand(setSharesCmd())

return cmd
}

func addPeersCmd() *cobra.Command {
Expand Down Expand Up @@ -450,9 +457,15 @@ func diffSetCosignerPeer(setA, setB []CosignerPeer) (diff []CosignerPeer) {
return
}

var chainIDCmd = &cobra.Command{
Use: "chain-id",
Short: "Commands to configure the chain ID",
func chainIDCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "chain-id",
Short: "Commands to configure the chain ID",
}

cmd.AddCommand(setChainIDCmd())

return cmd
}

func setChainIDCmd() *cobra.Command {
Expand Down
31 changes: 20 additions & 11 deletions cmd/horcrux/cmd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,14 @@ func TestConfigInitCmd(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
tmpConfig := filepath.Join(tc.home, ".horcrux")

t.Setenv("HOME", tc.home)
err := os.MkdirAll(tc.home, 0777)
require.NoError(t, err)

cmd := initCmd()
cmd := rootCmd()
cmd.SetOutput(io.Discard)
cmd.SetArgs(tc.args)
args := []string{"--home", tmpConfig, "config", "init"}
args = append(args, tc.args...)
cmd.SetArgs(args)
err = cmd.Execute()

if tc.expectErr {
Expand Down Expand Up @@ -108,11 +109,13 @@ func TestConfigInitCmd(t *testing.T) {
}

func TestConfigChainIDSetCmd(t *testing.T) {
t.Setenv("HOME", t.TempDir())
tmpConfig := filepath.Join(t.TempDir(), ".horcrux")

cmd := initCmd()
cmd := rootCmd()
cmd.SetOutput(io.Discard)
cmd.SetArgs([]string{
"--home", tmpConfig,
"config", "init",
chainID,
"tcp://10.168.0.1:1234",
"-c",
Expand Down Expand Up @@ -159,11 +162,13 @@ func TestConfigChainIDSetCmd(t *testing.T) {
}

func TestConfigNodesAddAndRemove(t *testing.T) {
t.Setenv("HOME", t.TempDir())
tmpConfig := filepath.Join(t.TempDir(), ".horcrux")

cmd := initCmd()
cmd := rootCmd()
cmd.SetOutput(io.Discard)
cmd.SetArgs([]string{
"--home", tmpConfig,
"config", "init",
chainID,
"tcp://10.168.0.1:1234",
"-c",
Expand Down Expand Up @@ -296,11 +301,13 @@ func TestConfigNodesAddAndRemove(t *testing.T) {
}

func TestConfigPeersAddAndRemove(t *testing.T) {
t.Setenv("HOME", t.TempDir())
tmpConfig := filepath.Join(t.TempDir(), ".horcrux")

cmd := initCmd()
cmd := rootCmd()
cmd.SetOutput(io.Discard)
cmd.SetArgs([]string{
"--home", tmpConfig,
"config", "init",
chainID,
"tcp://10.168.0.1:1234",
"-c",
Expand Down Expand Up @@ -566,11 +573,13 @@ func TestDiffSetCosignerPeer(t *testing.T) {
}

func TestSetShares(t *testing.T) {
t.Setenv("HOME", t.TempDir())
tmpConfig := filepath.Join(t.TempDir(), ".horcrux")

cmd := initCmd()
cmd := rootCmd()
cmd.SetOutput(io.Discard)
cmd.SetArgs([]string{
"--home", tmpConfig,
"config", "init",
chainID,
"tcp://10.168.0.1:1234",
"-c",
Expand Down
21 changes: 11 additions & 10 deletions cmd/horcrux/cmd/cosigner.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@ import (
"github.com/tendermint/tendermint/types"
)

func init() {
cosignerCmd.AddCommand(StartCosignerCmd())
cosignerCmd.AddCommand(AddressCmd())
rootCmd.AddCommand(cosignerCmd)
}
func cosignerCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "cosigner",
Short: "Threshold mpc signer for TM based nodes",
}

var cosignerCmd = &cobra.Command{
Use: "cosigner",
Short: "Threshold mpc signer for TM based nodes",
cmd.AddCommand(startCosignerCmd())
cmd.AddCommand(addressCmd())

return cmd
}

type AddressCmdOutput struct {
Expand All @@ -36,7 +37,7 @@ type AddressCmdOutput struct {
ValConsPubAddress string
}

func AddressCmd() *cobra.Command {
func addressCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "address [bech32]",
Short: "Get public key hex address and valcons address",
Expand Down Expand Up @@ -98,7 +99,7 @@ func AddressCmd() *cobra.Command {
return cmd
}

func StartCosignerCmd() *cobra.Command {
func startCosignerCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "start",
Short: "Start cosigner process",
Expand Down
6 changes: 1 addition & 5 deletions cmd/horcrux/cmd/key2shares.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,8 @@ import (
"github.com/tendermint/tendermint/libs/os"
)

func init() {
rootCmd.AddCommand(CreateCosignerSharesCmd())
}

// CreateCosignerSharesCmd is a cobra command for creating cosigner shares from a priv validator
func CreateCosignerSharesCmd() *cobra.Command {
func createCosignerSharesCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "create-shares [priv_validator.json] [threshold] [shares]",
Aliases: []string{"shard", "shares"},
Expand Down
2 changes: 1 addition & 1 deletion cmd/horcrux/cmd/key2shares_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestKey2Shares(t *testing.T) {

for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
cmd := CreateCosignerSharesCmd()
cmd := createCosignerSharesCmd()
cmd.SetOutput(io.Discard)
cmd.SetArgs(tc.args)
err := cmd.Execute()
Expand Down
Loading

0 comments on commit 759b05a

Please sign in to comment.