ReadConfigFile returns InputSourceContext initialized from the configuration file. On repeat calls returns with the same file, returns without reading the file again; however, if value of "config" flag changes, will read the new config file
(c *cli.Context, log *zerolog.Logger)
| 382 | // On repeat calls returns with the same file, returns without reading the file again; however, |
| 383 | // if value of "config" flag changes, will read the new config file |
| 384 | func ReadConfigFile(c *cli.Context, log *zerolog.Logger) (settings *configFileSettings, warnings string, err error) { |
| 385 | configFile := c.String("config") |
| 386 | if configuration.Source() == configFile || configFile == "" { |
| 387 | if configuration.Source() == "" { |
| 388 | return nil, "", ErrNoConfigFile |
| 389 | } |
| 390 | return &configuration, "", nil |
| 391 | } |
| 392 | |
| 393 | log.Debug().Msgf("Loading configuration from %s", configFile) |
| 394 | file, err := os.Open(configFile) |
| 395 | if err != nil { |
| 396 | // If does not exist and config file was not specificly specified then return ErrNoConfigFile found. |
| 397 | if os.IsNotExist(err) && !c.IsSet("config") { |
| 398 | err = ErrNoConfigFile |
| 399 | } |
| 400 | return nil, "", err |
| 401 | } |
| 402 | defer file.Close() |
| 403 | if err := yaml.NewDecoder(file).Decode(&configuration); err != nil { |
| 404 | if err == io.EOF { |
| 405 | log.Error().Msgf("Configuration file %s was empty", configFile) |
| 406 | return &configuration, "", nil |
| 407 | } |
| 408 | return nil, "", errors.Wrap(err, "error parsing YAML in config file at "+configFile) |
| 409 | } |
| 410 | configuration.sourceFile = configFile |
| 411 | |
| 412 | // Parse it again, with strict mode, to find warnings. |
| 413 | if file, err := os.Open(configFile); err == nil { |
| 414 | decoder := yaml.NewDecoder(file) |
| 415 | decoder.KnownFields(true) |
| 416 | var unusedConfig configFileSettings |
| 417 | if err := decoder.Decode(&unusedConfig); err != nil { |
| 418 | warnings = err.Error() |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | return &configuration, warnings, nil |
| 423 | } |
| 424 | |
| 425 | // A CustomDuration is a Duration that has custom serialization for JSON. |
| 426 | // JSON in Javascript assumes that int fields are 32 bits and Duration fields are deserialized assuming that numbers |