github.com/kjdelisle/consul@v1.4.5/command/flags/http.go (about)

     1  package flags
     2  
     3  import (
     4  	"flag"
     5  
     6  	"github.com/hashicorp/consul/api"
     7  )
     8  
     9  type HTTPFlags struct {
    10  	// client api flags
    11  	address       StringValue
    12  	token         StringValue
    13  	caFile        StringValue
    14  	caPath        StringValue
    15  	certFile      StringValue
    16  	keyFile       StringValue
    17  	tlsServerName StringValue
    18  
    19  	// server flags
    20  	datacenter StringValue
    21  	stale      BoolValue
    22  }
    23  
    24  func (f *HTTPFlags) ClientFlags() *flag.FlagSet {
    25  	fs := flag.NewFlagSet("", flag.ContinueOnError)
    26  	fs.Var(&f.address, "http-addr",
    27  		"The `address` and port of the Consul HTTP agent. The value can be an IP "+
    28  			"address or DNS address, but it must also include the port. This can "+
    29  			"also be specified via the CONSUL_HTTP_ADDR environment variable. The "+
    30  			"default value is http://127.0.0.1:8500. The scheme can also be set to "+
    31  			"HTTPS by setting the environment variable CONSUL_HTTP_SSL=true.")
    32  	fs.Var(&f.token, "token",
    33  		"ACL token to use in the request. This can also be specified via the "+
    34  			"CONSUL_HTTP_TOKEN environment variable. If unspecified, the query will "+
    35  			"default to the token of the Consul agent at the HTTP address.")
    36  	fs.Var(&f.caFile, "ca-file",
    37  		"Path to a CA file to use for TLS when communicating with Consul. This "+
    38  			"can also be specified via the CONSUL_CACERT environment variable.")
    39  	fs.Var(&f.caPath, "ca-path",
    40  		"Path to a directory of CA certificates to use for TLS when communicating "+
    41  			"with Consul. This can also be specified via the CONSUL_CAPATH environment variable.")
    42  	fs.Var(&f.certFile, "client-cert",
    43  		"Path to a client cert file to use for TLS when 'verify_incoming' is enabled. This "+
    44  			"can also be specified via the CONSUL_CLIENT_CERT environment variable.")
    45  	fs.Var(&f.keyFile, "client-key",
    46  		"Path to a client key file to use for TLS when 'verify_incoming' is enabled. This "+
    47  			"can also be specified via the CONSUL_CLIENT_KEY environment variable.")
    48  	fs.Var(&f.tlsServerName, "tls-server-name",
    49  		"The server name to use as the SNI host when connecting via TLS. This "+
    50  			"can also be specified via the CONSUL_TLS_SERVER_NAME environment variable.")
    51  
    52  	return fs
    53  }
    54  
    55  func (f *HTTPFlags) ServerFlags() *flag.FlagSet {
    56  	fs := flag.NewFlagSet("", flag.ContinueOnError)
    57  	fs.Var(&f.datacenter, "datacenter",
    58  		"Name of the datacenter to query. If unspecified, this will default to "+
    59  			"the datacenter of the queried agent.")
    60  	fs.Var(&f.stale, "stale",
    61  		"Permit any Consul server (non-leader) to respond to this request. This "+
    62  			"allows for lower latency and higher throughput, but can result in "+
    63  			"stale data. This option has no effect on non-read operations. The "+
    64  			"default value is false.")
    65  	return fs
    66  }
    67  
    68  func (f *HTTPFlags) Addr() string {
    69  	return f.address.String()
    70  }
    71  
    72  func (f *HTTPFlags) Datacenter() string {
    73  	return f.datacenter.String()
    74  }
    75  
    76  func (f *HTTPFlags) Stale() bool {
    77  	if f.stale.v == nil {
    78  		return false
    79  	}
    80  	return *f.stale.v
    81  }
    82  
    83  func (f *HTTPFlags) Token() string {
    84  	return f.token.String()
    85  }
    86  
    87  func (f *HTTPFlags) SetToken(v string) error {
    88  	return f.token.Set(v)
    89  }
    90  
    91  func (f *HTTPFlags) APIClient() (*api.Client, error) {
    92  	c := api.DefaultConfig()
    93  
    94  	f.MergeOntoConfig(c)
    95  
    96  	return api.NewClient(c)
    97  }
    98  
    99  func (f *HTTPFlags) MergeOntoConfig(c *api.Config) {
   100  	f.address.Merge(&c.Address)
   101  	f.token.Merge(&c.Token)
   102  	f.caFile.Merge(&c.TLSConfig.CAFile)
   103  	f.caPath.Merge(&c.TLSConfig.CAPath)
   104  	f.certFile.Merge(&c.TLSConfig.CertFile)
   105  	f.keyFile.Merge(&c.TLSConfig.KeyFile)
   106  	f.tlsServerName.Merge(&c.TLSConfig.Address)
   107  	f.datacenter.Merge(&c.Datacenter)
   108  }