Example #1
0
func Example_std() {
	hello := func(args []string) (string, error) {
		if len(args) == 0 {
			return "What's your name?\n", nil
		}
		return fmt.Sprintf("Hello, %s\n", strings.Join(args, " ")), nil
	}

	c := cmd.New(map[string]cmd.CmdFn{"hello": hello}, os.Stdin, os.Stdout)
	c.Loop()
}
Example #2
0
func Example_tcp() {
	hello := func(args []string) (string, error) {
		if len(args) == 0 {
			return "What's your name?\n", nil
		}
		return fmt.Sprintf("Hello, %s\n", strings.Join(args, " ")), nil
	}

	ln, err := net.Listen("tcp", ":6000")
	if err != nil {
		log.Fatal("could not open port:", err)
	}
	for {
		conn, err := ln.Accept()
		if err != nil {
			log.Println("couldn't accept console:", err)
			continue
		}
		c := cmd.New(map[string]cmd.CmdFn{"hello": hello}, conn, conn)
		go c.Loop()
	}
}
Example #3
0
// serve reads from the consoleConn and responds to incoming requests
func (c *consoleConn) serve() {
	defer c.conn.Close()

	commands := map[string]cmd.CmdFn{
		"help": func(args []string) (string, error) {
			return "Commands: stats, counters, timers, gauges, delcounters, deltimers, delgauges, quit\n", nil
		},
		"stats": func(args []string) (string, error) {
			c.server.Aggregator.Lock()
			defer c.server.Aggregator.Unlock()
			return fmt.Sprintf(
				"Invalid messages received: %d\n"+
					"Last message received: %s\n"+
					"Last flush to Graphite: %s\n"+
					"Last error from Graphite: %s\n",
				c.server.Aggregator.Stats.BadLines,
				c.server.Aggregator.Stats.LastMessage,
				c.server.Aggregator.Stats.LastFlush,
				c.server.Aggregator.Stats.LastFlushError), nil
		},
		"counters": func(args []string) (string, error) {
			c.server.Aggregator.Lock()
			defer c.server.Aggregator.Unlock()
			return fmt.Sprintln(c.server.Aggregator.Counters), nil
		},
		"timers": func(args []string) (string, error) {
			c.server.Aggregator.Lock()
			defer c.server.Aggregator.Unlock()
			return fmt.Sprintln(c.server.Aggregator.Timers), nil
		},
		"gauges": func(args []string) (string, error) {
			c.server.Aggregator.Lock()
			defer c.server.Aggregator.Unlock()
			return fmt.Sprintln(c.server.Aggregator.Gauges), nil
		},
		"delcounters": func(args []string) (string, error) {
			c.server.Aggregator.Lock()
			defer c.server.Aggregator.Unlock()
			i := 0
			for _, k := range args {
				delete(c.server.Aggregator.Counters, k)
				i++
			}
			return fmt.Sprintf("deleted %d counters\n", i), nil
		},
		"deltimers": func(args []string) (string, error) {
			c.server.Aggregator.Lock()
			defer c.server.Aggregator.Unlock()
			i := 0
			for _, k := range args {
				delete(c.server.Aggregator.Timers, k)
				i++
			}
			return fmt.Sprintf("deleted %d timers\n", i), nil
		},
		"delgauges": func(args []string) (string, error) {
			c.server.Aggregator.Lock()
			defer c.server.Aggregator.Unlock()
			i := 0
			for _, k := range args {
				delete(c.server.Aggregator.Gauges, k)
				i++
			}
			return fmt.Sprintf("deleted %d gauges\n", i), nil
		},
		"quit": func(args []string) (string, error) {
			return "goodbye\n", fmt.Errorf("client quit")
		},
	}

	console := cmd.New(commands, c.conn, c.conn)
	console.Prompt = "console> "
	console.Loop()
}