spf13 / viper

Go configuration with fangs
MIT License
26.29k stars 2.01k forks source link

Should Viper add a Close() method? #1771

Closed lvyonghuan closed 4 months ago

lvyonghuan commented 4 months ago

Preflight Checklist

Problem Description

I am using Viper to read JSON formatted files. I use this function to retrieve the file.

func (conf *ConfigManager) InitModuleConfig(moduleName string) (err error) {
    conf.viperList[moduleName] = viper.New()
    conf.viperList[moduleName].SetConfigType("json")
    conf.viperList[moduleName].SetConfigName(moduleName)
    conf.viperList[moduleName].AddConfigPath(conf.configPath + "/")
    return nil
}

My program requires deleting configuration files. However, when I attempt to remove the configuration file, it prompts:

    call_test.go:43: remove ./configs/test.json: The process cannot access the file because it is being used by another process.

I suspect that Viper is holding onto this file, causing the deletion to fail. I tried to find a close method to release Viper's hold on the file, but I couldn't find one.

Or could there be other possibilities?

Proposed Solution

Add a close() method for viper.viper.

Alternatives Considered

No response

Additional Information

But I'm curious, given the large user base of Viper and the number of years it has been in use, it's surprising that my requirement hasn't come up before. When I searched for "The process cannot access the file because it is being used by another process" in the issues, I found only one related problem. This makes me hesitant about whether I should raise this issue. I've only been studying software design for a year, and I'm not very proficient in many things. If my understanding is incorrect, please correct me.

github-actions[bot] commented 4 months ago

👋 Thanks for reporting!

A maintainer will take a look at your issue shortly. 👀

In the meantime: We are working on Viper v2 and we would love to hear your thoughts about what you like or don't like about Viper, so we can improve or fix those issues.

⏰ If you have a couple minutes, please take some time and share your thoughts: https://forms.gle/R6faU74qPRPAzchZ9

📣 If you've already given us your feedback, you can still help by spreading the news, either by sharing the above link or telling people about this on Twitter:

https://twitter.com/sagikazarmark/status/1306904078967074816

Thank you! ❤️

lvyonghuan commented 4 months ago

Sorry, my mistake. The problem was on my end. I forgot to close the file after creating it, which led to the inability to delete it.