Skip to content

Commit

Permalink
feat: change configure file load method
Browse files Browse the repository at this point in the history
  • Loading branch information
bzp2010 committed Jun 21, 2021
1 parent a03ec49 commit 4403a17
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion api/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ var rootCmd = &cobra.Command{
func init() {
cobra.OnInitialize(onInitialize)

rootCmd.PersistentFlags().StringVarP(&conf.ConfigFile, "config", "c", "./conf/conf.yaml", "config file")
rootCmd.PersistentFlags().StringVarP(&conf.ConfigFile, "config", "c", "", "config file")
rootCmd.PersistentFlags().StringVarP(&conf.WorkDir, "work-dir", "p", ".", "current work directory")

rootCmd.AddCommand(
Expand Down
12 changes: 10 additions & 2 deletions api/internal/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,20 @@ func InitConf() {
}

func setConf() {
viper.SetConfigFile(ConfigFile)
// if not input config file path, search config file in default path and file name
if ConfigFile != "" {
viper.SetConfigFile(ConfigFile)
} else {
viper.SetConfigName("conf.yaml")
viper.AddConfigPath(WorkDir)
viper.AddConfigPath(WorkDir+"/conf/")
}
viper.AutomaticEnv()

if err := viper.ReadInConfig(); err != nil {
panic(fmt.Sprintf("fail to read configuration: %s", ConfigFile))
panic(fmt.Sprintf("fail to load configuration: %v", err))
}
log.Printf("config file used: %s", viper.ConfigFileUsed())

var err error
config := Config{}
Expand Down

0 comments on commit 4403a17

Please sign in to comment.