Merge pull request #1921 from dexidp/feat-add-flags

Add flags for bind address config options
This commit is contained in:
Márk Sági-Kazár 2021-01-14 17:54:38 +01:00 committed by GitHub
commit ccbf6c6e0f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 65 additions and 27 deletions

View file

@ -29,32 +29,47 @@ import (
"github.com/dexidp/dex/storage" "github.com/dexidp/dex/storage"
) )
func commandServe() *cobra.Command { type serveOptions struct {
return &cobra.Command{ // Config file path
Use: "serve [ config file ]", config string
Short: "Connect to the storage and begin serving requests.",
Long: ``, // Flags
Example: "dex serve config.yaml", webHTTPAddr string
Run: func(cmd *cobra.Command, args []string) { webHTTPSAddr string
if err := serve(cmd, args); err != nil { telemetryAddr string
fmt.Fprintln(os.Stderr, err) grpcAddr string
os.Exit(2)
} }
func commandServe() *cobra.Command {
options := serveOptions{}
cmd := &cobra.Command{
Use: "serve [flags] [config file]",
Short: "Launch Dex",
Example: "dex serve config.yaml",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
cmd.SilenceErrors = true
options.config = args[0]
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 serve(cmd *cobra.Command, args []string) error { func runServe(options serveOptions) error {
switch len(args) { configFile := options.config
default:
return errors.New("surplus arguments")
case 0:
// TODO(ericchiang): Consider having a default config file location.
return errors.New("no arguments provided")
case 1:
}
configFile := args[0]
configData, err := ioutil.ReadFile(configFile) configData, err := ioutil.ReadFile(configFile)
if err != nil { if err != nil {
return fmt.Errorf("failed to read config file %s: %v", configFile, err) return fmt.Errorf("failed to read config file %s: %v", configFile, err)
@ -65,6 +80,8 @@ func serve(cmd *cobra.Command, args []string) 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)
@ -384,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
}
}

View file

@ -13,11 +13,14 @@ func commandVersion() *cobra.Command {
return &cobra.Command{ return &cobra.Command{
Use: "version", Use: "version",
Short: "Print the version and exit", Short: "Print the version and exit",
Run: func(cmd *cobra.Command, args []string) { Run: func(_ *cobra.Command, _ []string) {
fmt.Printf(`dex Version: %s fmt.Printf(
Go Version: %s "Dex Version: %s\nGo Version: %s\nGo OS/ARCH: %s %s\n",
Go OS/ARCH: %s %s version.Version,
`, version.Version, runtime.Version(), runtime.GOOS, runtime.GOARCH) runtime.Version(),
runtime.GOOS,
runtime.GOARCH,
)
}, },
} }
} }