1
0
mirror of https://blitiri.com.ar/repos/chasquid synced 2025-12-21 15:17:01 +00:00

config: Allow overrides from the command line

This patch allows the configuration values to be overridden from the
command-line, with a new -config_overrides flag.

There is a fairly specific use case for this, when editing the
configuration file is not feasible or convenient (e.g. running an
user-supplied configuration in a managed environment).
This commit is contained in:
Alberto Bertogli
2020-05-16 23:22:00 +01:00
parent 7909b479eb
commit 4c28efcb20
6 changed files with 54 additions and 14 deletions

View File

@@ -33,8 +33,8 @@ var defaultConfig = &Config{
MailLogPath: "<syslog>",
}
// Load the config from the given file.
func Load(path string) (*Config, error) {
// Load the config from the given file, with the given overrides.
func Load(path, overrides string) (*Config, error) {
// Start with a copy of the default config.
c := proto.Clone(defaultConfig).(*Config)
@@ -51,6 +51,14 @@ func Load(path string) (*Config, error) {
}
override(c, fromFile)
// Handle command line overrides.
fromOverrides := &Config{}
err = prototext.Unmarshal([]byte(overrides), fromOverrides)
if err != nil {
return nil, fmt.Errorf("parsing override: %v", err)
}
override(c, fromOverrides)
// Handle hostname separate, because if it is set, we don't need to call
// os.Hostname which can fail.
if c.Hostname == "" {