Skip to content

Commit

Permalink
cmd/network: Extract absolute path of local endpoint in add-local
Browse files Browse the repository at this point in the history
  • Loading branch information
abukosek committed Jan 22, 2025
1 parent 38a81e8 commit afb1700
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 58 deletions.
15 changes: 15 additions & 0 deletions cmd/network/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package network

import (
"context"
"os"
"strings"

"github.com/spf13/cobra"

Expand All @@ -22,6 +24,19 @@ var addCmd = &cobra.Command{
// Validate initial network configuration early.
cobra.CheckErr(config.ValidateIdentifier(name))

isLocal := strings.HasPrefix(rpc, "unix:")
if !isLocal {
// Check if a local file name was given without protocol.
if info, err := os.Stat(rpc); err == nil {
isLocal = !info.IsDir()
rpc = "unix:" + rpc
}
}
if isLocal {
AddLocalNetwork(name, rpc)
return
}

net := config.Network{
RPC: rpc,
}
Expand Down
146 changes: 88 additions & 58 deletions cmd/network/add_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ package network
import (
"context"
"fmt"
"net/url"
"os"
"path/filepath"
"strings"

"github.com/spf13/cobra"
flag "github.com/spf13/pflag"
Expand All @@ -25,72 +29,98 @@ var (
Short: "Add a new local network",
Args: cobra.ExactArgs(2),
Run: func(_ *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 !cmnGrpc.IsLocalAddress(net.RPC) {
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
}

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

if symbol != "" {
net.Denomination.Symbol = symbol
}

if numDecimals != 0 {
net.Denomination.Decimals = uint8(numDecimals)
}

if description != "" {
net.Description = description
}

// If we failed to crib details from a hardcoded config,
// and user did not set -y flag ask the user.
if !clonedDefault && !common.GetAnswerYes() {
networkDetailsFromSurvey(&net)
}

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

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

AddLocalNetwork(name, rpc)
},
}
)

func AddLocalNetwork(name string, rpc string) {
cfg := cliConfig.Global()

net := config.Network{
RPC: rpc,
}

if !cmnGrpc.IsLocalAddress(net.RPC) {
cobra.CheckErr(fmt.Errorf("rpc-endpoint '%s' is not local", net.RPC))
}

// Extract absolute path for given local endpoint.
parsedRPC, err := url.Parse(net.RPC)
if err != nil {
cobra.CheckErr(fmt.Errorf("malformed RPC endpoint: %w", err))
}
if strings.HasPrefix(parsedRPC.Opaque, "~/") {
// Expand to user's home directory.
home, grr := os.UserHomeDir()
if grr != nil {
cobra.CheckErr(fmt.Errorf("unable to get user's home directory: %w", grr))
}
parsedRPC.Opaque = filepath.Join(home, parsedRPC.Opaque[2:])
}
parsedRPC.Opaque, err = filepath.Abs(parsedRPC.Opaque)
if err != nil {
cobra.CheckErr(fmt.Errorf("malformed path in RPC endpoint: %w", err))
}
net.RPC = parsedRPC.String()

// 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 symbol != "" {
net.Denomination.Symbol = symbol
}

if numDecimals != 0 {
net.Denomination.Decimals = uint8(numDecimals)
}

if description != "" {
net.Description = description
}

// If we failed to crib details from a hardcoded config,
// and user did not set -y flag ask the user.
if !clonedDefault && !common.GetAnswerYes() {
networkDetailsFromSurvey(&net)
}

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

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

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

Expand Down

0 comments on commit afb1700

Please sign in to comment.