Пример #1
0
// Version returns the current config file format version
func (d config) Version() string {
	st := structs.New(d.data)

	f, ok := st.FieldOk("Version")
	if !ok {
		return ""
	}

	val := f.Value()
	ver, ok := val.(string)
	if ok {
		return ver
	}
	return ""
}
Пример #2
0
// CheckData - checks the validity of config data. Data sould be of type struct and contain a string type field called "Version"
func CheckData(data interface{}) *probe.Error {
	if !structs.IsStruct(data) {
		return probe.NewError(fmt.Errorf("Invalid argument type. Expecing \"struct\" type."))
	}

	st := structs.New(data)
	f, ok := st.FieldOk("Version")
	if !ok {
		return probe.NewError(fmt.Errorf("Invalid type of struct argument. No [%s.Version] field found.", st.Name()))
	}

	if f.Kind() != reflect.String {
		return probe.NewError(fmt.Errorf("Invalid type of struct argument. Expecting \"string\" type [%s.Version] field.", st.Name()))
	}

	return nil
}
Пример #3
0
// Load - loads JSON config from file and merge with currently set values
func (d *config) Load(filename string) *probe.Error {
	d.lock.Lock()
	defer d.lock.Unlock()

	_, err := os.Stat(filename)
	if err != nil {
		return probe.NewError(err)
	}

	fileData, err := ioutil.ReadFile(filename)
	if err != nil {
		return probe.NewError(err)
	}

	if runtime.GOOS == "windows" {
		fileData = []byte(strings.Replace(string(fileData), "\r\n", "\n", -1))
	}

	st := structs.New(d.data)
	f, ok := st.FieldOk("Version")
	if !ok {
		return probe.NewError(fmt.Errorf("Argument struct [%s] does not contain field \"Version\".", st.Name()))
	}

	err = json.Unmarshal(fileData, d.data)
	if err != nil {
		return probe.NewError(err)
	}

	if err := CheckData(d.data); err != nil {
		return err.Trace()
	}

	if (*d).Version() != f.Value() {
		return probe.NewError(fmt.Errorf("Version mismatch"))
	}

	return nil
}