Config should be read from cwd before user profile (#225)

fixes #219
This commit is contained in:
StashAppDev
2019-11-26 13:11:42 -08:00
committed by GitHub
parent 2a02e5a65d
commit 7c97e36af8

View File

@@ -25,6 +25,11 @@ type singleton struct {
var instance *singleton var instance *singleton
var once sync.Once var once sync.Once
type flagStruct struct {
configFilePath string
}
var flags = flagStruct{}
func GetInstance() *singleton { func GetInstance() *singleton {
Initialize() Initialize()
return instance return instance
@@ -33,9 +38,9 @@ func GetInstance() *singleton {
func Initialize() *singleton { func Initialize() *singleton {
once.Do(func() { once.Do(func() {
_ = utils.EnsureDir(paths.GetConfigDirectory()) _ = utils.EnsureDir(paths.GetConfigDirectory())
initFlags()
initConfig() initConfig()
initLog() initLog()
initFlags()
initEnvs() initEnvs()
instance = &singleton{ instance = &singleton{
Status: TaskStatus{Status: Idle, Progress: -1}, Status: TaskStatus{Status: Idle, Progress: -1},
@@ -55,8 +60,11 @@ func initConfig() {
// The config file is called config. Leave off the file extension. // The config file is called config. Leave off the file extension.
viper.SetConfigName("config") viper.SetConfigName("config")
viper.AddConfigPath("$HOME/.stash") // Look for the config in the home directory if flagConfigFileExists, _ := utils.FileExists(flags.configFilePath); flagConfigFileExists {
viper.SetConfigFile(flags.configFilePath)
}
viper.AddConfigPath(".") // Look for config in the working directory viper.AddConfigPath(".") // Look for config in the working directory
viper.AddConfigPath("$HOME/.stash") // Look for the config in the home directory
err := viper.ReadInConfig() // Find and read the config file err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file if err != nil { // Handle errors reading the config file
@@ -65,6 +73,7 @@ func initConfig() {
panic(err) panic(err)
} }
} }
logger.Infof("using config file: %s", viper.ConfigFileUsed())
viper.SetDefault(config.Database, paths.GetDefaultDatabaseFilePath()) viper.SetDefault(config.Database, paths.GetDefaultDatabaseFilePath())
@@ -91,6 +100,7 @@ func initConfig() {
func initFlags() { func initFlags() {
pflag.IP("host", net.IPv4(0, 0, 0, 0), "ip address for the host") pflag.IP("host", net.IPv4(0, 0, 0, 0), "ip address for the host")
pflag.Int("port", 9999, "port to serve from") pflag.Int("port", 9999, "port to serve from")
pflag.StringVarP(&flags.configFilePath, "config", "c", "", "config file to use")
pflag.Parse() pflag.Parse()
if err := viper.BindPFlags(pflag.CommandLine); err != nil { if err := viper.BindPFlags(pflag.CommandLine); err != nil {