package tools import ( "strings" "cloud.o-forge.io/core/oc-lib/logs" "github.com/goraz/onion" ) // GetConfLoader // Get the configuration loader for the application // Parameters: // - AppName: string : the name of the application // Returns: // - *onion.Onion : the configuration loader // The configuration loader will load the configuration from the following sources: // - the environment variables with the prefix APPNAME_ // - the file /etc/oc/appname.json // - the file ./appname.json // The configuration loader will merge the configuration from the different sources // The configuration loader will give priority to the environment variables // The configuration loader will give priority to the local file over the default file func GetConfLoader(AppName string) *onion.Onion { logger := logs.GetLogger() EnvPrefix := strings.ToUpper(AppName[0:2]+AppName[3:]) + "_" defaultConfigFile := "/etc/oc/" + AppName[0:2] + ".json" localConfigFile := "./" + AppName[0:2] + ".json" var configFile string var o *onion.Onion l3 := onion.NewEnvLayerPrefix("_", EnvPrefix) l2, err := onion.NewFileLayer(localConfigFile, nil) if err == nil { logger.Info().Msg("Local config file found " + localConfigFile + ", overriding default file") configFile = localConfigFile } l1, err := onion.NewFileLayer(defaultConfigFile, nil) if err == nil { logger.Info().Msg("Config file found : " + defaultConfigFile) configFile = defaultConfigFile } if configFile == "" { logger.Info().Msg("No config file found, using env") o = onion.New(l3) } else if l1 != nil && l2 != nil { o = onion.New(l1, l2, l3) } else if l1 == nil { o = onion.New(l2, l3) } else if l2 == nil { o = onion.New(l1, l3) } return o }