Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/network: Extract absolute path of local endpoint in add-local #350

Merged
merged 1 commit into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 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,21 @@ 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()
if isLocal {
rpc = "unix:" + rpc
}
}
}
if isLocal {
AddLocalNetwork(name, rpc)
return
}

net := config.Network{
RPC: rpc,
}
Expand Down
141 changes: 90 additions & 51 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,71 +29,106 @@ 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
// Check if a local file name was given without protocol.
if !strings.HasPrefix(rpc, "unix:") {
if info, err := os.Stat(rpc); err == nil {
if !info.IsDir() {
rpc = "unix:" + rpc
}
}

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

if symbol != "" {
net.Denomination.Symbol = symbol
}
AddLocalNetwork(name, rpc)
},
}
)

if numDecimals != 0 {
net.Denomination.Decimals = uint8(numDecimals)
}
func AddLocalNetwork(name string, rpc string) {
cfg := cliConfig.Global()

if description != "" {
net.Description = description
}
net := config.Network{
RPC: rpc,
}

// 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)
}
if !cmnGrpc.IsLocalAddress(net.RPC) {
cobra.CheckErr(fmt.Errorf("rpc-endpoint '%s' is not local", net.RPC))
}

err = cfg.Networks.Add(name, &net)
cobra.CheckErr(err)
// 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
}

err = cfg.Save()
cobra.CheckErr(err)
},
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
Loading