// Get extracts nodes from a EdgeOS/VyOS configuration structure

Get extracts nodes from a EdgeOS/VyOS configuration structure
// Get extracts nodes from a EdgeOS / VyOS configuration structure
func Get(cfg string) {
	type re struct {
		brkt, cmnt, desc, dsbl, leaf, misc, mlti, mpty, name, node *regexp.Regexp
	}
	rx := &re{}
	rx.brkt = regexp.MustCompile(`[}]`)
	rx.cmnt = regexp.MustCompile(`^([\/*]+).*([*\/]+)$`)
	rx.desc = regexp.MustCompile(`^(?:description)+\s"?([^"]+)?"?$`)
	rx.dsbl = regexp.MustCompile(`^(disabled)+\s([\S]+)$`)
	rx.leaf = regexp.MustCompile(`^(source)+\s([\S]+)\s[{]{1}$`)
	rx.misc = regexp.MustCompile(`^([\w-]+)$`)
	rx.mlti = regexp.MustCompile(`^((?:include|exclude)+)\s([\S]+)$`)
	rx.mpty = regexp.MustCompile(`^$`)
	rx.name = regexp.MustCompile(`^([\w-]+)\s([\S]+)$`)
	rx.node = regexp.MustCompile(`^([\w-]+)\s[{]{1}$`)
	cfgtree := make(map[string]*Node)
	var tnode string
	var leaf string
	for _, line := range strings.Split(cfg, "\n") {
		line = strings.TrimSpace(line)
		switch {
		case rx.mlti.MatchString(line):
			{
				IncExc := rx.mlti.FindStringSubmatch(line)
				switch IncExc[1] {
				case "exclude":
					cfgtree[tnode].Exclude = append(cfgtree[tnode].Exclude, IncExc[2])
				case "include":
					cfgtree[tnode].Include = append(cfgtree[tnode].Include, IncExc[2])
				}
			}
		case rx.node.MatchString(line):
			{
				node := rx.node.FindStringSubmatch(line)
				tnode = node[1]
				cfgtree[tnode] = &Node{}
				cfgtree[tnode].Source = make(map[string]*Src)
			}
		case rx.leaf.MatchString(line):
			src := rx.leaf.FindStringSubmatch(line)
			leaf = src[2]
			if src[1] == "source" {
				cfgtree[tnode].Source[leaf] = &Src{}
			}
		case rx.dsbl.MatchString(line):
			{
				disabled := rx.dsbl.FindStringSubmatch(line)
				cfgtree[tnode].Disable = ToBool(disabled[1])
			}
		case rx.name.MatchString(line):
			{
				name := rx.name.FindStringSubmatch(line)
				switch name[1] {
				case "prefix":
					cfgtree[tnode].Source[leaf].Prfx = name[2]
				case "url":
					cfgtree[tnode].Source[leaf].URL = name[2]
				case "description":
					cfgtree[tnode].Source[leaf].Desc = name[2]
				case "dns-redirect-ip":
					cfgtree[tnode].IP = name[2]
				}
			}
		case rx.desc.MatchString(line) || rx.cmnt.MatchString(line) || rx.misc.MatchString(line):
			break
		}
	}
	fmt.Println(cfgtree)
}
func Get ( cfg string ) { type re struct { brkt , cmnt , desc , dsbl , leaf , misc , mlti , mpty , name , node * regexp . Regexp } rx := & re { } rx . brkt = regexp . MustCompile ( `[}]` ) rx . cmnt = regexp . MustCompile ( `^([\/*]+).*([*\/]+)$` ) rx . desc = regexp . MustCompile ( `^(?:description)+\s"?([^"]+)?"?$` ) rx . dsbl = regexp . MustCompile ( `^(disabled)+\s([\S]+)$` ) rx . leaf = regexp . MustCompile ( `^(source)+\s([\S]+)\s[{]{1}$` ) rx . misc = regexp . MustCompile ( `^([\w-]+)$` ) rx . mlti = regexp . MustCompile ( `^((?:include|exclude)+)\s([\S]+)$` ) rx . mpty = regexp . MustCompile ( `^$` ) rx . name = regexp . MustCompile ( `^([\w-]+)\s([\S]+)$` ) rx . node = regexp . MustCompile ( `^([\w-]+)\s[{]{1}$` ) cfgtree := make ( map [ string ] * Node ) var tnode string var leaf string for _ , line := range strings . Split ( cfg , " \n " ) { line = strings . TrimSpace ( line ) switch { case rx . mlti . MatchString ( line ) : { IncExc := rx . mlti . FindStringSubmatch ( line ) switch IncExc [ 1 ] { case " " : cfgtree [ tnode ] . Exclude = append ( cfgtree [ tnode ] . Exclude , IncExc [ 2 ] ) case " " : cfgtree [ tnode ] . Include = append ( cfgtree [ tnode ] . Include , IncExc [ 2 ] ) } } case rx . node . MatchString ( line ) : { node := rx . node . FindStringSubmatch ( line ) tnode = node [ 1 ] cfgtree [ tnode ] = & Node { } cfgtree [ tnode ] . Source = make ( map [ string ] * Src ) } case rx . leaf . MatchString ( line ) : src := rx . leaf . FindStringSubmatch ( line ) leaf = src [ 2 ] if src [ 1 ] == " " { cfgtree [ tnode ] . Source [ leaf ] = & Src { } } case rx . dsbl . MatchString ( line ) : { disabled := rx . dsbl . FindStringSubmatch ( line ) cfgtree [ tnode ] . Disable = ToBool ( disabled [ 1 ] ) } case rx . name . MatchString ( line ) : { name := rx . name . FindStringSubmatch ( line ) switch name [ 1 ] { case " " : cfgtree [ tnode ] . Source [ leaf ] . Prfx = name [ 2 ] case " " : cfgtree [ tnode ] . Source [ leaf ] . URL = name [ 2 ] case " " : cfgtree [ tnode ] . Source [ leaf ] . Desc = name [ 2 ] case " " : cfgtree [ tnode ] . IP = name [ 2 ] } } case rx . desc . MatchString ( line ) || rx . cmnt . MatchString ( line ) || rx . misc . MatchString ( line ) : break } } fmt . Println ( cfgtree ) }
Get extracts nodes from a EdgeOS/VyOS configuration structure
Get extracts nodes from a EdgeOS / VyOS configuration structure
// fmt.Printf("%s\n", line)
