Skip to content

Commit 9d951d5

Browse files
committed
cmd/network: Extract absolute path of local endpoint in add-local
1 parent 38a81e8 commit 9d951d5

File tree

2 files changed

+107
-51
lines changed

2 files changed

+107
-51
lines changed

cmd/network/add.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package network
22

33
import (
44
"context"
5+
"os"
6+
"strings"
57

68
"github.com/spf13/cobra"
79

@@ -22,6 +24,21 @@ var addCmd = &cobra.Command{
2224
// Validate initial network configuration early.
2325
cobra.CheckErr(config.ValidateIdentifier(name))
2426

27+
isLocal := strings.HasPrefix(rpc, "unix:")
28+
if !isLocal {
29+
// Check if a local file name was given without protocol.
30+
if info, err := os.Stat(rpc); err == nil {
31+
isLocal = !info.IsDir()
32+
if isLocal {
33+
rpc = "unix:" + rpc
34+
}
35+
}
36+
}
37+
if isLocal {
38+
AddLocalNetwork(name, rpc)
39+
return
40+
}
41+
2542
net := config.Network{
2643
RPC: rpc,
2744
}

cmd/network/add_local.go

Lines changed: 90 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ package network
33
import (
44
"context"
55
"fmt"
6+
"net/url"
7+
"os"
8+
"path/filepath"
9+
"strings"
610

711
"github.com/spf13/cobra"
812
flag "github.com/spf13/pflag"
@@ -25,71 +29,106 @@ var (
2529
Short: "Add a new local network",
2630
Args: cobra.ExactArgs(2),
2731
Run: func(_ *cobra.Command, args []string) {
28-
cfg := cliConfig.Global()
2932
name, rpc := args[0], args[1]
3033

31-
net := config.Network{
32-
RPC: rpc,
33-
}
3434
// Validate initial network configuration early.
3535
cobra.CheckErr(config.ValidateIdentifier(name))
36-
if !cmnGrpc.IsLocalAddress(net.RPC) {
37-
cobra.CheckErr(fmt.Errorf("rpc-endpoint '%s' is not local", rpc))
38-
}
3936

40-
// Connect to the network and query the chain context.
41-
ctx := context.Background()
42-
conn, err := connection.ConnectNoVerify(ctx, &net)
43-
cobra.CheckErr(err)
44-
45-
chainContext, err := conn.Consensus().GetChainContext(ctx)
46-
cobra.CheckErr(err)
47-
net.ChainContext = chainContext
48-
cobra.CheckErr(net.Validate())
49-
50-
// With a very high probability, the user is going to be
51-
// adding a local endpoint for an existing network, so try
52-
// to clone config details from any of the hardcoded
53-
// defaults.
54-
var clonedDefault bool
55-
for _, defaultNet := range config.DefaultNetworks.All {
56-
if defaultNet.ChainContext != chainContext {
57-
continue
37+
// Check if a local file name was given without protocol.
38+
if !strings.HasPrefix(rpc, "unix:") {
39+
if info, err := os.Stat(rpc); err == nil {
40+
if !info.IsDir() {
41+
rpc = "unix:" + rpc
42+
}
5843
}
59-
60-
// Yep.
61-
net.Denomination = defaultNet.Denomination
62-
net.ParaTimes = defaultNet.ParaTimes
63-
clonedDefault = true
64-
break
6544
}
6645

67-
if symbol != "" {
68-
net.Denomination.Symbol = symbol
69-
}
46+
AddLocalNetwork(name, rpc)
47+
},
48+
}
49+
)
7050

71-
if numDecimals != 0 {
72-
net.Denomination.Decimals = uint8(numDecimals)
73-
}
51+
func AddLocalNetwork(name string, rpc string) {
52+
cfg := cliConfig.Global()
7453

75-
if description != "" {
76-
net.Description = description
77-
}
54+
net := config.Network{
55+
RPC: rpc,
56+
}
7857

79-
// If we failed to crib details from a hardcoded config,
80-
// and user did not set -y flag ask the user.
81-
if !clonedDefault && !common.GetAnswerYes() {
82-
networkDetailsFromSurvey(&net)
83-
}
58+
if !cmnGrpc.IsLocalAddress(net.RPC) {
59+
cobra.CheckErr(fmt.Errorf("rpc-endpoint '%s' is not local", net.RPC))
60+
}
8461

85-
err = cfg.Networks.Add(name, &net)
86-
cobra.CheckErr(err)
62+
// Extract absolute path for given local endpoint.
63+
parsedRPC, err := url.Parse(net.RPC)
64+
if err != nil {
65+
cobra.CheckErr(fmt.Errorf("malformed RPC endpoint: %w", err))
66+
}
67+
if strings.HasPrefix(parsedRPC.Opaque, "~/") {
68+
// Expand to user's home directory.
69+
home, grr := os.UserHomeDir()
70+
if grr != nil {
71+
cobra.CheckErr(fmt.Errorf("unable to get user's home directory: %w", grr))
72+
}
73+
parsedRPC.Opaque = filepath.Join(home, parsedRPC.Opaque[2:])
74+
}
75+
parsedRPC.Opaque, err = filepath.Abs(parsedRPC.Opaque)
76+
if err != nil {
77+
cobra.CheckErr(fmt.Errorf("malformed path in RPC endpoint: %w", err))
78+
}
79+
net.RPC = parsedRPC.String()
80+
81+
// Connect to the network and query the chain context.
82+
ctx := context.Background()
83+
conn, err := connection.ConnectNoVerify(ctx, &net)
84+
cobra.CheckErr(err)
85+
86+
chainContext, err := conn.Consensus().GetChainContext(ctx)
87+
cobra.CheckErr(err)
88+
net.ChainContext = chainContext
89+
cobra.CheckErr(net.Validate())
90+
91+
// With a very high probability, the user is going to be
92+
// adding a local endpoint for an existing network, so try
93+
// to clone config details from any of the hardcoded
94+
// defaults.
95+
var clonedDefault bool
96+
for _, defaultNet := range config.DefaultNetworks.All {
97+
if defaultNet.ChainContext != chainContext {
98+
continue
99+
}
100+
101+
// Yep.
102+
net.Denomination = defaultNet.Denomination
103+
net.ParaTimes = defaultNet.ParaTimes
104+
clonedDefault = true
105+
break
106+
}
87107

88-
err = cfg.Save()
89-
cobra.CheckErr(err)
90-
},
108+
if symbol != "" {
109+
net.Denomination.Symbol = symbol
91110
}
92-
)
111+
112+
if numDecimals != 0 {
113+
net.Denomination.Decimals = uint8(numDecimals)
114+
}
115+
116+
if description != "" {
117+
net.Description = description
118+
}
119+
120+
// If we failed to crib details from a hardcoded config,
121+
// and user did not set -y flag ask the user.
122+
if !clonedDefault && !common.GetAnswerYes() {
123+
networkDetailsFromSurvey(&net)
124+
}
125+
126+
err = cfg.Networks.Add(name, &net)
127+
cobra.CheckErr(err)
128+
129+
err = cfg.Save()
130+
cobra.CheckErr(err)
131+
}
93132

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

0 commit comments

Comments
 (0)