@@ -22,6 +22,7 @@ import (
22
22
"io/ioutil"
23
23
"net"
24
24
"os"
25
+ "strings"
25
26
"syscall"
26
27
27
28
"github.com/arduino/arduino-cli/cli/errorcodes"
@@ -111,8 +112,8 @@ func runDaemonCommand(cmd *cobra.Command, args []string) {
111
112
}()
112
113
}
113
114
114
- logrus . Infof ( "Starting daemon on TCP address 127.0.0.1:%s" , port )
115
- lis , err := net .Listen ("tcp" , fmt .Sprintf ("127.0.0.1 :%s" , port ))
115
+ ip := " 127.0.0.1"
116
+ lis , err := net .Listen ("tcp" , fmt .Sprintf ("%s :%s" , ip , port ))
116
117
if err != nil {
117
118
// Invalid port, such as "Foo"
118
119
var dnsError * net.DNSError
@@ -135,9 +136,40 @@ func runDaemonCommand(cmd *cobra.Command, args []string) {
135
136
feedback .Errorf (tr ("Failed to listen on TCP port: %[1]s. Unexpected error: %[2]v" ), port , err )
136
137
os .Exit (errorcodes .ErrGeneric )
137
138
}
138
- // This message will show up on the stdout of the daemon process so that gRPC clients know it is time to connect.
139
- logrus .Infof ("Daemon is now listening on 127.0.0.1:%s..." , port )
139
+
140
+ // We need to parse the port used only if the user let
141
+ // us choose it randomly, in all other cases we already
142
+ // know which is used.
143
+ if port == "0" {
144
+ address := lis .Addr ()
145
+ split := strings .Split (address .String (), ":" )
146
+
147
+ if len (split ) == 0 {
148
+ feedback .Error (tr ("Failed choosing port, address: %s" , address ))
149
+ }
150
+
151
+ port = split [len (split )- 1 ]
152
+ }
153
+
154
+ feedback .PrintResult (daemonResult {
155
+ IP : ip ,
156
+ Port : port ,
157
+ })
158
+
140
159
if err := s .Serve (lis ); err != nil {
141
160
logrus .Fatalf ("Failed to serve: %v" , err )
142
161
}
143
162
}
163
+
164
+ type daemonResult struct {
165
+ IP string
166
+ Port string
167
+ }
168
+
169
+ func (r daemonResult ) Data () interface {} {
170
+ return r
171
+ }
172
+
173
+ func (r daemonResult ) String () string {
174
+ return tr ("Daemon is now listening on %s:%s" , r .IP , r .Port )
175
+ }
0 commit comments