github.com/celestiaorg/celestia-node@v0.15.0-beta.1/nodebuilder/core/flags.go (about)

     1  package core
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/spf13/cobra"
     7  	flag "github.com/spf13/pflag"
     8  )
     9  
    10  var (
    11  	coreFlag     = "core.ip"
    12  	coreRPCFlag  = "core.rpc.port"
    13  	coreGRPCFlag = "core.grpc.port"
    14  )
    15  
    16  // Flags gives a set of hardcoded Core flags.
    17  func Flags() *flag.FlagSet {
    18  	flags := &flag.FlagSet{}
    19  
    20  	flags.String(
    21  		coreFlag,
    22  		"",
    23  		"Indicates node to connect to the given core node. "+
    24  			"Example: <ip>, 127.0.0.1. <dns>, subdomain.domain.tld "+
    25  			"Assumes RPC port 26657 and gRPC port 9090 as default unless otherwise specified.",
    26  	)
    27  	flags.String(
    28  		coreRPCFlag,
    29  		"26657",
    30  		"Set a custom RPC port for the core node connection. The --core.ip flag must also be provided.",
    31  	)
    32  	flags.String(
    33  		coreGRPCFlag,
    34  		"9090",
    35  		"Set a custom gRPC port for the core node connection. The --core.ip flag must also be provided.",
    36  	)
    37  	return flags
    38  }
    39  
    40  // ParseFlags parses Core flags from the given cmd and saves them to the passed config.
    41  func ParseFlags(
    42  	cmd *cobra.Command,
    43  	cfg *Config,
    44  ) error {
    45  	coreIP := cmd.Flag(coreFlag).Value.String()
    46  	if coreIP == "" {
    47  		if cmd.Flag(coreGRPCFlag).Changed || cmd.Flag(coreRPCFlag).Changed {
    48  			return fmt.Errorf("cannot specify RPC/gRPC ports without specifying an IP address for --core.ip")
    49  		}
    50  		return nil
    51  	}
    52  
    53  	rpc := cmd.Flag(coreRPCFlag).Value.String()
    54  	grpc := cmd.Flag(coreGRPCFlag).Value.String()
    55  
    56  	cfg.IP = coreIP
    57  	cfg.RPCPort = rpc
    58  	cfg.GRPCPort = grpc
    59  	return cfg.Validate()
    60  }