Esempio n. 1
0
func (s *decodStatus) readMap(r io.Reader) (err error) {
	s.data = make(map[string]string)
	s.env = make(map[string]map[string]string)
	var l, v, lastKey = 0, "", ""
	for r := bufio.NewReader(r); err == nil; l++ {
		v, err = r.ReadString(byte('\n'))
		if v == "" || v[0] == '#' || v[0] == '\n' {
			continue
		}
		v = clean(v)
		for ; v[len(v)-1] == '\\'; l++ {
			x, err := r.ReadString(byte('\n'))
			if err != nil && err != io.EOF {
				return err
			}
			v = v[:len(v)-1] + "\n" + strings.Trim(clean(x), " \t")
		}
		if v[0] == '\t' {
			s.data[lastKey] = s.data[lastKey] + " " + v[1:]
			continue
		}
		i := strings.Index(v, "=")
		if i < 1 {
			i = strings.Index(v, ":")
			if i < 1 {
				return fmt.Errorf("bad row %v, %s", l, v)
			}
		}
		lastKey = strings.Trim(strings.Replace(v[:i], "-", "", -1), " ")
		var env = ""
		if x := strings.Split(lastKey, "@"); len(x) > 1 {
			lastKey = x[0]
			env = x[1]
		}
		if env == "" {
			s.data[lastKey] = strings.Trim(v[i+1:], " ")
		} else {
			if s.env[env] == nil {
				s.env[env] = make(map[string]string)
			}
			s.env[env][lastKey] = strings.Trim(v[i+1:], " ")
		}
	}
	if err != io.EOF {
		return err
	}
	return nil
}