Ejemplo n.º 1
0
// LoadModuleSettings loads the given module's configuration.
//
// module is the name of the module, e.g. "data"
// cfgPath is the path to the configuration directory
// settings is a pointer to a struct to be filled with the module settings. It
// must contain a field named "Monsti" of type settings.Monsti to be filled
// with Monsti's common settings.
func LoadModuleSettings(module, cfgPath string, settings interface{}) error {
	// Value checking
	value := reflect.ValueOf(settings)
	if !value.IsValid() || value.Kind() != reflect.Ptr ||
		reflect.Indirect(value).Kind() != reflect.Struct {
		return fmt.Errorf("util: LoadModuleSettings expects its third " +
			"argument to be a pointer to a struct")
	}
	monstiSettings := reflect.Indirect(value).FieldByName("Monsti")
	if reflect.ValueOf(monstiSettings).Kind() != reflect.Struct {
		return fmt.Errorf("util: LoadModuleSettings expects its third " +
			`argument to contain a field named "Monsti" of type ` +
			`settings.Monsti`)
	}

	// Load module settings
	path := filepath.Join(cfgPath, module+".yaml")
	if err := yaml.Parse(path, settings); err != nil {
		return fmt.Errorf("util: Could not parse module settings: %v", err)
	}

	// Load Monsti settings
	monstiSettingsRet, err := LoadMonstiSettings(cfgPath)
	if err != nil {
		return fmt.Errorf("util: Could not load monsti settings: %v", err)
	}
	monstiSettings.Set(reflect.ValueOf(*monstiSettingsRet))
	return nil
}
Ejemplo n.º 2
0
func LoadMonstiSettings(cfgPath string) (*Monsti, error) {
	path := filepath.Join(cfgPath, "monsti.yaml")
	var settings Monsti
	if err := yaml.Parse(path, &settings); err != nil {
		return nil, fmt.Errorf("util: Could not parse Monsti settings: %v", err)
	}
	settings.Directories.Config = cfgPath
	MakeAbsolute(&settings.Directories.Data, cfgPath)
	MakeAbsolute(&settings.Directories.Share, cfgPath)
	MakeAbsolute(&settings.Directories.Locale, cfgPath)
	MakeAbsolute(&settings.Directories.Run, cfgPath)
	return &settings, nil
}