@@ -21,7 +21,6 @@ import (
2121 "fmt"
2222 "io"
2323 "io/ioutil"
24- "log"
2524 "net"
2625 "net/http"
2726 "os"
@@ -31,31 +30,32 @@ import (
3130 "github.com/arduino/arduino-cli/commands/daemon"
3231 srv_commands "github.com/arduino/arduino-cli/rpc/commands"
3332 srv_monitor "github.com/arduino/arduino-cli/rpc/monitor"
33+ "github.com/sirupsen/logrus"
3434 "github.com/spf13/cobra"
35+ "github.com/spf13/viper"
3536 "google.golang.org/grpc"
3637)
3738
38- const (
39- port = ":50051"
40- )
41-
4239// NewCommand created a new `daemon` command
4340func NewCommand () * cobra.Command {
4441 cmd := & cobra.Command {
4542 Use : "daemon" ,
46- Short : fmt .Sprintf ("Run as a daemon on port %s" , port ),
43+ Short : fmt .Sprintf ("Run as a daemon on port %s" , viper . GetString ( "daemon. port" ) ),
4744 Long : "Running as a daemon the initialization of cores and libraries is done only once." ,
4845 Example : " " + os .Args [0 ] + " daemon" ,
4946 Args : cobra .NoArgs ,
5047 Run : runDaemonCommand ,
5148 }
49+ cmd .PersistentFlags ().String ("port" , "" , "The TCP port the daemon will listen to" )
50+ viper .BindPFlag ("daemon.port" , cmd .PersistentFlags ().Lookup ("port" ))
5251 cmd .Flags ().BoolVar (& daemonize , "daemonize" , false , "Do not terminate daemon process if the parent process dies" )
5352 return cmd
5453}
5554
5655var daemonize bool
5756
5857func runDaemonCommand (cmd * cobra.Command , args []string ) {
58+ port := viper .GetString ("daemon.port" )
5959 s := grpc .NewServer ()
6060
6161 // register the commands service
@@ -82,11 +82,12 @@ func runDaemonCommand(cmd *cobra.Command, args []string) {
8282 }()
8383 }
8484
85- lis , err := net .Listen ("tcp" , port )
85+ logrus .Infof ("Starting daemon on TCP port %s" , port )
86+ lis , err := net .Listen ("tcp" , fmt .Sprintf (":%s" , port ))
8687 if err != nil {
87- log .Fatalf ("failed to listen: %v" , err )
88+ logrus .Fatalf ("failed to listen: %v" , err )
8889 }
8990 if err := s .Serve (lis ); err != nil {
90- log .Fatalf ("failed to serve: %v" , err )
91+ logrus .Fatalf ("failed to serve: %v" , err )
9192 }
9293}
0 commit comments