nimsuggest: add an option to bind to a free port (#10328)

This commit is contained in:
alaviss 2019-01-17 03:06:32 +07:00 • committed by Andreas Rumpf
commit fc30cf02bc

View file

@ -35,6 +35,7 @@ Usage:
nimsuggest [options] projectfile.nim nimsuggest [options] projectfile.nim
Options: Options:
--autobind automatically binds into a free port
--port:PORT port, by default 6000 --port:PORT port, by default 6000
--address:HOST binds to that address, by default "" --address:HOST binds to that address, by default ""
--stdin read commands from stdin and write results to --stdin read commands from stdin and write results to
@ -50,6 +51,8 @@ Options:
The server then listens to the connection and takes line-based commands. The server then listens to the connection and takes line-based commands.
If --autobind is used, the binded port number will be printed to stdout.
In addition, all command line options of Nim that do not affect code generation In addition, all command line options of Nim that do not affect code generation
are supported. are supported.
""" """
@ -68,6 +71,7 @@ var
gEmitEof: bool # whether we write '!EOF!' dummy lines gEmitEof: bool # whether we write '!EOF!' dummy lines
gLogging = defined(logging) gLogging = defined(logging)
gRefresh: bool gRefresh: bool
gAutoBind = false
requests: Channel[string] requests: Channel[string]
results: Channel[Suggest] results: Channel[Suggest]
@ -306,9 +310,15 @@ proc replCmdline(x: ThreadParams) {.thread.} =
proc replTcp(x: ThreadParams) {.thread.} = proc replTcp(x: ThreadParams) {.thread.} =
var server = newSocket() var server = newSocket()
server.bindAddr(x.port, x.address) if gAutoBind:
let port = server.connectToNextFreePort(x.address)
server.listen()
echo port
stdout.flushFile()
else:
server.bindAddr(x.port, x.address)
server.listen()
var inp = "".TaintedString var inp = "".TaintedString
server.listen()
while true: while true:
var stdoutSocket = newSocket() var stdoutSocket = newSocket()
accept(server, stdoutSocket) accept(server, stdoutSocket)
@ -544,6 +554,9 @@ proc processCmdLine*(pass: TCmdLinePass, cmd: string; conf: ConfigRef) =
of "help", "h": of "help", "h":
stdout.writeline(Usage) stdout.writeline(Usage)
quit() quit()
of "autobind":
gMode = mtcp
gAutoBind = true
of "port": of "port":
gPort = parseInt(p.val).Port gPort = parseInt(p.val).Port
gMode = mtcp gMode = mtcp