-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#11 Added config provider & loading a config file
- Loading branch information
1 parent
3625a4a
commit ae80099
Showing
6 changed files
with
82 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
// Provider reads, collects, validates and process config files | ||
type Provider struct { | ||
Config *Config | ||
} | ||
|
||
// NewProvider creates a instance of Config Provider | ||
func NewProvider() *Provider { | ||
return &Provider{ | ||
Config: nil, | ||
} | ||
} | ||
|
||
func (p *Provider) Load(configPath string) (*Provider, error) { | ||
rawConfig, err := os.ReadFile(filepath.Clean(configPath)) | ||
if err != nil { | ||
return p, fmt.Errorf("unable to read the file %v: %w", configPath, err) | ||
} | ||
|
||
var cfg *Config | ||
|
||
if err := yaml.Unmarshal(rawConfig, &cfg); err != nil { | ||
return p, fmt.Errorf("unable to serialize the file %v: %w", configPath, err) | ||
} | ||
|
||
p.Config = cfg | ||
|
||
return p, nil | ||
} | ||
|
||
func (p *Provider) Get() *Config { | ||
return p.Config | ||
} | ||
|
||
func (p *Provider) Start() { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters