Skip to content

Commit

Permalink
refactor: return config from load function
Browse files Browse the repository at this point in the history
  • Loading branch information
doron-cohen committed Sep 26, 2020
1 parent aa6f247 commit fdfc4d0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cmd/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var cleanCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
log.Println("Cleaning up!")

err := rules.LoadRulesConfig("rules.yaml")
_, err := rules.LoadRulesConfig("rules.yaml")
if err != nil {
log.Fatalln("Failed to read rules file: ", err)
}
Expand Down
12 changes: 6 additions & 6 deletions internal/rules/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ type RulesConfig struct {

var rulesConfig RulesConfig

func LoadRulesConfig(filepath string) error {
func LoadRulesConfig(filepath string) (RulesConfig, error) {
log.Printf("Loading rules config file %s", filepath)
rulesBytes, err := ioutil.ReadFile(filepath)
if err != nil {
return err
return RulesConfig{}, err
}

var rawConfig map[string]interface{}
err = yaml.Unmarshal(rulesBytes, &rawConfig)
if err != nil {
return err
return RulesConfig{}, err
}

config := &mapstructure.DecoderConfig{
Expand All @@ -35,14 +35,14 @@ func LoadRulesConfig(filepath string) error {

decoder, err := mapstructure.NewDecoder(config)
if err != nil {
return err
return RulesConfig{}, err
}

err = decoder.Decode(rawConfig)
if err != nil {
return err
return RulesConfig{}, err
}

log.Printf("Loaded %d rules", len(rulesConfig.Rules))
return nil
return rulesConfig, nil
}

0 comments on commit fdfc4d0

Please sign in to comment.