Skip to content

Commit

Permalink
refactor(cmd): create config in root
Browse files Browse the repository at this point in the history
  • Loading branch information
sundowndev committed Aug 23, 2020
1 parent eb774ad commit 73975fe
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
6 changes: 6 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"github.com/dreamvo/gilfoyle/config"
"os"

"github.com/spf13/cobra"
Expand All @@ -17,6 +18,11 @@ var rootCmd = &cobra.Command{
}

func init() {
err := config.NewConfig()
if err != nil {
panic(err)
}

rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file path (default ./gilfoyle.yaml")
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var serveCmd = &cobra.Command{
Use: "serve",
Short: "RegisterRoutes REST API",
Run: func(cmd *cobra.Command, args []string) {
err := db.InitClient(config.NewConfig())
err := db.InitClient(config.GetConfig())
if err != nil {
log.Fatalf("failed opening connection: %v", err)
}
Expand Down
9 changes: 6 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,13 @@ type settingsConfig struct {
var c Config

// NewConfig a new config object
func NewConfig() *Config {
_ = configor.Load(&c)
func NewConfig(files ...string) error {
err := configor.Load(&c, files...)
if err != nil {
return err
}

return &c
return nil
}

// GetConfig helps you to get configuration data
Expand Down

0 comments on commit 73975fe

Please sign in to comment.