Skip to content

Commit

Permalink
feat(cmd/netwrok/add-local): Support non-interactive mode
Browse files Browse the repository at this point in the history
  • Loading branch information
amela committed Sep 5, 2024
1 parent 5ee5605 commit 28b9230
Showing 1 changed file with 87 additions and 54 deletions.
141 changes: 87 additions & 54 deletions cmd/network/add_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,67 +5,100 @@ import (
"fmt"

"github.com/spf13/cobra"
flag "github.com/spf13/pflag"

"github.com/oasisprotocol/oasis-sdk/client-sdk/go/config"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/connection"

"github.com/oasisprotocol/cli/cmd/common"
cliConfig "github.com/oasisprotocol/cli/config"
)

var addLocalCmd = &cobra.Command{
Use: "add-local <name> <rpc-endpoint>",
Short: "Add a new local network",
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
cfg := cliConfig.Global()
name, rpc := args[0], args[1]

net := config.Network{
RPC: rpc,
}
// Validate initial network configuration early.
cobra.CheckErr(config.ValidateIdentifier(name))
if !net.IsLocalRPC() {
cobra.CheckErr(fmt.Errorf("rpc-endpoint '%s' is not local", rpc))
}

// Connect to the network and query the chain context.
ctx := context.Background()
conn, err := connection.ConnectNoVerify(ctx, &net)
cobra.CheckErr(err)

chainContext, err := conn.Consensus().GetChainContext(ctx)
cobra.CheckErr(err)
net.ChainContext = chainContext
cobra.CheckErr(net.Validate())

// With a very high probability, the user is going to be
// adding a local endpoint for an existing network, so try
// to clone config details from any of the hardcoded
// defaults.
var clonedDefault bool
for _, defaultNet := range config.DefaultNetworks.All {
if defaultNet.ChainContext != chainContext {
continue
var (
symbol string
numDecimals uint
description string

addLocalCmd = &cobra.Command{
Use: "add-local <name> <rpc-endpoint>",
Short: "Add a new local network",
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
cfg := cliConfig.Global()
name, rpc := args[0], args[1]

net := config.Network{
RPC: rpc,
}
// Validate initial network configuration early.
cobra.CheckErr(config.ValidateIdentifier(name))
if !net.IsLocalRPC() {
cobra.CheckErr(fmt.Errorf("rpc-endpoint '%s' is not local", rpc))
}

// Yep.
net.Denomination = defaultNet.Denomination
net.ParaTimes = defaultNet.ParaTimes
clonedDefault = true
break
}

// If we failed to crib details from a hardcoded config,
// ask the user.
if !clonedDefault {
networkDetailsFromSurvey(&net)
}

err = cfg.Networks.Add(name, &net)
cobra.CheckErr(err)

err = cfg.Save()
cobra.CheckErr(err)
},
// Connect to the network and query the chain context.
ctx := context.Background()
conn, err := connection.ConnectNoVerify(ctx, &net)
cobra.CheckErr(err)

chainContext, err := conn.Consensus().GetChainContext(ctx)
cobra.CheckErr(err)
net.ChainContext = chainContext
cobra.CheckErr(net.Validate())

// With a very high probability, the user is going to be
// adding a local endpoint for an existing network, so try
// to clone config details from any of the hardcoded
// defaults.
var clonedDefault bool
for _, defaultNet := range config.DefaultNetworks.All {
if defaultNet.ChainContext != chainContext {
continue
}

// Yep.
net.Denomination = defaultNet.Denomination
net.ParaTimes = defaultNet.ParaTimes
clonedDefault = true
break
}

// If we failed to crib details from a hardcoded config,
// ask the user.
if !clonedDefault {
if !common.GetAnswerYes() {
networkDetailsFromSurvey(&net)
} else {
var denomInfo config.DenominationInfo
denomInfo.Symbol = symbol
denomInfo.Decimals = uint8(numDecimals)
net.Denomination = denomInfo

net.Description = description
}
}

err = cfg.Networks.Add(name, &net)
cobra.CheckErr(err)

err = cfg.Save()
cobra.CheckErr(err)
},
}
)

func init() {
addLocalCmd.Flags().AddFlagSet(common.AnswerYesFlag)

symbolFlag := flag.NewFlagSet("", flag.ContinueOnError)
symbolFlag.StringVar(&symbol, "symbol", "", "network's symbol")
addLocalCmd.Flags().AddFlagSet(symbolFlag)

numDecimalsFlag := flag.NewFlagSet("", flag.ContinueOnError)
numDecimalsFlag.UintVar(&numDecimals, "num-decimals", 9, "network's number of decimals")
addLocalCmd.Flags().AddFlagSet(numDecimalsFlag)

descriptionFlag := flag.NewFlagSet("", flag.ContinueOnError)
descriptionFlag.StringVar(&description, "description", "", "network's description")
addLocalCmd.Flags().AddFlagSet(descriptionFlag)
}

0 comments on commit 28b9230

Please sign in to comment.