forked from ricbra/rabbitmq-cli-consumer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
114 lines (96 loc) · 2.58 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
112
113
114
package main
import (
"fmt"
"github.com/codegangsta/cli"
"github.com/oBlank/rabbitmq-cli-consumer/command"
"github.com/oBlank/rabbitmq-cli-consumer/config"
"github.com/oBlank/rabbitmq-cli-consumer/consumer"
"io"
"log"
"os"
"path/filepath"
)
var default_concurrency = 5
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.2.0"
app.Flags = []cli.Flag{
cli.IntFlag{
Name: "concurrency, n",
Usage: "Number of Concurrency, default is 5",
},
cli.StringFlag{
Name: "executable, e",
Usage: "Location of executable",
},
cli.StringFlag{
Name: "configuration, c",
Usage: "Location of configuration file",
},
cli.BoolFlag{
Name: "verbose, V",
Usage: "Enable verbose mode (logs to stdout and stderr)",
},
}
app.Action = func(c *cli.Context) {
concurrency := c.Int("concurrency")
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 concurrency > 0 {
cfg.Concurrency.Max = concurrency
}
if cfg.Concurrency.Max <= 0 {
cfg.Concurrency.Max = default_concurrency
}
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)
}
//todo
exec_path := c.String("executable")
if !filepath.IsAbs(exec_path) {
localtion, err := filepath.Abs(exec_path)
if err != nil {
logger.Fatalf("Failed executable path log: %s", err)
}
exec_path = localtion
}
fmt.Println(exec_path)
factory := command.Factory(exec_path)
client, err := consumer.New(cfg, factory, errLogger, infLogger)
if err != nil {
errLogger.Fatalf("Failed creating consumer: %s", err)
}
client.Consume()
}
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
}