From cdefd1f788fdce00230e80a93a21cd3008ef3b9d Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Thu, 14 Jan 2021 15:30:03 +0100 Subject: [PATCH 1/3] refactor: serve command Signed-off-by: Mark Sagi-Kazar --- cmd/dex/serve.go | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/cmd/dex/serve.go b/cmd/dex/serve.go index ca740593..858281af 100644 --- a/cmd/dex/serve.go +++ b/cmd/dex/serve.go @@ -29,32 +29,31 @@ import ( "github.com/dexidp/dex/storage" ) +type serveOptions struct { + config string +} + func commandServe() *cobra.Command { + options := serveOptions{} + return &cobra.Command{ - Use: "serve [ config file ]", - Short: "Connect to the storage and begin serving requests.", - Long: ``, + Use: "serve [flags] [config file]", + Short: "Launch Dex", Example: "dex serve config.yaml", - Run: func(cmd *cobra.Command, args []string) { - if err := serve(cmd, args); err != nil { - fmt.Fprintln(os.Stderr, err) - os.Exit(2) - } + 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) }, } } -func serve(cmd *cobra.Command, args []string) error { - switch len(args) { - 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] +func runServe(options serveOptions) error { + configFile := options.config configData, err := ioutil.ReadFile(configFile) if err != nil { return fmt.Errorf("failed to read config file %s: %v", configFile, err) From c55d84b5d2f926eb87208fceff3867b8713b815b Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Thu, 14 Jan 2021 15:50:19 +0100 Subject: [PATCH 2/3] feat: add flags for bind address config options Signed-off-by: Mark Sagi-Kazar --- cmd/dex/serve.go | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/cmd/dex/serve.go b/cmd/dex/serve.go index 858281af..634e2606 100644 --- a/cmd/dex/serve.go +++ b/cmd/dex/serve.go @@ -30,13 +30,20 @@ import ( ) type serveOptions struct { + // Config file path config string + + // Flags + webHTTPAddr string + webHTTPSAddr string + telemetryAddr string + grpcAddr string } func commandServe() *cobra.Command { options := serveOptions{} - return &cobra.Command{ + cmd := &cobra.Command{ Use: "serve [flags] [config file]", Short: "Launch Dex", Example: "dex serve config.yaml", @@ -50,6 +57,15 @@ func commandServe() *cobra.Command { 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 { @@ -64,6 +80,8 @@ func runServe(options serveOptions) error { return fmt.Errorf("error parse config file %s: %v", configFile, err) } + applyConfigOverrides(options, &c) + logger, err := newLogger(c.Logger.Level, c.Logger.Format) if err != nil { return fmt.Errorf("invalid config: %v", err) @@ -383,3 +401,21 @@ func newLogger(level string, format string) (log.Logger, error) { Level: logLevel, }, 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 + } +} From 6742008fc2874d9ccccfe4a466e9343c660b39a9 Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Thu, 14 Jan 2021 15:55:07 +0100 Subject: [PATCH 3/3] refactor: version command Signed-off-by: Mark Sagi-Kazar --- cmd/dex/version.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/cmd/dex/version.go b/cmd/dex/version.go index b74cb016..de206e16 100644 --- a/cmd/dex/version.go +++ b/cmd/dex/version.go @@ -13,11 +13,14 @@ func commandVersion() *cobra.Command { return &cobra.Command{ Use: "version", Short: "Print the version and exit", - Run: func(cmd *cobra.Command, args []string) { - fmt.Printf(`dex Version: %s -Go Version: %s -Go OS/ARCH: %s %s -`, version.Version, runtime.Version(), runtime.GOOS, runtime.GOARCH) + Run: func(_ *cobra.Command, _ []string) { + fmt.Printf( + "Dex Version: %s\nGo Version: %s\nGo OS/ARCH: %s %s\n", + version.Version, + runtime.Version(), + runtime.GOOS, + runtime.GOARCH, + ) }, } }