Skip to content

Commit

Permalink
Config from code (fclairamb#714) (fclairamb#749)
Browse files Browse the repository at this point in the history
Allow Config to be created without a file.

It's sometimes desirable to create a server that is configured by logic
that creates the `Content`. Prior to this commit, such content needed
to be written to a temporary file and then loaded into memory again in
order to get a complete `Config`.

This commit adds a `FromContent` method, allowing the `Config` to be
created directly from a `Content`.

Authored-by: Thomas Hallgren <[email protected]>
  • Loading branch information
fclairamb authored Jul 29, 2022
1 parent d0e8a31 commit e2524a2
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,22 @@ func NewConfig(fileName string, logger log.Logger) (*Config, error) {
return config, nil
}

// FromContent creates a new config instance from a pre-created Content and logger. The
// fileName should indicate origin of the given Content, but the file will never be opened.
func FromContent(content *confpar.Content, fileName string, logger log.Logger) (*Config, error) {
c := &Config{
fileName: fileName,
logger: logger,
Content: content,
}

if err := c.Prepare(); err != nil {
return nil, err
}

return c, nil
}

// Load the config
func (c *Config) Load() error {
file, errOpen := os.Open(c.fileName)
Expand Down

0 comments on commit e2524a2

Please sign in to comment.