@@ -3,6 +3,10 @@ package network
33import (
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,72 +29,98 @@ 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- }
39-
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
58- }
59-
60- // Yep.
61- net .Denomination = defaultNet .Denomination
62- net .ParaTimes = defaultNet .ParaTimes
63- clonedDefault = true
64- break
65- }
66-
67- if symbol != "" {
68- net .Denomination .Symbol = symbol
69- }
70-
71- if numDecimals != 0 {
72- net .Denomination .Decimals = uint8 (numDecimals )
73- }
74-
75- if description != "" {
76- net .Description = description
77- }
78-
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- }
84-
85- err = cfg .Networks .Add (name , & net )
86- cobra .CheckErr (err )
87-
88- err = cfg .Save ()
89- cobra .CheckErr (err )
36+
37+ AddLocalNetwork (name , rpc )
9038 },
9139 }
9240)
9341
42+ func AddLocalNetwork (name string , rpc string ) {
43+ cfg := cliConfig .Global ()
44+
45+ net := config.Network {
46+ RPC : rpc ,
47+ }
48+
49+ if ! cmnGrpc .IsLocalAddress (net .RPC ) {
50+ cobra .CheckErr (fmt .Errorf ("rpc-endpoint '%s' is not local" , net .RPC ))
51+ }
52+
53+ // Extract absolute path for given local endpoint.
54+ parsedRPC , err := url .Parse (net .RPC )
55+ if err != nil {
56+ cobra .CheckErr (fmt .Errorf ("malformed RPC endpoint: %w" , err ))
57+ }
58+ if strings .HasPrefix (parsedRPC .Opaque , "~/" ) {
59+ // Expand to user's home directory.
60+ home , grr := os .UserHomeDir ()
61+ if grr != nil {
62+ cobra .CheckErr (fmt .Errorf ("unable to get user's home directory: %w" , grr ))
63+ }
64+ parsedRPC .Opaque = filepath .Join (home , parsedRPC .Opaque [2 :])
65+ }
66+ parsedRPC .Opaque , err = filepath .Abs (parsedRPC .Opaque )
67+ if err != nil {
68+ cobra .CheckErr (fmt .Errorf ("malformed path in RPC endpoint: %w" , err ))
69+ }
70+ net .RPC = parsedRPC .String ()
71+
72+ // Connect to the network and query the chain context.
73+ ctx := context .Background ()
74+ conn , err := connection .ConnectNoVerify (ctx , & net )
75+ cobra .CheckErr (err )
76+
77+ chainContext , err := conn .Consensus ().GetChainContext (ctx )
78+ cobra .CheckErr (err )
79+ net .ChainContext = chainContext
80+ cobra .CheckErr (net .Validate ())
81+
82+ // With a very high probability, the user is going to be
83+ // adding a local endpoint for an existing network, so try
84+ // to clone config details from any of the hardcoded
85+ // defaults.
86+ var clonedDefault bool
87+ for _ , defaultNet := range config .DefaultNetworks .All {
88+ if defaultNet .ChainContext != chainContext {
89+ continue
90+ }
91+
92+ // Yep.
93+ net .Denomination = defaultNet .Denomination
94+ net .ParaTimes = defaultNet .ParaTimes
95+ clonedDefault = true
96+ break
97+ }
98+
99+ if symbol != "" {
100+ net .Denomination .Symbol = symbol
101+ }
102+
103+ if numDecimals != 0 {
104+ net .Denomination .Decimals = uint8 (numDecimals )
105+ }
106+
107+ if description != "" {
108+ net .Description = description
109+ }
110+
111+ // If we failed to crib details from a hardcoded config,
112+ // and user did not set -y flag ask the user.
113+ if ! clonedDefault && ! common .GetAnswerYes () {
114+ networkDetailsFromSurvey (& net )
115+ }
116+
117+ err = cfg .Networks .Add (name , & net )
118+ cobra .CheckErr (err )
119+
120+ err = cfg .Save ()
121+ cobra .CheckErr (err )
122+ }
123+
94124func init () {
95125 addLocalCmd .Flags ().AddFlagSet (common .AnswerYesFlag )
96126
0 commit comments