From cdefd1f788fdce00230e80a93a21cd3008ef3b9d Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Thu, 14 Jan 2021 15:30:03 +0100 Subject: [PATCH] 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)