2022-03-10 11:46:36 +00:00
package service
import (
"fmt"
"strings"
2024-10-17 16:09:58 +00:00
"github.com/caitlinelfring/go-env-default"
2022-03-10 11:46:36 +00:00
"github.com/spf13/pflag"
)
const (
serverAddress = "address"
insecure = "insecure"
reflection = "reflection"
caCert = "ca-cert"
serverCert = "server-cert"
serverKey = "server-key"
2024-10-17 16:09:58 +00:00
clientCACert = "client-ca-cert"
clientCert = "client-cert"
clientKey = "client-key"
2022-03-10 11:46:36 +00:00
)
var u = strings . ToUpper
func NewFlagSet ( ) ( * pflag . FlagSet , Option ) {
var (
optAddress string
optInsecure bool
optReflection bool
optCACert string
optCert string
optKey string
)
flags := pflag . NewFlagSet ( "gRPC" , pflag . ContinueOnError )
flags . StringVarP ( & optAddress , serverAddress , "a" , env . GetDefault ( u ( serverAddress ) , "0.0.0.0:0" ) , "Bind address for the server, e.g. 127.0.0.1:9090" + flagEnv ( serverAddress ) )
flags . BoolVar ( & optInsecure , insecure , env . GetBoolDefault ( u ( insecure ) , false ) , "Do not generate self signed certificate if none provided" + flagEnv ( insecure ) )
flags . BoolVar ( & optReflection , reflection , env . GetBoolDefault ( u ( reflection ) , false ) , "Enable gRPC reflection server" + flagEnv ( reflection ) )
flags . StringVar ( & optCACert , caCert , "" , "Path to Root CA certificate" + flagEnv ( caCert ) )
flags . StringVar ( & optCert , serverCert , "" , "Path to Server certificate" + flagEnv ( serverCert ) )
flags . StringVar ( & optKey , serverKey , "" , "Path to Server key" + flagEnv ( serverKey ) )
2024-10-17 16:09:58 +00:00
flags . StringVar ( & optCACert , clientCACert , "" , "Path to Root CA certificate" + flagEnv ( clientCACert ) )
flags . StringVar ( & optCert , clientCert , "" , "Path to Client certificate" + flagEnv ( clientCert ) )
flags . StringVar ( & optKey , clientKey , "" , "Path to Client key" + flagEnv ( clientKey ) )
2022-03-10 11:46:36 +00:00
return flags , func ( o * options ) {
o . address = optAddress
o . secure = ! optInsecure
o . reflection = optReflection
2023-03-11 11:30:32 +00:00
o . caCert = optCACert
o . cert = optCert
o . key = optKey
2024-10-17 16:09:58 +00:00
o . clientCACert = optCACert
o . clientCert = optCert
o . clientKey = optKey
2022-03-10 11:46:36 +00:00
}
}
func flagEnv ( name string ) string {
return fmt . Sprintf ( " [$%s]" , strings . Replace ( u ( name ) , "-" , "_" , - 1 ) )
}