package parser import "bufio" var parsers map[string]Parser func init() { parsers = make(map[string]Parser, 0) addParser(&CPUParser{}) addParser(&MemoryParser{}) } func addParser(p Parser) { parsers[p.Key()] = p } func GetParser(name string) Parser { p, ok := parsers[name] if !ok { return nil } return p } type Parser interface { Key() string Parse(scanner *ParserScanner) (map[string]string, error) } type ParserScanner struct { scanner *bufio.Scanner boundary string text string ended bool eof bool } func (ps *ParserScanner) Clean() bool { if ps.eof { return false } if ps.ended { ps.ended = false return true } for ps.scanner.Scan() { line := ps.scanner.Text() if line == "--"+ps.boundary+"--" { ps.eof = true return false } if line == "--"+ps.boundary { break } } return true } func (ps *ParserScanner) EOF() bool { return ps.eof } func (ps *ParserScanner) Scan() bool { if ps.eof || ps.ended { return false } if !ps.scanner.Scan() { return false } line := ps.scanner.Text() if line == "--"+ps.boundary+"--" { ps.eof = true return false } if line == "--"+ps.boundary { ps.ended = true return false } ps.text = line return true } func (ps *ParserScanner) Text() string { if ps.eof || ps.ended { return "" } return ps.text } func (ps *ParserScanner) Err() error { return ps.scanner.Err() } func NewParserScanner(scanner *bufio.Scanner, boundary string) *ParserScanner { ps := &ParserScanner{ scanner: scanner, boundary: boundary, } return ps }