service: replace cmd with NewFlagSet, add internal service version metadata interceptors

client: add NewFlagSet, add missing Options interface methods

Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
This commit is contained in:
2022-03-10 12:46:36 +01:00
parent e578d62a29
commit c0e79d8834
10 changed files with 597 additions and 159 deletions

43
client/flags.go Normal file
View File

@ -0,0 +1,43 @@
package client
import (
"fmt"
"strings"
"github.com/caitlinelfring/go-env-default"
"github.com/spf13/pflag"
)
var u = strings.ToUpper
func NewFlagSet() (*pflag.FlagSet, Option) {
const (
addr = "address"
secure = "secure"
// caCert = "ca-cert"
// clientCert = "client-cert"
// clientKey = "client-key"
)
var (
optAddress string
optSecure bool
// optCACert string
// optCert string
// optKey string
)
flags := pflag.NewFlagSet("gRPC", pflag.ContinueOnError)
flags.StringVar(&optAddress, addr, env.GetDefault(u(addr), "0.0.0.0:0"), "Bind address for the server. 127.0.0.1:9090"+flagEnv(addr))
flags.BoolVar(&optSecure, secure, env.GetBoolDefault(u(secure), true), "Generate self signed certificate if none provided"+flagEnv(secure))
// flags.StringVar(&optCACert, caCert, "", "Path to Root CA certificate"+flagEnv(optCACert))
// flags.StringVar(&optCert, clientCert, "", "Path to Server certificate"+flagEnv(clientCert))
// flags.StringVar(&optKey, clientKey, "", "Path to Server key"+flagEnv(clientKey))
return flags, func(o *options) {
o.addr = optAddress
o.secure = optSecure
}
}
func flagEnv(name string) string {
return fmt.Sprintf(" [$%s]", strings.Replace(u(name), "-", "_", -1))
}

View File

@ -12,9 +12,13 @@ import (
type Options interface {
Name() string
Version() string
Address() string
Secure() bool
Registry() registry.Registry
TLSConfig() *tls.Config
DialOptions() []grpc.DialOption
UnaryInterceptors() []grpc.UnaryClientInterceptor
StreamInterceptors() []grpc.StreamClientInterceptor
}
type Option func(*options)
@ -91,7 +95,7 @@ type options struct {
secure bool
dialOptions []grpc.DialOption
unaryInterceptors []grpc.UnaryClientInterceptor
unaryInterceptors []grpc.UnaryClientInterceptor
streamInterceptors []grpc.StreamClientInterceptor
}
@ -103,6 +107,10 @@ func (o *options) Version() string {
return o.version
}
func (o *options) Address() string {
return o.addr
}
func (o *options) Registry() registry.Registry {
return o.registry
}
@ -118,3 +126,11 @@ func (o *options) Secure() bool {
func (o *options) DialOptions() []grpc.DialOption {
return o.dialOptions
}
func (o *options) UnaryInterceptors() []grpc.UnaryClientInterceptor {
return o.unaryInterceptors
}
func (o *options) StreamInterceptors() []grpc.StreamClientInterceptor {
return o.streamInterceptors
}