Last active
August 29, 2015 14:05
-
-
Save niedbalski/797849ddb6144161ac84 to your computer and use it in GitHub Desktop.
config.go
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
//NewConfigFromFile function loads a YAML file and returns | |
//a pointer to a newly create Configuration struct | |
func NewConfigFromFile(path string) (*Config, error) { | |
cfg := New(Config) | |
if _, err := os.Stat(path); os.IsNotExist(err) { | |
return nil, errors.New("Cannot open configuration file") | |
} | |
readed, err := ioutil.ReadFile(path) | |
if err != nil { | |
return nil, errors.New("Cannot read configuration file") | |
} | |
config := make(map[interface{}]interface{}) | |
err = goyaml.Unmarshal(readed, &m) | |
if err != nil { | |
return nil, errors.New(fmt.Sprintf("error: %v", err)) | |
} | |
for _, required := range []string{"services", "levels"} { | |
_, ok := config[required] | |
if !ok { | |
return nil, errors.New(fmt.Sprintf( | |
"configuration key(%s) not found", required)) | |
} | |
} | |
servicesFiles, err := filepath.Glob(config["services"]) | |
if err != nil { | |
return nil, errors.New("cannot find any defined services") | |
} | |
for _, serviceFile := range servicesFiles { | |
service, err := core.NewServiceFromFile(serviceFile) | |
if err != nil { | |
return nil, err | |
} | |
cfg.services = append(cfg.services, &service) | |
} | |
for _, level := range config["levels"] { | |
level, err := core.NewLevel(level) | |
if err != nil { | |
return nil, err | |
} | |
cfg.levels = append(cfg.levels, &level) | |
} | |
return &cfg, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment