Skip to content

Commit

Permalink
chore: interactive cli improvements suggestions (#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtsitrin authored Jul 4, 2023
1 parent 63d166d commit 64422fa
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 810 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
build/
*.exe
/.idea
/roller
25 changes: 2 additions & 23 deletions cmd/config/init/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package initconfig

import (
"fmt"
"strings"
"unicode"

"github.com/dymensionxyz/roller/cmd/consts"
"github.com/dymensionxyz/roller/cmd/utils"
Expand Down Expand Up @@ -71,25 +69,6 @@ func getAvailableHubsMessage() string {
return fmt.Sprintf("Acceptable values are '%s' or '%s'", StagingHubID, LocalHubID)
}

func verifyDecimals(cmd *cobra.Command) error {
decimals, err := cmd.Flags().GetUint(FlagNames.Decimals)
if err != nil {
return err
}
if decimals > 18 {
return fmt.Errorf("invalid decimals: %d. Must be less than or equal to 18", decimals)
}
return nil
}

func isValidDenom(s string) bool {
if len(s) != 3 {
return false
}
for _, r := range s {
if !unicode.IsLetter(r) || !strings.ContainsRune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", r) {
return false
}
}
return true
func getValidDenomMessage() string {
return "A valid denom should consist of exactly 3 English alphabet letters, for example 'btc', 'eth'"
}
10 changes: 1 addition & 9 deletions cmd/config/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"os"
)

const validDenomMsg = "A valid denom should consist of exactly 3 English alphabet letters, for example 'btc', 'eth'"

func InitCmd() *cobra.Command {
initCmd := &cobra.Command{
Use: "init <rollapp-id> <denom>",
Expand All @@ -32,13 +30,7 @@ func InitCmd() *cobra.Command {
if len(args) < 2 {
return fmt.Errorf("invalid number of arguments. Expected 2, got %d", len(args))
}
denom := args[1]
if !isValidDenom(denom) {
return fmt.Errorf("invalid denom '%s'. %s", denom, validDenomMsg)
}
if err := verifyDecimals(cmd); err != nil {
return err
}

//TODO: parse the config here instead of GetInitConfig in Run command
// cmd.SetContextValue("mydata", data)

Expand Down
66 changes: 49 additions & 17 deletions cmd/config/init/interactive.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package initconfig

import (
"fmt"
"os"

"github.com/dymensionxyz/roller/cmd/utils"
"github.com/manifoldco/promptui"
Expand All @@ -10,9 +11,8 @@ import (
// TODO: return error output
func RunInteractiveMode(config *utils.RollappConfig) {
promptNetwork := promptui.Select{
Label: "Select your network",
Items: []string{"local", "devnet"},
CursorPos: 1,
Label: "Select your network",
Items: []string{"devnet", "local"},
}
_, mode, _ := promptNetwork.Run()
config.HubData = Hubs[mode]
Expand All @@ -21,18 +21,31 @@ func RunInteractiveMode(config *utils.RollappConfig) {
Label: "Enter your RollApp ID",
Default: "myrollapp_1234-1",
AllowEdit: true,
Validate: utils.ValidateRollAppID,
}
chainID, _ := promptChainID.Run()
config.RollappID = chainID
for {
chainID, err := promptChainID.Run()
if err != nil {
break
}
if err := utils.ValidateRollAppID(chainID); err == nil {
config.RollappID = chainID
break
}
fmt.Println("Expected format: name_uniqueID-revision (e.g. myrollapp_1234-1)")
}

promptDenom := promptui.Prompt{
Label: "Specify your RollApp denom",
Default: "RAX",
// TODO: add validation for denomination
Validate: func(s string) error {
if !utils.IsValidTokenSymbol(s) {
return fmt.Errorf("invalid token symbol")
}
return nil
},
}
denom, _ := promptDenom.Run()
config.Denom = denom
config.Denom = "u" + denom

promptTokenSupply := promptui.Prompt{
Label: "How many " + denom + " tokens do you wish to mint for Genesis?",
Expand All @@ -46,15 +59,34 @@ func RunInteractiveMode(config *utils.RollappConfig) {
Label: "Choose your data layer",
Items: []string{"Celestia", "Avail"},
}
_, _, _ = promptDAType.Run()
fmt.Println("Only Celestia supported for now")

promptBinaryPath := promptui.Prompt{
Label: "Set your runtime binary",
Default: config.RollappBinary,
AllowEdit: true,
//TODO: add validate for binary path
//TODO(#76): temporary hack to only support Celestia
for {
_, da, err := promptDAType.Run()
if err != nil || da == "Celestia" {
break
}
if da != "Celestia" {
fmt.Println("Only Celestia supported for now")
}
}

promptExecutionEnv := promptui.Select{
Label: "Choose your rollapp execution environment",
Items: []string{"EVM rollapp", "custom"},
}
_, env, _ := promptExecutionEnv.Run()
if env == "custom" {
promptBinaryPath := promptui.Prompt{
Label: "Set your runtime binary",
Default: config.RollappBinary,
AllowEdit: true,
Validate: func(s string) error {
_, err := os.Stat(s)
return err
},
}
path, _ := promptBinaryPath.Run()
config.RollappBinary = path
}
path, _ := promptBinaryPath.Run()
config.RollappBinary = path
}
3 changes: 2 additions & 1 deletion cmd/config/init/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package initconfig

import (
"fmt"
"strings"

"github.com/dymensionxyz/roller/cmd/consts"
"github.com/dymensionxyz/roller/cmd/utils"
"strings"
)

func printInitOutput(rollappConfig utils.RollappConfig, addresses []utils.AddressData, rollappId string) {
Expand Down
40 changes: 39 additions & 1 deletion cmd/utils/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"math/big"
"path/filepath"
"regexp"
"strings"
"unicode"

"github.com/pelletier/go-toml"
)
Expand Down Expand Up @@ -58,7 +60,14 @@ func (c RollappConfig) Validate() error {
}
err = ValidateRollAppID(c.RollappID)
if err != nil {
return fmt.Errorf("invalid RollApp ID '%s'", c.RollappID)
return err
}
err = IsValidDenom(c.Denom)
if err != nil {
return err
}
if err := ValidateDecimals(c.Decimals); err != nil {
return err
}
return nil
}
Expand Down Expand Up @@ -104,3 +113,32 @@ func VerifyTokenSupply(supply string) error {

return nil
}

func ValidateDecimals(decimals uint) error {
if decimals > 18 {
return fmt.Errorf("invalid decimals: %d. Must be less than or equal to 18", decimals)
}
return nil
}

func IsValidDenom(s string) error {
if !strings.HasPrefix(s, "u") {
return fmt.Errorf("invalid denom '%s'. denom expected to start with 'u'", s)
}
if !IsValidTokenSymbol(s[1:]) {
return fmt.Errorf("invalid token symbol '%s'", s[1:])
}
return nil
}

func IsValidTokenSymbol(s string) bool {
if len(s) != 3 {
return false
}
for _, r := range s {
if !unicode.IsLetter(r) || !strings.ContainsRune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", r) {
return false
}
}
return true
}
74 changes: 6 additions & 68 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,110 +10,48 @@ require (
)

require (
github.com/briandowns/spinner v1.23.0
github.com/cosmos/cosmos-sdk v0.45.10
github.com/fatih/color v1.15.0
github.com/gizak/termui/v3 v3.1.0
github.com/google/go-cmp v0.5.9
github.com/manifoldco/promptui v0.9.0
github.com/olekukonko/tablewriter v0.0.5
github.com/pelletier/go-toml v1.9.5
github.com/stretchr/testify v1.8.1
github.com/tidwall/sjson v1.2.5
gopkg.in/natefinch/lumberjack.v2 v2.2.1
gopkg.in/yaml.v2 v2.4.0
)

require (
filippo.io/edwards25519 v1.0.0-beta.2 // indirect
github.com/99designs/keyring v1.1.6 // indirect
github.com/ChainSafe/go-schnorrkel v0.0.0-20200405005733-88cbf1b4c40d // indirect
github.com/DataDog/zstd v1.4.5 // indirect
github.com/armon/go-metrics v0.4.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bgentry/speakeasy v0.1.0 // indirect
github.com/briandowns/spinner v1.23.0 // indirect
github.com/btcsuite/btcd v0.22.1 // indirect
github.com/cespare/xxhash v1.1.0 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
github.com/confio/ics23/go v0.7.0 // indirect
github.com/cosmos/btcutil v1.0.4 // indirect
github.com/cosmos/go-bip39 v1.0.0 // indirect
github.com/cosmos/gorocksdb v1.2.0 // indirect
github.com/cosmos/ledger-cosmos-go v0.11.1 // indirect
github.com/cosmos/ledger-go v0.9.2 // indirect
github.com/danieljoos/wincred v1.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgraph-io/badger/v2 v2.2007.2 // indirect
github.com/dgraph-io/ristretto v0.0.3 // indirect
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 // indirect
github.com/dustin/go-humanize v1.0.1-0.20200219035652-afde56e7acac // indirect
github.com/dvsekhvalnov/jose2go v0.0.0-20200901110807-248326c1351b // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/gizak/termui/v3 v3.1.0 // indirect
github.com/go-kit/kit v0.12.0 // indirect
github.com/go-kit/log v0.2.1 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.3 // indirect
github.com/google/btree v1.0.0 // indirect
github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c // indirect
github.com/gtank/merlin v0.1.1 // indirect
github.com/gtank/ristretto255 v0.1.2 // indirect
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hdevalence/ed25519consensus v0.0.0-20210204194344-59a8610d2b87 // indirect
github.com/jmhodges/levigo v1.0.0 // indirect
github.com/keybase/go-keychain v0.0.0-20190712205309-48d3d31d256d // indirect
github.com/libp2p/go-buffer-pool v0.1.0 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
github.com/mimoo/StrobeGo v0.0.0-20210601165009-122bf33a46e0 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mtibben/percent v0.2.1 // indirect
github.com/nsf/termbox-go v0.0.0-20190121233118-02980233997d // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/petermattis/goid v0.0.0-20180202154549-b0b1615b78e5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.12.2 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.34.0 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/sasha-s/go-deadlock v0.3.1 // indirect
github.com/spf13/afero v1.8.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/viper v1.13.0 // indirect
github.com/subosito/gotenv v1.4.1 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20200815110645-5c35d600f0ca // indirect
github.com/tendermint/btcd v0.1.1 // indirect
github.com/tendermint/crypto v0.0.0-20191022145703-50d29ede1e15 // indirect
github.com/tendermint/go-amino v0.16.0 // indirect
github.com/tendermint/tendermint v0.34.22 // indirect
github.com/tendermint/tm-db v0.6.7 // indirect
github.com/tidwall/gjson v1.14.2 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/zondax/hid v0.9.0 // indirect
go.etcd.io/bbolt v1.3.6 // indirect
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa // indirect
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e // indirect
golang.org/x/net v0.0.0-20221014081412-f15817d10f9b // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/term v0.1.0 // indirect
golang.org/x/text v0.4.0 // indirect
google.golang.org/genproto v0.0.0-20221207170731-23e4bf6bdc37 // indirect
google.golang.org/grpc v1.51.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

Expand Down
Loading

0 comments on commit 64422fa

Please sign in to comment.