From afb1700d4d147d370b0d45f86f03f5a55fcf6c88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrej=20Buko=C5=A1ek?= Date: Tue, 21 Jan 2025 23:11:10 +0100 Subject: [PATCH] cmd/network: Extract absolute path of local endpoint in add-local --- cmd/network/add.go | 15 ++++ cmd/network/add_local.go | 146 +++++++++++++++++++++++---------------- 2 files changed, 103 insertions(+), 58 deletions(-) diff --git a/cmd/network/add.go b/cmd/network/add.go index 58941ab..b6ba86a 100644 --- a/cmd/network/add.go +++ b/cmd/network/add.go @@ -2,6 +2,8 @@ package network import ( "context" + "os" + "strings" "github.com/spf13/cobra" @@ -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, } diff --git a/cmd/network/add_local.go b/cmd/network/add_local.go index 8f71e55..586c4e8 100644 --- a/cmd/network/add_local.go +++ b/cmd/network/add_local.go @@ -3,6 +3,10 @@ package network import ( "context" "fmt" + "net/url" + "os" + "path/filepath" + "strings" "github.com/spf13/cobra" flag "github.com/spf13/pflag" @@ -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)