-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathmain.go
111 lines (93 loc) · 2.8 KB
/
main.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"io"
"log"
"os"
"github.com/codegangsta/cli"
"github.com/ricbra/rabbitmq-cli-consumer/command"
"github.com/ricbra/rabbitmq-cli-consumer/config"
"github.com/ricbra/rabbitmq-cli-consumer/consumer"
)
func main() {
app := cli.NewApp()
app.Name = "rabbitmq-cli-consumer"
app.Usage = "Consume RabbitMQ easily to any cli program"
app.Author = "Richard van den Brand"
app.Email = "[email protected]"
app.Version = "1.4.2"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "executable, e",
Usage: "Location of executable",
},
cli.StringFlag{
Name: "configuration, c",
Usage: "Location of configuration file",
},
cli.BoolFlag{
Name: "output, o",
Usage: "Enable logging of output from executable",
},
cli.BoolFlag{
Name: "verbose, V",
Usage: "Enable verbose mode (logs to stdout and stderr)",
},
cli.BoolFlag{
Name: "include, i",
Usage: "Include metadata. Passes message as JSON data including headers, properties and message body.",
},
cli.BoolFlag{
Name: "strict-exit-code",
Usage: "Strict exit code processing will rise a fatal error if exit code is different from allowed onces.",
},
cli.StringFlag{
Name: "queue-name, q",
Usage: "Optional queue name to which can be passed in, without needing to define it in config, if set will override config queue name",
},
}
app.Action = func(c *cli.Context) {
if c.String("configuration") == "" && c.String("executable") == "" {
cli.ShowAppHelp(c)
os.Exit(1)
}
verbose := c.Bool("verbose")
logger := log.New(os.Stderr, "", log.Ldate|log.Ltime)
cfg, err := config.LoadAndParse(c.String("configuration"))
if err != nil {
logger.Fatalf("Failed parsing configuration: %s\n", err)
}
errLogger, err := createLogger(cfg.Logs.Error, verbose, os.Stderr)
if err != nil {
logger.Fatalf("Failed creating error log: %s", err)
}
infLogger, err := createLogger(cfg.Logs.Info, verbose, os.Stdout)
if err != nil {
logger.Fatalf("Failed creating info log: %s", err)
}
if c.String("queue-name") != "" {
cfg.RabbitMq.Queue = c.String("queue-name")
}
factory := command.Factory(c.String("executable"))
client, err := consumer.New(cfg, factory, errLogger, infLogger)
if err != nil {
errLogger.Fatalf("Failed creating consumer: %s", err)
}
client.IncludeMetadata = c.Bool("include")
client.StrictExitCode = c.Bool("strict-exit-code")
client.Consume(c.Bool("output"))
}
app.Run(os.Args)
}
func createLogger(filename string, verbose bool, out io.Writer) (*log.Logger, error) {
file, err := os.OpenFile(filename, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0660)
if err != nil {
return nil, err
}
var writers = []io.Writer{
file,
}
if verbose {
writers = append(writers, out)
}
return log.New(io.MultiWriter(writers...), "", log.Ldate|log.Ltime), nil
}