Skip to content
Snippets Groups Projects
Unverified Commit ccbf6c6e authored by Márk Sági-Kazár's avatar Márk Sági-Kazár Committed by GitHub
Browse files

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

Add flags for bind address config options
parents 8515dfb3 6742008f
No related branches found
No related tags found
No related merge requests found
...@@ -29,32 +29,47 @@ import ( ...@@ -29,32 +29,47 @@ import (
"github.com/dexidp/dex/storage" "github.com/dexidp/dex/storage"
) )
type serveOptions struct {
// Config file path
config string
// Flags
webHTTPAddr string
webHTTPSAddr string
telemetryAddr string
grpcAddr string
}
func commandServe() *cobra.Command { func commandServe() *cobra.Command {
return &cobra.Command{ options := serveOptions{}
Use: "serve [ config file ]",
Short: "Connect to the storage and begin serving requests.", cmd := &cobra.Command{
Long: ``, Use: "serve [flags] [config file]",
Short: "Launch Dex",
Example: "dex serve config.yaml", Example: "dex serve config.yaml",
Run: func(cmd *cobra.Command, args []string) { Args: cobra.ExactArgs(1),
if err := serve(cmd, args); err != nil { RunE: func(cmd *cobra.Command, args []string) error {
fmt.Fprintln(os.Stderr, err) cmd.SilenceUsage = true
os.Exit(2) cmd.SilenceErrors = true
}
options.config = args[0]
return runServe(options)
}, },
} }
}
func serve(cmd *cobra.Command, args []string) error { flags := cmd.Flags()
switch len(args) {
default: flags.StringVar(&options.webHTTPAddr, "web-http-addr", "", "Web HTTP address")
return errors.New("surplus arguments") flags.StringVar(&options.webHTTPSAddr, "web-https-addr", "", "Web HTTPS address")
case 0: flags.StringVar(&options.telemetryAddr, "telemetry-addr", "", "Telemetry address")
// TODO(ericchiang): Consider having a default config file location. flags.StringVar(&options.grpcAddr, "grpc-addr", "", "gRPC API address")
return errors.New("no arguments provided")
case 1: return cmd
} }
configFile := args[0] func runServe(options serveOptions) error {
configFile := options.config
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 { ...@@ -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) { ...@@ -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
}
}
...@@ -13,11 +13,14 @@ func commandVersion() *cobra.Command { ...@@ -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,
)
}, },
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment