feat: add flags for bind address config options

Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com>
This commit is contained in:
Mark Sagi-Kazar 2021-01-14 15:50:19 +01:00
parent cdefd1f788
commit c55d84b5d2
No known key found for this signature in database
GPG Key ID: 34CC109EB5ED1C2A

View File

@ -30,13 +30,20 @@ import (
) )
type serveOptions struct { type serveOptions struct {
// Config file path
config string config string
// Flags
webHTTPAddr string
webHTTPSAddr string
telemetryAddr string
grpcAddr string
} }
func commandServe() *cobra.Command { func commandServe() *cobra.Command {
options := serveOptions{} options := serveOptions{}
return &cobra.Command{ cmd := &cobra.Command{
Use: "serve [flags] [config file]", Use: "serve [flags] [config file]",
Short: "Launch Dex", Short: "Launch Dex",
Example: "dex serve config.yaml", Example: "dex serve config.yaml",
@ -50,6 +57,15 @@ func commandServe() *cobra.Command {
return runServe(options) return runServe(options)
}, },
} }
flags := cmd.Flags()
flags.StringVar(&options.webHTTPAddr, "web-http-addr", "", "Web HTTP address")
flags.StringVar(&options.webHTTPSAddr, "web-https-addr", "", "Web HTTPS address")
flags.StringVar(&options.telemetryAddr, "telemetry-addr", "", "Telemetry address")
flags.StringVar(&options.grpcAddr, "grpc-addr", "", "gRPC API address")
return cmd
} }
func runServe(options serveOptions) error { func runServe(options serveOptions) error {
@ -64,6 +80,8 @@ func runServe(options serveOptions) error {
return fmt.Errorf("error parse config file %s: %v", configFile, err) return fmt.Errorf("error parse config file %s: %v", configFile, err)
} }
applyConfigOverrides(options, &c)
logger, err := newLogger(c.Logger.Level, c.Logger.Format) logger, err := newLogger(c.Logger.Level, c.Logger.Format)
if err != nil { if err != nil {
return fmt.Errorf("invalid config: %v", err) return fmt.Errorf("invalid config: %v", err)
@ -383,3 +401,21 @@ func newLogger(level string, format string) (log.Logger, error) {
Level: logLevel, Level: logLevel,
}, nil }, nil
} }
func applyConfigOverrides(options serveOptions, config *Config) {
if options.webHTTPAddr != "" {
config.Web.HTTP = options.webHTTPAddr
}
if options.webHTTPSAddr != "" {
config.Web.HTTPS = options.webHTTPSAddr
}
if options.telemetryAddr != "" {
config.Telemetry.HTTP = options.telemetryAddr
}
if options.grpcAddr != "" {
config.GRPC.Addr = options.grpcAddr
}
}