Skip to content

Commit 5462de9

Browse files
committed
Add -o flag to auto setting client output to json or resp
It's now possible to set the default client output using the startup flag `-o json` or `-o resp`. see #779
1 parent 27bd77a commit 5462de9

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

cmd/tile38-server/main.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ Basic Options:
7878
-d path : data directory (default: data)
7979
-s socket : listen on unix socket file
8080
-l encoding : set log encoding to json or text (default: text)
81+
-o output : auto set client output to json or resp (default: resp)
8182
-q : no logging. totally silent output
8283
-v : enable verbose logging
8384
-vv : enable very verbose logging
@@ -168,6 +169,9 @@ Developer Options:
168169

169170
// QueueFileName allows for custom queue.db file path
170171
queueFileName = ""
172+
173+
// ClientOutput for auto assigning the output for client.
174+
clientOutput = ""
171175
)
172176

173177
// parse non standard args.
@@ -232,6 +236,17 @@ Developer Options:
232236
os.Exit(1)
233237
}
234238
queueFileName = os.Args[i]
239+
case "-o":
240+
i++
241+
if i < len(os.Args) {
242+
switch strings.ToLower(os.Args[i]) {
243+
case "resp", "json":
244+
clientOutput = strings.ToLower(os.Args[i])
245+
continue
246+
}
247+
}
248+
fmt.Fprintf(os.Stderr, "output must be 'resp' or 'json'\n")
249+
os.Exit(1)
235250
case "--http-transport", "-http-transport":
236251
i++
237252
if i < len(os.Args) {
@@ -490,6 +505,7 @@ Developer Options:
490505
QueueFileName: queueFileName,
491506
Shutdown: shutdown,
492507
Spinlock: spinlock,
508+
ClientOutput: clientOutput,
493509
}
494510
if err := server.Serve(opts); err != nil {
495511
log.Fatal(err)

internal/server/server.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ type Options struct {
244244
UseHTTP bool
245245
MetricsAddr string
246246
UnixSocketPath string // path for unix socket
247+
ClientOutput string // "" or "resp" or "json"
247248

248249
// DevMode puts application in to dev mode
249250
DevMode bool
@@ -583,6 +584,14 @@ func (s *Server) netServe() error {
583584
log.Debug("Client connection closed")
584585
}()
585586

587+
var defaultOutputType Type
588+
switch s.opts.ClientOutput {
589+
case "resp":
590+
defaultOutputType = RESP
591+
case "json":
592+
defaultOutputType = JSON
593+
}
594+
586595
log.Infof("Ready to accept connections at %s", ln.Addr())
587596
var clientID int64
588597
for {
@@ -680,6 +689,8 @@ func (s *Server) netServe() error {
680689
if msg != nil && msg.Command() != "" {
681690
if client.outputType != Null {
682691
msg.OutputType = client.outputType
692+
} else if defaultOutputType != Null {
693+
msg.OutputType = defaultOutputType
683694
}
684695
if msg.Command() == "quit" {
685696
if msg.OutputType == RESP {

0 commit comments

Comments
 (0)