From 25f0a12e15e608aa2ff11f1dc278247a1b00e6d3 Mon Sep 17 00:00:00 2001 From: Michael Tsitrin <114929630+mtsitrin@users.noreply.github.com> Date: Tue, 12 Sep 2023 17:08:05 +0300 Subject: [PATCH] feat: using flags as defualts in interactive mode (#528) --- cmd/config/init/flags.go | 51 +++++++++++++++++----------- cmd/config/init/init.go | 7 +++- cmd/config/init/interactive.go | 62 ++++++++++++++++++++++++---------- 3 files changed, 82 insertions(+), 38 deletions(-) diff --git a/cmd/config/init/flags.go b/cmd/config/init/flags.go index 74cc3726..eb30696b 100644 --- a/cmd/config/init/flags.go +++ b/cmd/config/init/flags.go @@ -3,6 +3,7 @@ package initconfig import ( "fmt" "math/rand" + "regexp" "strings" "time" @@ -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) } @@ -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 +} diff --git a/cmd/config/init/init.go b/cmd/config/init/init.go index 4c9480e6..4d452e2e 100644 --- a/cmd/config/init/init.go +++ b/cmd/config/init/init.go @@ -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" @@ -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) diff --git a/cmd/config/init/interactive.go b/cmd/config/init/interactive.go index 40a393af..33576296 100644 --- a/cmd/config/init/interactive.go +++ b/cmd/config/init/interactive.go @@ -4,8 +4,8 @@ import ( "fmt" "os" "strings" - "unicode" + "github.com/dymensionxyz/roller/cmd/consts" "github.com/dymensionxyz/roller/config" "github.com/manifoldco/promptui" ) @@ -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, @@ -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 { @@ -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) { @@ -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() @@ -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 -}