refactor: serve command

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

View File

@ -29,32 +29,31 @@ import (
"github.com/dexidp/dex/storage" "github.com/dexidp/dex/storage"
) )
type serveOptions struct {
config string
}
func commandServe() *cobra.Command { func commandServe() *cobra.Command {
options := serveOptions{}
return &cobra.Command{ return &cobra.Command{
Use: "serve [ config file ]", Use: "serve [flags] [config file]",
Short: "Connect to the storage and begin serving requests.", Short: "Launch Dex",
Long: ``,
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 { 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)