-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.go
More file actions
115 lines (105 loc) · 2.6 KB
/
cli.go
File metadata and controls
115 lines (105 loc) · 2.6 KB
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
115
package main
import (
"fmt"
"os"
"strings"
"time"
"github.com/larskluge/babl/bablutils"
"github.com/urfave/cli"
)
func configureCli() (app *cli.App) {
app = cli.NewApp()
app.Usage = "Server for a Babl Module"
app.Version = Version
app.Action = defaultAction
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "module, m",
Usage: "Module to serve",
EnvVar: "BABL_MODULE",
},
cli.StringFlag{
Name: "cmd",
Usage: "Command to be executed",
Value: "cat",
EnvVar: "BABL_COMMAND",
},
cli.StringFlag{
Name: "cmd-timeout",
Usage: "Max execution duration for command to complete, before its killed",
Value: "30s",
EnvVar: "BABL_COMMAND_TIMEOUT",
},
cli.StringFlag{
Name: "restart-timeout",
Usage: "Timeout after each we gracefully restart babl-server (defaults to none)",
EnvVar: "BABL_RESTART_TIMEOUT",
Value: "0s",
},
cli.IntFlag{
Name: "port",
Usage: "Port for server to be started on",
EnvVar: "PORT",
Value: 4444,
},
cli.StringFlag{
Name: "kafka-brokers, kb",
Usage: "Comma separated list of kafka brokers",
EnvVar: "BABL_KAFKA_BROKERS",
},
cli.BoolFlag{
Name: "kafka-flush",
Usage: "For debugging only: error all incoming messages from Kafka to flush the topic",
EnvVar: "BABL_KAFKA_FLUSH",
},
cli.StringFlag{
Name: "storage",
Usage: "Endpoint for Babl storage",
EnvVar: "BABL_STORAGE",
Value: "babl.sh:4443",
},
cli.BoolFlag{
Name: "debug",
Usage: "Enable debug mode & verbose logging",
EnvVar: "BABL_DEBUG",
},
}
app.Commands = []cli.Command{
{
Name: "upgrade",
Usage: "Upgrades the server to the latest available version",
Action: func(_ *cli.Context) {
m := bablutils.NewUpgrade("babl-server", []string{})
m.Upgrade(Version)
},
},
}
return
}
func defaultAction(c *cli.Context) error {
ModuleName = c.String("module")
if ModuleName == "" {
cli.ShowAppHelp(c)
os.Exit(1)
} else {
address := fmt.Sprintf(":%d", c.Int("port"))
command = c.String("cmd")
var err error
if CommandTimeout, err = time.ParseDuration(c.String("cmd-timeout")); err != nil {
panic("cmd-timeout: Command timeout not a valid duration")
}
if RestartTimeout, err = time.ParseDuration(c.String("restart-timeout")); err != nil {
panic("restart-timeout: Restart timeout not a valid duration")
}
debug = c.GlobalBool("debug")
StorageEndpoint = c.String("storage")
kb := c.String("kafka-brokers")
brokers := []string{}
if kb != "" {
brokers = strings.Split(kb, ",")
}
KafkaFlush = c.Bool("kafka-flush")
run(address, brokers)
}
return nil
}