This repository has been archived by the owner on Jun 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
177 lines (147 loc) · 3.95 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package main
import (
"crypto/tls"
"encoding/json"
"flag"
"fmt"
"os"
"text/template"
"time"
"layeh.com/murmur-cli/MurmurRPC"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
var (
root = NewCommand()
void = &MurmurRPC.Void{}
ctx = context.Background()
client MurmurRPC.V1Client
)
const usage = `murmur-cli provides an interface to a grpc-enabled murmur server.
usage: murmur-cli [flags] [command... [arguments...]]
Flags:
--address="127.0.0.1:50051" address and port of murmur's grpc endpoint
(can also be set via $MURMUR_ADDRESS).
--timeout="10s" duration to wait for connection.
--template="" Go text/template template to use when outputting
data. By default, JSON objects are printed.
--insecure=false Disable TLS encryption.
--help Print command list.
`
const usageCommands = `
Commands:
acl get <server id> <channel id>
acl get-effective-permissions <server id> <session> <channel id>
ban get <server id>
channel query <server id>
channel get <server id> <channel id>
channel add <server id> <parent channel id> <name>
channel remove <server id> <channel id>
config get <server id>
config get-field <server id> <key>
config set-field <server id> <key> <value>
config get-default
contextaction add <server id> <context> <action> <text> <session>
Context is a comma separated list of the following:
Server
Channel
User
contextaction remove <server id> <action> [session]
contextaction events <server id> <action>
database query <server id> [filter]
database get <server id> <user id>
log query <server id> (<min> <max>)
meta uptime
meta version
meta events
server create
server query
server get <server id>
server start <server id>
server stop <server id>
server remove <server id>
server events <server id>
textmessage send <server id> [sender:<session>] [targets...] <text>
Valid targets:
user:<session>
channel:<id>
tree:<id>
textmessage filter <server id> <program> [args...]
tree query <server id>
user query <server id>
user get <server id> <session>
user kick <server id> <session> [reason]
`
var outputTemplate *template.Template
func main() {
// Flags
defaultAddress := "127.0.0.1:50051"
if envAddress := os.Getenv("MURMUR_ADDRESS"); envAddress != "" {
defaultAddress = envAddress
}
address := flag.String("address", defaultAddress, "")
timeout := flag.Duration("timeout", time.Second*10, "")
templateText := flag.String("template", "", "")
insecure := flag.Bool("insecure", false, "")
help := flag.Bool("help", false, "")
helpShort := flag.Bool("h", false, "")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, usage)
if *help || *helpShort {
fmt.Fprintf(os.Stderr, usageCommands)
}
}
flag.Parse()
if *help || *helpShort {
fmt.Fprintf(os.Stderr, usage)
if *help || *helpShort {
fmt.Fprintf(os.Stderr, usageCommands)
}
os.Exit(0)
}
if *templateText != "" {
var err error
outputTemplate, err = template.New("output").Parse(*templateText)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
// grpc connection
opts := []grpc.DialOption{
grpc.WithBlock(),
grpc.WithTimeout(*timeout),
}
if *insecure {
opts = append(opts, grpc.WithInsecure())
} else {
var tlsConfig tls.Config
opts = append(opts, grpc.WithTransportCredentials(credentials.NewTLS(&tlsConfig)))
}
conn, err := grpc.Dial(*address, opts...)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer conn.Close()
client = MurmurRPC.NewV1Client(conn)
defer func() {
if r := recover(); r != nil {
err, ok := r.(error)
if ok {
jsonErr := struct {
Error string `json:"error"`
}{
Error: err.Error(),
}
json.NewEncoder(os.Stderr).Encode(&jsonErr)
os.Exit(3)
}
}
}()
if root.Do() != nil {
flag.Usage()
os.Exit(1)
}
}