11/*
2- Package conf config.go defines functions to read file configuration using Viper.
2+ Package conf reads values from a configuration file such as JSON, TOML, YAML and etc using Viper.
33
44It was separated to a different package from `cmd` to ease testing and re-use.
55
@@ -19,24 +19,29 @@ import (
1919 "github.com/spf13/viper"
2020)
2121
22- // TConfigApp defines the data structure to store basic config of the app.
23- type TConfigApp struct {
24- // one-liner-conf
22+ // TConfigApp is an alias of TConfigFile for backward compatibility and it will be removed in v1.3.0.
23+ // Deprecated: as of v1.2.1. Use TConfigFile instead.
24+ type TConfigApp = TConfigFile
25+
26+ // TConfigFile defines the data structure to store the information of the file to load.
27+ type TConfigFile struct {
28+ /* one-liner-conf of file path */
2529
2630 PathFileConf string // File path of config. If set, will have priority than NameFileConf and PathDirConf.
2731
28- // parted conf
32+ /* parted conf of file path */
2933
3034 PathDirConf string // Dir path of config to search.
3135 NameFileConf string // File name of config file. May or may not have an extension.
3236 NameTypeConf string // File extension. REQUIRED if the conf file does not have the extension.
3337
38+ /* configuration of it's state */
3439 IsUsingDefaultConf bool // Flag to determine if the app is using the default value or conf file value.
3540}
3641
37- // GetNameConf is a method of TConfigApp that returns the config file name.
38- func (c TConfigApp ) GetNameConf () string {
39- if "" != c . PathFileConf {
42+ // GetNameConf is a method of TConfigFile that returns the config file name.
43+ func (c TConfigFile ) GetNameConf () string {
44+ if c . PathFileConf != "" {
4045 return filepath .Base (c .PathFileConf )
4146 }
4247
@@ -47,19 +52,23 @@ func (c TConfigApp) GetNameConf() string {
4752 return c .NameFileConf + "." + c .NameTypeConf
4853}
4954
50- // LoadConfig() stores values from the config file or env variables to userConfig.
55+ // LoadConfig is an alias of LoadFile for backward compatibility and it will be removed in v1.3.0.
56+ // Deprecated: as of v1.2.1. Use LoadFile() instead.
57+ var LoadConfig = LoadFile
58+
59+ // LoadFile() stores values from the config file to userConfig.
5160//
5261// @args
53- // appConfig TConfigApp : Basic configuration to read the conf file.
54- // userConfig struct : An object to store values from conf file.
62+ // appConfig TConfigFile : Basic configuration to read the conf file.
63+ // userConfig struct : An object to store values from conf file.
5564// @return
56- // err error : If fails to read/store values from conf file returns error othersise nil.
65+ // err error : If fails to read/store values from conf file returns error othersise nil.
5766// Usage:
5867// type TConfUser struct {
5968// MyValue string `mapstructure:"my_value"`
6069// }
6170// var (
62- // configApp = conf.TConfigApp {
71+ // configApp = conf.TConfigFile {
6372// PathDirConf: ".",
6473// NameFileConf: "userConfig",
6574// NameTypeConf: "json",
@@ -68,33 +77,30 @@ func (c TConfigApp) GetNameConf() string {
6877// MyValue: "",
6978// }
7079// )
71- // if err := conf.LoadConfig (*configApp, &configUser); err != nil {
80+ // if err := conf.LoadFile (*configApp, &configUser); err != nil {
7281// // do something with the error
7382// }
7483// myValue := configUser.MyValue
75- func LoadConfig (appConfig TConfigApp , userConfig interface {}) (err error ) {
76- // Reset current stored values in viper
77- viper .Reset ()
78-
79- // Read values from the ENV variable if set
80- viper .AutomaticEnv ()
84+ func LoadFile (appConfig TConfigFile , userConfig interface {}) (err error ) {
85+ // pitViper is a temporary viper instance
86+ pitViper := viper .New ()
8187
8288 // Set file path to search
83- if "" != appConfig . PathFileConf {
89+ if appConfig . PathFileConf != "" {
8490 // Set one-liner file path
85- viper .SetConfigFile (appConfig .PathFileConf )
91+ pitViper .SetConfigFile (appConfig .PathFileConf )
8692 } else {
8793 // Set inidividual file path info
88- viper .AddConfigPath (appConfig .PathDirConf )
89- viper .SetConfigName (appConfig .NameFileConf )
90- viper .SetConfigType (appConfig .NameTypeConf )
94+ pitViper .AddConfigPath (appConfig .PathDirConf )
95+ pitViper .SetConfigName (appConfig .NameFileConf )
96+ pitViper .SetConfigType (appConfig .NameTypeConf )
9197 }
9298
9399 // Search and read values from the config file and stores to "userConfig"
94- err = viper .ReadInConfig ()
100+ err = pitViper .ReadInConfig ()
95101 if err == nil {
96102 // Map the read config values
97- err = viper .Unmarshal (& userConfig )
103+ err = pitViper .Unmarshal (& userConfig )
98104 }
99105
100106 return err // return error if viper fails to read or map the values
0 commit comments