beetbox/confuse

How to check if configuration exists without creating it?

nmstoker opened this issue · 3 comments

Hello,

I'm new to Confuse so I'm sorry if this is obvious, but can anyone advise on how I could solve a particular scenario.

I'm building a CLI tool that can be used by people without config but has an option to install config if they wish (eg once they decide they like the tool and would use it enough to warrant it).

Right now I can't see a clean way to check if the config folder exists without automatically creating it. I see that there's config.config_dir() but as per the comments, this creates the directory if it doesn't exist, so the process of my app checking if it has config installed starts to create it.

Ideally I'd like to leverage the code that looks in all the platform specific places to establish where the directory would be, if it were to exist , but then not put it there (out of politeness to the users, as I don't want to presume they're fine with things being written without their direction to do so 🙂 )

Is there a way to do this?

Many thanks,
Neil

For now, looks like simplest method may just be to use util.config_dirs() and construct the directories from that, appending my appname myself, and then check for existence of them. That would forgo the environment directory part (unless I recreate that part manually too) but it's a fair workaround, unless some other approach is suggested (or a PR to make app directory creation optional is submitted)

Yep, I think you've basically got it! We don't currently have a way to do that; the way to fix it would be to add an extra option to the config_dir method; and walking config_dirs manually is a good workaround.

At a somewhat higher level, the general workflow you describe (a tool that can be used with or without a config, and you can let users create a config if they want) is also what we do in beets, for which Confuse was created. If you run beets without creating a configuration, it doesn't create anything because it never asks for config_dir. But you can also type beet config -e to edit your configuration, in which case it creates the file and opens it in a text editor.

Thanks for getting back to me @sampsyo , it's really helpful to know I'm on the right track.

Thanks also for drawing my attention to beets too - I'll take a look (as it looks useful itself and sounds like it addresses some similar low level scenarios to those I'm working on)