Skip to content

Commit 1117d34

Browse files
silvanocerzaumbynos
authored andcommitted
Add json output to daemon command (#1585)
1 parent 8923130 commit 1117d34

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

Diff for: cli/daemon/daemon.go

+36-4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"io/ioutil"
2323
"net"
2424
"os"
25+
"strings"
2526
"syscall"
2627

2728
"github.com/arduino/arduino-cli/cli/errorcodes"
@@ -111,8 +112,8 @@ func runDaemonCommand(cmd *cobra.Command, args []string) {
111112
}()
112113
}
113114

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))
116117
if err != nil {
117118
// Invalid port, such as "Foo"
118119
var dnsError *net.DNSError
@@ -135,9 +136,40 @@ func runDaemonCommand(cmd *cobra.Command, args []string) {
135136
feedback.Errorf(tr("Failed to listen on TCP port: %[1]s. Unexpected error: %[2]v"), port, err)
136137
os.Exit(errorcodes.ErrGeneric)
137138
}
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+
140159
if err := s.Serve(lis); err != nil {
141160
logrus.Fatalf("Failed to serve: %v", err)
142161
}
143162
}
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

Comments
 (0)