Example #1
0
func loadKeys() (pub, prv string, err error) {
	var file ini.File
	file, err = ini.LoadFile("keys.ini")
	if err != nil {
		return
	}
	var ok bool
	pub, ok = file.Get("keys", "public")
	if !ok {
		err = errors.New("pubkey missed")
		return
	}
	prv, ok = file.Get("keys", "private")
	if !ok {
		err = errors.New("privkey missed")
		return
	}
	return
}
Example #2
0
func testConfig(appConfig ini.File) (bool, error) {
	//test if required parameters are present and are valid

	requiredFields := []setting{{"SETTINGS", "SERVERHOST"},
		{"SETTINGS", "SERVERPORT"},
		{"SETTINGS", "RETRIES"},
		{"SETTINGS", "DEVICES"},
		{"SETTINGS", "BUFFERSIZE"},
		{"SETTINGS", "BUFFERLOW"},
		{"SETTINGS", "MSGTIMEOUT"},
		{"SETTINGS", "MSGCOUNTOUT"},
		{"SETTINGS", "MSGTIMEOUTLONG"},
	}

	for _, c := range requiredFields {
		v, ok := appConfig.Get(c[0], c[1])
		if !ok || strings.TrimSpace(v) == "" {
			return false, errors.New("Fatal: " + c[1] + " is not set")
		}
	}

	//now make sure all the devices are have required settings
	tno, _ := appConfig.Get("SETTINGS", "DEVICES")
	noOfDevices, _ := strconv.Atoi(tno)
	requiredFields = []setting{}

	for i := 0; i < noOfDevices; i++ {
		d := fmt.Sprintf("DEVICE%v", i)
		sCom := setting{d, "COMPORT"}
		sBaud := setting{d, "BAUDRATE"}
		sDevid := setting{d, "DEVID"}
		requiredFields = append(requiredFields, sCom)
		requiredFields = append(requiredFields, sBaud)
		requiredFields = append(requiredFields, sDevid)
	}

	for _, c := range requiredFields {
		v, ok := appConfig.Get(c[0], c[1])
		if !ok || strings.TrimSpace(v) == "" {
			return false, errors.New(strings.Join([]string{"Fatal:", c[0], c[1], "is not set"}, " "))
		}
	}

	return true, nil
}