[X] I've searched for similar issues and couldn't find anything matching
[X] I've discussed this feature request in the OpenIMSDK Slack and got positive feedback
Is this feature request related to a problem?
✅ Yes
Problem Description
I've been examining the LoadConfiguration function in our openim project and noticed some areas where we could potentially enhance its functionality and robustness. The function currently utilizes Cobra and Viper for command-line flags, environment variables, and config file management, but there might be room for improvements in terms of flexibility and error handling.
Solution Description
Current Behavior
The function LoadConfiguration:
Binds command-line flags to Viper.
Reads configurations from environment variables and a config file (if specified).
Prioritizes configurations in the order: Command-Line Flags > Environment Variables > Config File.
Performs error handling primarily for config file loading and flag requirements.
Benefits
Dynamic Configuration Reloading: Consider adding support for dynamic reloading of configuration without needing to restart the application. This could be particularly useful for long-running applications.
Improved Error Messages: While the function currently handles errors, the error messages could be more descriptive, providing better insights for debugging and configuration issues.
Enhanced Documentation: Adding more comprehensive documentation or comments within the code, explaining the priority order and how each part of the configuration is loaded and processed.
Config Validation Logic: Introduce a validation step for the configurations loaded. This could include checking for conflicting settings or ensuring all necessary configurations are loaded correctly before the application starts.
Environment-Specific Configurations: Implement a mechanism to load different configurations based on the environment (development, staging, production, etc.), which could be specified via an environment variable or a command-line flag.
func LoadConfiguration(cfgFile string, cmd *cobra.Command, requiredFlags []string) (*Options, error) {
v := viper.New()
cmd.Flags().VisitAll(func(flag *flag.Flag) {
flagName := flag.Name
if flagName != "config" && flagName != "help" {
if err := v.BindPFlag(flagName, flag); err != nil {
// can't really happen
panic(fmt.Sprintln(errors.Wrapf(err, "Error binding flag '%s'", flagName)))
}
}
})
v.AutomaticEnv()
v.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
v.SetEnvPrefix("CR")
if cfgFile != "" {
v.SetConfigFile(cfgFile)
} else {
v.SetConfigName("cr")
for _, searchLocation := range configSearchLocations {
v.AddConfigPath(searchLocation)
}
}
if err := v.ReadInConfig(); err != nil {
if cfgFile != "" {
// Only error out for specified config file. Ignore for default locations.
return nil, errors.Wrap(err, "Error loading config file")
}
} else {
fmt.Println("Using config file: ", v.ConfigFileUsed())
}
opts := &Options{}
if err := v.Unmarshal(opts); err != nil {
return nil, errors.Wrap(err, "Error unmarshaling configuration")
}
if opts.Push && opts.PR {
return nil, errors.New("specify either --push or --pr, but not both")
}
elem := reflect.ValueOf(opts).Elem()
for _, requiredFlag := range requiredFlags {
fieldName := kebabCaseToTitleCamelCase(requiredFlag)
f := elem.FieldByName(fieldName)
value := fmt.Sprintf("%v", f.Interface())
if value == "" {
return nil, errors.Errorf("'--%s' is required", requiredFlag)
}
}
return opts, nil
}
Additional Information
The current implementation serves its purpose but enhancing these aspects could significantly improve the usability and maintainability of the openim configuration process. This could also align with best practices for modern application development.
I believe these enhancements would greatly benefit our project and am keen to discuss them further with the team.
Checklist
Is this feature request related to a problem?
✅ Yes
Problem Description
I've been examining the
LoadConfiguration
function in ouropenim
project and noticed some areas where we could potentially enhance its functionality and robustness. The function currently utilizes Cobra and Viper for command-line flags, environment variables, and config file management, but there might be room for improvements in terms of flexibility and error handling.Solution Description
Current Behavior
The function
LoadConfiguration
:Benefits
Example:
Additional Information
The current implementation serves its purpose but enhancing these aspects could significantly improve the usability and maintainability of the
openim
configuration process. This could also align with best practices for modern application development.I believe these enhancements would greatly benefit our project and am keen to discuss them further with the team.