Skip to content

Commit

Permalink
feat(nodebuilder): add ip resolving capabilities to core.ip (#2277)
Browse files Browse the repository at this point in the history
Co-authored-by: Hlib Kanunnikov <hlibwondertan@gmail.com>
  • Loading branch information
sysrex and Wondertan committed May 31, 2023
1 parent b62452a commit 011765a
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions nodebuilder/core/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package core

import (
"fmt"
"net"

"github.com/spf13/cobra"
flag "github.com/spf13/pflag"
Expand All @@ -21,7 +22,8 @@ func Flags() *flag.FlagSet {
coreFlag,
"",
"Indicates node to connect to the given core node. "+
"Example: <ip>, 127.0.0.1. Assumes RPC port 26657 and gRPC port 9090 as default unless otherwise specified.",
"Example: <ip>, 127.0.0.1. <dns>, subdomain.domain.tld "+
"Assumes RPC port 26657 and gRPC port 9090 as default unless otherwise specified.",
)
flags.String(
coreRPCFlag,
Expand All @@ -37,10 +39,7 @@ func Flags() *flag.FlagSet {
}

// ParseFlags parses Core flags from the given cmd and saves them to the passed config.
func ParseFlags(
cmd *cobra.Command,
cfg *Config,
) error {
func ParseFlags(cmd *cobra.Command, cfg *Config) error {
coreIP := cmd.Flag(coreFlag).Value.String()
if coreIP == "" {
if cmd.Flag(coreGRPCFlag).Changed || cmd.Flag(coreRPCFlag).Changed {
Expand All @@ -49,10 +48,22 @@ func ParseFlags(
return nil
}

ip := net.ParseIP(coreIP)
if ip == nil {
ips, err := net.LookupIP(coreIP)
if err != nil {
return fmt.Errorf("failed to resolve DNS record: %v", err)
}
if len(ips) == 0 {
return fmt.Errorf("no IP addresses found for DNS record")
}
ip = ips[0]
}

rpc := cmd.Flag(coreRPCFlag).Value.String()
grpc := cmd.Flag(coreGRPCFlag).Value.String()

cfg.IP = coreIP
cfg.IP = ip.String()
cfg.RPCPort = rpc
cfg.GRPCPort = grpc
return nil
Expand Down

0 comments on commit 011765a

Please sign in to comment.