示例#1
0
// ParseCherryFile parses a file at @filepath and returns a *config.CherryRooms or a *CherryFileError.
func ParseCherryFile(filepath string) (*config.CherryRooms, *CherryFileError) {
	var cherryRooms *config.CherryRooms
	var cherryFileData []byte
	var data string
	var err *CherryFileError
	var line int
	cherryFileData, ioErr := ioutil.ReadFile(filepath)
	if ioErr != nil {
		return nil, NewCherryFileError("(no file)", -1, fmt.Sprintf("unable to read from \"%s\" [more details: %s].", filepath, ioErr.Error()))
	}
	data, _, line, err = GetDataFromSection("cherry.root", string(cherryFileData), 1, filepath)
	if err != nil {
		return nil, err
	}
	var set []string
	cherryRooms = config.NewCherryRooms()
	set, line, data = GetNextSetFromData(data, line, "=")
	for len(set) == 2 {
		switch set[0] {
		case "servername", "certificate", "private-key":
			if set[1][0] != '"' || set[1][len(set[1])-1] != '"' {
				return nil, NewCherryFileError(filepath, line, fmt.Sprintf("invalid string."))
			}
			data := set[1][1 : len(set[1])-1]

			if set[0] == "certificate" || set[0] == "private-key" {
				if _, err := os.Stat(data); os.IsNotExist(err) {
					return nil, NewCherryFileError(filepath, line, fmt.Sprintf("\"%s\" must receive an accessible file path.", set[0]))
				}
			}

			if set[0] == "servername" {
				cherryRooms.SetServername(data)
			} else if set[0] == "certificate" {
				cherryRooms.SetCertificatePath(data)
			} else if set[0] == "private-key" {
				cherryRooms.SetPrivateKeyPath(data)
			}
			break

		default:
			return nil, NewCherryFileError(filepath, line, fmt.Sprintf("unknown config set \"%s\".", set[0]))
		}
		set, line, data = GetNextSetFromData(data, line, "=")
	}
	if cherryRooms.GetServername() == "localhost" {
		fmt.Println("WARN: cherry.root.servername is equals to \"localhost\". Things will not work outside this node.")
	}
	data, _, line, err = GetDataFromSection("cherry.rooms", string(cherryFileData), 1, filepath)
	if err != nil {
		return nil, err
	}
	//  INFO(Santiago): Adding all scanned rooms from the first cherry.rooms section found
	//                  [cherry branches were scanned too at this point].
	set, line, data = GetNextSetFromData(data, line, ":")
	for len(set) == 2 {
		if cherryRooms.HasRoom(set[0]) {
			return nil, NewCherryFileError(filepath, line, fmt.Sprintf("room \"%s\" redeclared.", set[0]))
		}
		var value int64
		var convErr error
		value, convErr = strconv.ParseInt(set[1], 10, 16)
		if convErr != nil {
			return nil, NewCherryFileError(filepath, line, fmt.Sprintf("invalid port value \"%s\" [more details: %s].", set[1], convErr))
		}
		var port int16
		port = int16(value)
		if cherryRooms.PortBusyByAnotherRoom(port) {
			return nil, NewCherryFileError(filepath, line, fmt.Sprintf("the port \"%s\" is already busy by another room.", set[1]))
		}

		cherryRooms.AddRoom(set[0], port)

		errRoomConfig := GetRoomTemplates(set[0], cherryRooms, string(cherryFileData), filepath)
		if errRoomConfig != nil {
			return nil, errRoomConfig
		}

		errRoomConfig = GetRoomActions(set[0], cherryRooms, string(cherryFileData), filepath)
		if errRoomConfig != nil {
			return nil, errRoomConfig
		}

		//  INFO(Santiago): until now these two following sections are non-mandatory.

		_ = GetRoomImages(set[0], cherryRooms, string(cherryFileData), filepath)

		//_ = GetRoomSounds(set[0], cherryRooms, string(cherryFileData), filepath)

		errRoomConfig = GetRoomMisc(set[0], cherryRooms, string(cherryFileData), filepath)
		if errRoomConfig != nil {
			return nil, errRoomConfig
		}

		//  INFO(Santiago): Let's transfer the next room from file to the memory.
		set, line, data = GetNextSetFromData(data, line, ":")
	}
	return cherryRooms, nil
}