Skip to content

Commit

Permalink
refactor: CLI flags
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanchriswhite committed Jun 6, 2023
1 parent 81e15f6 commit a18c047
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 72 deletions.
5 changes: 3 additions & 2 deletions app/client/cli/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package cli

import (
"fmt"
"github.com/spf13/cobra"

"github.com/pokt-network/pocket/app/client/cli/flags"
"github.com/pokt-network/pocket/shared/crypto"
"github.com/pokt-network/pocket/utility/types"
"github.com/spf13/cobra"
)

func init() {
Expand Down Expand Up @@ -48,7 +49,7 @@ func accountCommands() []*cobra.Command {
return err
}

if !nonInteractive {
if !flags.NonInteractive {
pwd = readPassphrase(pwd)
}

Expand Down
12 changes: 7 additions & 5 deletions app/client/cli/actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import (
"regexp"
"strings"

"github.com/spf13/cobra"

"github.com/pokt-network/pocket/app/client/cli/flags"
coreTypes "github.com/pokt-network/pocket/shared/core/types"
"github.com/pokt-network/pocket/shared/crypto"
typesUtil "github.com/pokt-network/pocket/utility/types"
"github.com/spf13/cobra"
)

func init() {
Expand Down Expand Up @@ -100,7 +102,7 @@ If no changes are desired for the parameter, just enter the current param value
return err
}

if !nonInteractive {
if !flags.NonInteractive {
pwd = readPassphrase(pwd)
}

Expand Down Expand Up @@ -169,7 +171,7 @@ func newEditStakeCmd(cmdDef actorCmdDef) *cobra.Command {
return err
}

if !nonInteractive {
if !flags.NonInteractive {
pwd = readPassphrase(pwd)
}

Expand Down Expand Up @@ -233,7 +235,7 @@ func newUnstakeCmd(cmdDef actorCmdDef) *cobra.Command {
return err
}

if !nonInteractive {
if !flags.NonInteractive {
pwd = readPassphrase(pwd)
}
pk, err := kb.GetPrivKey(fromAddrHex, pwd)
Expand Down Expand Up @@ -284,7 +286,7 @@ func newUnpauseCmd(cmdDef actorCmdDef) *cobra.Command {
return err
}

if !nonInteractive {
if !flags.NonInteractive {
pwd = readPassphrase(pwd)
}

Expand Down
27 changes: 11 additions & 16 deletions app/client/cli/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,37 @@ package cli
import (
"context"

"github.com/pokt-network/pocket/runtime/configs"
"github.com/pokt-network/pocket/runtime/defaults"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/pokt-network/pocket/app/client/cli/flags"
"github.com/pokt-network/pocket/runtime/configs"
"github.com/pokt-network/pocket/runtime/defaults"
)

const (
cliExecutableName = "p1"
)

var (
remoteCLIURL string
dataDir string
configPath string
nonInteractive bool
verbose bool
cfg *configs.Config
cfg *configs.Config
)

func init() {
rootCmd.PersistentFlags().StringVar(&remoteCLIURL, "remote_cli_url", defaults.DefaultRemoteCLIURL, "takes a remote endpoint in the form of <protocol>://<host> (uses RPC Port)")
rootCmd.PersistentFlags().BoolVar(&nonInteractive, "non_interactive", false, "if true skips the interactive prompts wherever possible (useful for scripting & automation)")
rootCmd.PersistentFlags().StringVar(&flags.RemoteCLIURL, "remote_cli_url", defaults.DefaultRemoteCLIURL, "takes a remote endpoint in the form of <protocol>://<host> (uses RPC Port)")
rootCmd.PersistentFlags().BoolVar(&flags.NonInteractive, "non_interactive", false, "if true skips the interactive prompts wherever possible (useful for scripting & automation)")

// TECHDEBT: Why do we have a data dir when we have a config path if the data dir is only storing keys?
rootCmd.PersistentFlags().StringVar(&dataDir, "data_dir", defaults.DefaultRootDirectory, "Path to store pocket related data (keybase etc.)")
rootCmd.PersistentFlags().StringVar(&configPath, "config", "", "Path to config")
rootCmd.PersistentFlags().StringVar(&flags.DataDir, "data_dir", defaults.DefaultRootDirectory, "Path to store pocket related data (keybase etc.)")
rootCmd.PersistentFlags().StringVar(&flags.ConfigPath, "config", "", "Path to config")
if err := viper.BindPFlag("root_directory", rootCmd.PersistentFlags().Lookup("data_dir")); err != nil {
panic(err)
}

rootCmd.PersistentFlags().BoolVar(&verbose, "verbose", false, "Show verbose output")
rootCmd.PersistentFlags().BoolVar(&flags.Verbose, "verbose", false, "Show verbose output")
if err := viper.BindPFlag("verbose", rootCmd.PersistentFlags().Lookup("verbose")); err != nil {
panic(err)
}

rootCmd.AddCommand(PeerCmd)
}

var rootCmd = &cobra.Command{
Expand All @@ -47,7 +42,7 @@ var rootCmd = &cobra.Command{
Long: "The CLI is meant to be an user but also a machine friendly way for interacting with Pocket Network.",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
// by this time, the config path should be set
cfg = configs.ParseConfig(configPath)
cfg = configs.ParseConfig(flags.ConfigPath)
return nil
},
}
Expand Down
5 changes: 3 additions & 2 deletions app/client/cli/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package cli

import (
"fmt"
"github.com/spf13/cobra"

"github.com/pokt-network/pocket/app/client/cli/flags"
"github.com/pokt-network/pocket/rpc"
"github.com/spf13/cobra"
)

func init() {
Expand Down Expand Up @@ -96,7 +97,7 @@ func consensusCommands() []*cobra.Command {
}

func getConsensusState(cmd *cobra.Command) (*rpc.GetV1ConsensusStateResponse, error) {
client, err := rpc.NewClientWithResponses(remoteCLIURL)
client, err := rpc.NewClientWithResponses(flags.RemoteCLIURL)
if err != nil {
return nil, nil
}
Expand Down
13 changes: 13 additions & 0 deletions app/client/cli/flags/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package flags

var (
RemoteCLIURL string

DataDir string

ConfigPath string

NonInteractive bool

Verbose bool
)
6 changes: 4 additions & 2 deletions app/client/cli/gov.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package cli
import (
"fmt"

"github.com/pokt-network/pocket/utility/types"
"github.com/spf13/cobra"
"google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/wrapperspb"

"github.com/pokt-network/pocket/app/client/cli/flags"
"github.com/pokt-network/pocket/utility/types"
)

func init() {
Expand Down Expand Up @@ -52,7 +54,7 @@ func govCommands() []*cobra.Command {
return err
}

if !nonInteractive {
if !flags.NonInteractive {
pwd = readPassphrase(pwd)
}

Expand Down
22 changes: 12 additions & 10 deletions app/client/cli/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import (
"strconv"
"strings"

"github.com/spf13/cobra"

"github.com/pokt-network/pocket/app/client/cli/flags"
"github.com/pokt-network/pocket/shared/codec"
coreTypes "github.com/pokt-network/pocket/shared/core/types"
"github.com/pokt-network/pocket/shared/crypto"
"github.com/pokt-network/pocket/shared/utils"
"github.com/spf13/cobra"
)

var (
Expand Down Expand Up @@ -65,7 +67,7 @@ func keysCreateCommands() []*cobra.Command {
return err
}

if !nonInteractive {
if !flags.NonInteractive {
pwd = readPassphrase(pwd)
confirmPassphrase(pwd)
}
Expand Down Expand Up @@ -112,7 +114,7 @@ func keysUpdateCommands() []*cobra.Command {
return err
}

if !nonInteractive {
if !flags.NonInteractive {
pwd = readPassphrase(pwd)
newPwd = readPassphraseMessage(newPwd, "New passphrase: ")
confirmPassphrase(newPwd)
Expand Down Expand Up @@ -161,7 +163,7 @@ func keysDeleteCommands() []*cobra.Command {
return err
}

if !nonInteractive {
if !flags.NonInteractive {
pwd = readPassphrase(pwd)
}

Expand Down Expand Up @@ -276,7 +278,7 @@ func keysExportCommands() []*cobra.Command {
return err
}

if !nonInteractive {
if !flags.NonInteractive {
pwd = readPassphrase(pwd)
}

Expand Down Expand Up @@ -353,7 +355,7 @@ func keysImportCommands() []*cobra.Command {
return err
}

if !nonInteractive {
if !flags.NonInteractive {
pwd = readPassphrase(pwd)
}

Expand All @@ -367,7 +369,7 @@ func keysImportCommands() []*cobra.Command {
}
case "raw":
// it is unarmoured so we need to confirm the passphrase
if !nonInteractive {
if !flags.NonInteractive {
confirmPassphrase(pwd)
}
kp, err = kb.ImportFromString(privateKeyString, pwd, hint)
Expand Down Expand Up @@ -423,7 +425,7 @@ func keysSignMsgCommands() []*cobra.Command {
return err
}

if !nonInteractive {
if !flags.NonInteractive {
pwd = readPassphrase(pwd)
}

Expand Down Expand Up @@ -520,7 +522,7 @@ func keysSignTxCommands() []*cobra.Command {
return err
}

if !nonInteractive {
if !flags.NonInteractive {
pwd = readPassphrase(pwd)
}

Expand Down Expand Up @@ -678,7 +680,7 @@ func keysSlipCommands() []*cobra.Command {
return err
}

if !nonInteractive {
if !flags.NonInteractive {
pwd = readPassphrase(pwd)
}

Expand Down
Loading

0 comments on commit a18c047

Please sign in to comment.