Commit 7abcd625 authored by Kamil Kisiel's avatar Kamil Kisiel

Cleaned up console code, fixed bugs with empty command

parent be0aab99
...@@ -44,17 +44,29 @@ type consoleConn struct { ...@@ -44,17 +44,29 @@ type consoleConn struct {
} }
func (c *consoleConn) serve() { func (c *consoleConn) serve() {
defer c.conn.Close()
buf := make([]byte, 1024) buf := make([]byte, 1024)
for { for {
c.conn.Write([]byte("console> "))
nbytes, err := c.conn.Read(buf) nbytes, err := c.conn.Read(buf)
if err != nil { if err != nil {
// Connection has likely closed // Connection has likely closed
return return
} }
var command string
var args []string
var result string var result string
switch parts := strings.Fields(string(buf[:nbytes])); parts[0] {
if parts := strings.Fields(string(buf[:nbytes])); len(parts) > 0 {
command = parts[0]
if len(parts) > 1 {
args = parts[1:]
}
}
switch command {
case "help": case "help":
result = "Commands: stats, counters, timers, gauges, delcounters, deltimers, delgauges, quit\n" result = "Commands: stats, counters, timers, gauges, delcounters, deltimers, delgauges, quit\n"
case "stats": case "stats":
...@@ -83,32 +95,31 @@ func (c *consoleConn) serve() { ...@@ -83,32 +95,31 @@ func (c *consoleConn) serve() {
c.server.Aggregator.Unlock() c.server.Aggregator.Unlock()
case "delcounters": case "delcounters":
c.server.Aggregator.Lock() c.server.Aggregator.Lock()
for _, k := range parts[1:] { for _, k := range args {
delete(c.server.Aggregator.counters, k) delete(c.server.Aggregator.counters, k)
} }
c.server.Aggregator.Unlock() c.server.Aggregator.Unlock()
case "deltimers": case "deltimers":
c.server.Aggregator.Lock() c.server.Aggregator.Lock()
for _, k := range parts[1:] { for _, k := range args {
delete(c.server.Aggregator.timers, k) delete(c.server.Aggregator.timers, k)
} }
c.server.Aggregator.Unlock() c.server.Aggregator.Unlock()
case "delgauges": case "delgauges":
c.server.Aggregator.Lock() c.server.Aggregator.Lock()
for _, k := range parts[1:] { for _, k := range args {
delete(c.server.Aggregator.gauges, k) delete(c.server.Aggregator.gauges, k)
} }
c.server.Aggregator.Unlock() c.server.Aggregator.Unlock()
case "quit": case "quit":
result = "quit" result = "goodbye\n"
default: default:
result = fmt.Sprintf("unknown command: %s\n", parts[0]) result = fmt.Sprintf("unknown command: %s\n", command)
} }
if result == "quit" { c.conn.Write([]byte(result))
if result == "goodbye\n" {
return return
} else {
c.conn.Write([]byte(result))
} }
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment