예제 #1
0
파일: main.go 프로젝트: alevinval/foosbot
func process(ctx *foosbot.Context, msg *slack.MessageEvent) (response string) {
	in := []byte(msg.Text)
	r := bytes.NewReader(in)
	p := parsing.NewParser(r)

	token, err := p.ParseCommand()
	if err == parsing.ErrNotFoosbotCommand {
		return
	} else if err != nil {
		response = fmt.Sprintf("%s", err)
		return
	}
	fmt.Println(time.Now().String(), msg.Text)
	switch token.Type {
	case parsing.TokenCommandMatch:
		matchStatement, err := p.ParseMatch()
		if err != nil {
			response = err.Error()
			return
		}
		response = addMatchCommand(ctx, matchStatement)
	case parsing.TokenCommandLeaderboard:
		response = getLeaderboard(ctx)
	case parsing.TokenCommandStats:
		statStatement, err := p.ParseStats()
		if err != nil {
			response = err.Error()
			return
		}
		response = statsCommand(ctx, statStatement)

	}
	return
}
예제 #2
0
파일: main.go 프로젝트: alevinval/foosbot
func main() {
	foosbot.Verbose = false
	ctx := foosbot.NewContext()
	err := ctx.Load()
	if err != nil {
		panic(err)
	}
	q := foosbot.Query(ctx)

	var limit int

	app := cli.NewApp()
	app.Name = "foosbot"
	app.Usage = "The foosball match tracker"
	app.Commands = []cli.Command{
		{
			Name:    "match",
			Aliases: []string{"m"},
			Usage:   "Match history listing",
			Action: func(c *cli.Context) {
				matches := q.Matches().Limit(limit).Get()
				for _, match := range matches {
					fmt.Println(foosbot.Print(match))
				}
			},
			Flags: []cli.Flag{
				cli.IntFlag{
					Name:        "limit",
					Value:       10,
					Usage:       "Limits to the specified amount of results",
					Destination: &limit,
				},
			},
		},
		{
			Name: "run",
			Action: func(c *cli.Context) {
				args := c.Args()
				s := args.First()
				s += " "
				s += strings.Join(args.Tail(), " ")
				r := bytes.NewReader([]byte(s))
				parser := parsing.NewParser(r)
				out := process(ctx, parser)
				fmt.Println(out)
				ctx.Store()
			},
		},
	}
	app.Run(os.Args)
}
예제 #3
0
func newParser(input string) *parsing.Parser {
	r := bytes.NewReader([]byte(input))
	p := parsing.NewParser(r)
	return p
}