@@ -21,7 +21,6 @@ import (
21
21
"fmt"
22
22
"io"
23
23
"io/ioutil"
24
- "log"
25
24
"net"
26
25
"net/http"
27
26
"os"
@@ -31,31 +30,32 @@ import (
31
30
"github.com/arduino/arduino-cli/commands/daemon"
32
31
srv_commands "github.com/arduino/arduino-cli/rpc/commands"
33
32
srv_monitor "github.com/arduino/arduino-cli/rpc/monitor"
33
+ "github.com/sirupsen/logrus"
34
34
"github.com/spf13/cobra"
35
+ "github.com/spf13/viper"
35
36
"google.golang.org/grpc"
36
37
)
37
38
38
- const (
39
- port = ":50051"
40
- )
41
-
42
39
// NewCommand created a new `daemon` command
43
40
func NewCommand () * cobra.Command {
44
41
cmd := & cobra.Command {
45
42
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" ) ),
47
44
Long : "Running as a daemon the initialization of cores and libraries is done only once." ,
48
45
Example : " " + os .Args [0 ] + " daemon" ,
49
46
Args : cobra .NoArgs ,
50
47
Run : runDaemonCommand ,
51
48
}
49
+ cmd .PersistentFlags ().String ("port" , "" , "The TCP port the daemon will listen to" )
50
+ viper .BindPFlag ("daemon.port" , cmd .PersistentFlags ().Lookup ("port" ))
52
51
cmd .Flags ().BoolVar (& daemonize , "daemonize" , false , "Do not terminate daemon process if the parent process dies" )
53
52
return cmd
54
53
}
55
54
56
55
var daemonize bool
57
56
58
57
func runDaemonCommand (cmd * cobra.Command , args []string ) {
58
+ port := viper .GetString ("daemon.port" )
59
59
s := grpc .NewServer ()
60
60
61
61
// register the commands service
@@ -82,11 +82,12 @@ func runDaemonCommand(cmd *cobra.Command, args []string) {
82
82
}()
83
83
}
84
84
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 ))
86
87
if err != nil {
87
- log .Fatalf ("failed to listen: %v" , err )
88
+ logrus .Fatalf ("failed to listen: %v" , err )
88
89
}
89
90
if err := s .Serve (lis ); err != nil {
90
- log .Fatalf ("failed to serve: %v" , err )
91
+ logrus .Fatalf ("failed to serve: %v" , err )
91
92
}
92
93
}
0 commit comments