Skip to content

Commit

Permalink
feat: using flags as defualts in interactive mode (#528)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtsitrin authored Sep 12, 2023
1 parent 1ace736 commit 25f0a12
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 38 deletions.
51 changes: 31 additions & 20 deletions cmd/config/init/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package initconfig
import (
"fmt"
"math/rand"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -40,33 +41,38 @@ func addFlags(cmd *cobra.Command) error {
}

func GetInitConfig(initCmd *cobra.Command, args []string) (*config.RollappConfig, error) {
cfg := config.RollappConfig{
RollerVersion: version.TrimVersionStr(version.BuildVersion),
}
cfg.Home = initCmd.Flag(utils.FlagNames.Home).Value.String()
cfg.RollappBinary = initCmd.Flag(FlagNames.RollappBinary).Value.String()
// Error is ignored because the flag is validated in the cobra preRun hook
home := initCmd.Flag(utils.FlagNames.Home).Value.String()
interactive, _ := initCmd.Flags().GetBool(FlagNames.Interactive)

//load initial config if exists
var cfg config.RollappConfig
//load from flags
cfg.Home = home
cfg.RollappBinary = initCmd.Flag(FlagNames.RollappBinary).Value.String()
cfg.VMType = config.VMType(initCmd.Flag(FlagNames.VMType).Value.String())
cfg.TokenSupply = initCmd.Flag(FlagNames.TokenSupply).Value.String()
decimals, _ := initCmd.Flags().GetUint(FlagNames.Decimals)
cfg.Decimals = decimals
cfg.DA = config.DAType(strings.ToLower(initCmd.Flag(FlagNames.DAType).Value.String()))
hubID := initCmd.Flag(FlagNames.HubID).Value.String()
if hub, ok := Hubs[hubID]; ok {
cfg.HubData = hub
}
cfg.RollerVersion = version.TrimVersionStr(version.BuildVersion)

if len(args) > 0 {
cfg.RollappID = args[0]
}
if len(args) > 1 {
cfg.Denom = "u" + args[1]
}

if interactive {
if err := RunInteractiveMode(&cfg); err != nil {
return nil, err
}
return formatBaseCfg(cfg, initCmd)
}

rollappId := args[0]
denom := args[1]
if !isLowercaseAlphabetical(rollappId) {
return nil, fmt.Errorf("invalid rollapp id %s. %s", rollappId, validRollappIDMsg)
}
hubID := initCmd.Flag(FlagNames.HubID).Value.String()
tokenSupply := initCmd.Flag(FlagNames.TokenSupply).Value.String()
cfg.RollappID = rollappId
cfg.Denom = "u" + denom
cfg.HubData = Hubs[hubID]
cfg.TokenSupply = tokenSupply
cfg.DA = config.DAType(strings.ToLower(initCmd.Flag(FlagNames.DAType).Value.String()))
cfg.VMType = config.VMType(initCmd.Flag(FlagNames.VMType).Value.String())
return formatBaseCfg(cfg, initCmd)
}

Expand Down Expand Up @@ -114,3 +120,8 @@ func setDecimals(initCmd *cobra.Command, cfg *config.RollappConfig) {
func getAvailableHubsMessage() string {
return fmt.Sprintf("Acceptable values are '%s', '%s' or '%s'", FroopylandHubName, StagingHubName, LocalHubName)
}

func isLowercaseAlphabetical(s string) bool {
match, _ := regexp.MatchString("^[a-z]+$", s)
return match
}
7 changes: 6 additions & 1 deletion cmd/config/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package initconfig

import (
"fmt"
"os"

"github.com/dymensionxyz/roller/relayer"
global_utils "github.com/dymensionxyz/roller/utils"
"os"

"github.com/dymensionxyz/roller/cmd/consts"
"github.com/dymensionxyz/roller/cmd/utils"
Expand All @@ -29,6 +30,10 @@ func InitCmd() *cobra.Command {
return fmt.Errorf("invalid number of arguments. Expected 2, got %d", len(args))
}

if !isLowercaseAlphabetical(args[0]) {
return fmt.Errorf("invalid rollapp id %s. %s", args[0], validRollappIDMsg)
}

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

Expand Down
62 changes: 45 additions & 17 deletions cmd/config/init/interactive.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"
"os"
"strings"
"unicode"

"github.com/dymensionxyz/roller/cmd/consts"
"github.com/dymensionxyz/roller/config"
"github.com/manifoldco/promptui"
)
Expand All @@ -14,26 +14,51 @@ func RunInteractiveMode(cfg *config.RollappConfig) error {
promptNetwork := promptui.Select{
Label: "Select your network",
Items: []string{"froopyland", "devnet", "local"},
CursorPos: func() int {
switch cfg.HubData.ID {
case Hubs[FroopylandHubName].ID:
return 0
case Hubs[StagingHubName].ID:
return 1
case Hubs[LocalHubName].ID:
return 2
default:
return 0
}
}(),
}
_, mode, err := promptNetwork.Run()
if err != nil {
return err
}
cfg.HubData = Hubs[mode]
cfg.VMType = config.EVM_ROLLAPP

promptExecutionEnv := promptui.Select{
Label: "Choose your rollapp execution environment",
Items: []string{"EVM rollapp", "custom EVM rollapp", "custom non-EVM rollapp"},
CursorPos: func() int {
if cfg.RollappBinary == consts.Executables.RollappEVM {
return 0
} else if cfg.VMType == config.EVM_ROLLAPP {
return 1
} else {
return 2
}
}(),
}
_, env, err := promptExecutionEnv.Run()
if err != nil {
return err
}

if env == "custom non-EVM rollapp" {
cfg.VMType = config.SDK_ROLLAPP
} else {
cfg.VMType = config.EVM_ROLLAPP
}

//if custom binary, get the binary path
if env != "EVM rollapp" {
if env == "custom non-EVM rollapp" {
cfg.VMType = config.SDK_ROLLAPP
}
promptBinaryPath := promptui.Prompt{
Label: "Set your runtime binary",
Default: cfg.RollappBinary,
Expand All @@ -51,7 +76,7 @@ func RunInteractiveMode(cfg *config.RollappConfig) error {
}
promptChainID := promptui.Prompt{
Label: "Enter your RollApp ID",
Default: "myrollapp",
Default: strings.Split(cfg.RollappID, "_")[0],
AllowEdit: true,
}
for {
Expand All @@ -69,7 +94,7 @@ func RunInteractiveMode(cfg *config.RollappConfig) error {

promptDenom := promptui.Prompt{
Label: "Specify your RollApp denom",
Default: "RAX",
Default: strings.TrimPrefix(cfg.Denom, "u"),
AllowEdit: true,
Validate: func(s string) error {
if !config.IsValidTokenSymbol(s) {
Expand All @@ -86,7 +111,7 @@ func RunInteractiveMode(cfg *config.RollappConfig) error {

promptTokenSupply := promptui.Prompt{
Label: "How many " + denom + " tokens do you wish to mint for Genesis?",
Default: "1000000000",
Default: cfg.TokenSupply,
Validate: config.VerifyTokenSupply,
}
supply, err := promptTokenSupply.Run()
Expand All @@ -99,18 +124,21 @@ func RunInteractiveMode(cfg *config.RollappConfig) error {
promptDAType := promptui.Select{
Label: "Choose your data layer",
Items: availableDAs,
CursorPos: func() int {
switch cfg.DA {
case config.Celestia:
return 0
case config.Avail:
return 1
case config.Local:
return 2
default:
return 0
}
}(),
}
_, da, _ := promptDAType.Run()
cfg.DA = config.DAType(strings.ToLower(da))

return nil
}

func isLowercaseAlphabetical(s string) bool {
for _, r := range s {
if !unicode.IsLetter(r) || !unicode.IsLower(r) {
return false
}
}
return true
}

0 comments on commit 25f0a12

Please sign in to comment.