/
bot.go
58 lines (49 loc) · 1.37 KB
/
bot.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package app
import (
"flag"
"github.com/bivas/rivi/pkg/runner"
"github.com/bivas/rivi/pkg/server"
"github.com/bivas/rivi/pkg/util/log"
"github.com/mitchellh/cli"
)
type botCommand struct {
port int
uri string
}
func (s *botCommand) Help() string {
return `
Usage: rivi bot [options] CONFIGURATION_FILE(S)...
Starts rivi in bot mode and listen to incoming webhooks
Options:
-port=8080 Listen on port (default: 8080)
-uri=/ URI path (default: "/")
`
}
func (s *botCommand) Run(args []string) int {
flagSet := flag.NewFlagSet("bot", flag.ContinueOnError)
flagSet.IntVar(&s.port, "port", 8080, "Runner listening port")
flagSet.StringVar(&s.uri, "uri", "/", "Runner URI path")
if err := flagSet.Parse(args); err != nil {
log.ErrorWith(log.MetaFields{log.E(err)}, "command line arguments")
return cli.RunResultHelp
}
if len(flagSet.Args()) == 0 {
log.Error("missing configuration file")
return cli.RunResultHelp
}
run, err := runner.New(flagSet.Args()...)
if err != nil {
log.ErrorWith(log.MetaFields{log.E(err)}, "Unable to start runner handler")
return 1
}
log.Info("Rivi is ready")
srv := server.BotServer{Port: s.port, Uri: s.uri, Runner: run}
if err := srv.Run(); err != nil {
log.ErrorWith(log.MetaFields{log.E(err)}, "Runner exited with error")
return -1
}
return 0
}
func (s *botCommand) Synopsis() string {
return "start rivi server"
}