muesli / beehive

A flexible event/agent & automation system with lots of bees 🐝
GNU Affero General Public License v3.0
6.3k stars 324 forks source link

Load and write config from/to standard paths #286

Closed rubiojr closed 4 years ago

rubiojr commented 4 years ago

This changes Beehive's behavior.

When writting the config file for the first time (i.e. if beehive was run without a config), the file will be written to ~/.config/beehive/beehive.conf (OS dependant) instead of the current directory.

Loading the config from the current directory, if available, is still preferred, so that doesn't change.

Fixes #283

rubiojr commented 4 years ago

I'll need to fix the tests. I added os.UserConfigDir() which was introduced in Go 1.13

rubiojr commented 4 years ago

@muesli I think the last commit addresses your feedback, simplifying the logic a bit also.

Usage of ./beehive:
  -bind string
        Which address to bind Beehive's API & admin interface to (default "localhost:8181")
  -canonicalurl string
        Canonical URL for the API & admin interface (default "http://localhost:8181")
  -config string
        Config-file to use (default "/home/rubiojr/.config/beehive/beehive.conf")
  -debug
        Turn on debugging
  -version
        Beehive version

I tried using gap's LookupConfig, but complicated the code to exclude system paths unfortunately, so defaulted to something simpler. Operating in ready-only mode or loading from system path and writing to user dir is probably a good topic for a new issue/discussion/PR.

rubiojr commented 4 years ago

I don't have a Windows host around, but seems to be working fine on macOS:

➜  beehive git:(config-dir) ./beehive --help
Usage of ./beehive:
  -bind string
        Which address to bind Beehive's API & admin interface to (default "localhost:8181")
  -canonicalurl string
        Canonical URL for the API & admin interface (default "http://localhost:8181")
  -config string
        Config-file to use (default "/Users/rubiojr/Library/Preferences/beehive/beehive.conf")
  -debug
        Turn on debugging
  -version
        Beehive version
➜  beehive git:(config-dir) ./beehive 
INFO[0000] Registering Resource                          Resource=HiveResource
INFO[0000] Registering Resource                          Resource=BeeResource
INFO[0000] Registering Resource                          Resource=ChainResource
INFO[0000] Registering Resource                          Resource=ActionResource
INFO[0000] Registering Resource                          Resource=LogResource
INFO[0000]                                              
INFO[0000] Beehive is buzzing...                        
INFO[0000] No config file found, loading defaults       
^CINFO[0000] Got signal: interrupt                        
INFO[0000] Storing config to /Users/rubiojr/Library/Preferences/beehive/beehive.conf 
rubiojr commented 4 years ago

@muesli the last commit should address your feedback.

Took the chance to make the cfg package public interface a bit more idiomatic, renaming the LoadConfig and SaveConfig public functions, at the expense of breaking backwards compat (assuming no one cares about Beehive's cfg package that should probably be in internal or something).

rubiojr commented 4 years ago

Superseded by https://github.com/muesli/beehive/pull/292