container_network/crawler/ssh/parser/parser.go

112 lines
1.5 KiB
Go
Raw Permalink Normal View History

2018-04-19 15:46:38 +00:00
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) {
2018-06-21 10:49:14 +00:00
parsers[p.Key()] = p
2018-04-19 15:46:38 +00:00
}
func GetParser(name string) Parser {
p, ok := parsers[name]
if !ok {
return nil
}
return p
}
type Parser interface {
2018-06-21 10:49:14 +00:00
Key() string
2018-04-19 15:46:38 +00:00
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
}