Unverified Commit 6b047bb4 authored by Massimiliano Pippi's avatar Massimiliano Pippi Committed by GitHub

Set the daemon TCP port from config file or command line (#513)

parent 4f3fec6e
...@@ -21,7 +21,6 @@ import ( ...@@ -21,7 +21,6 @@ import (
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"log"
"net" "net"
"net/http" "net/http"
"os" "os"
...@@ -31,24 +30,24 @@ import ( ...@@ -31,24 +30,24 @@ import (
"github.com/arduino/arduino-cli/commands/daemon" "github.com/arduino/arduino-cli/commands/daemon"
srv_commands "github.com/arduino/arduino-cli/rpc/commands" srv_commands "github.com/arduino/arduino-cli/rpc/commands"
srv_monitor "github.com/arduino/arduino-cli/rpc/monitor" srv_monitor "github.com/arduino/arduino-cli/rpc/monitor"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper"
"google.golang.org/grpc" "google.golang.org/grpc"
) )
const (
port = ":50051"
)
// NewCommand created a new `daemon` command // NewCommand created a new `daemon` command
func NewCommand() *cobra.Command { func NewCommand() *cobra.Command {
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "daemon", Use: "daemon",
Short: fmt.Sprintf("Run as a daemon on port %s", port), Short: fmt.Sprintf("Run as a daemon on port %s", viper.GetString("daemon.port")),
Long: "Running as a daemon the initialization of cores and libraries is done only once.", Long: "Running as a daemon the initialization of cores and libraries is done only once.",
Example: " " + os.Args[0] + " daemon", Example: " " + os.Args[0] + " daemon",
Args: cobra.NoArgs, Args: cobra.NoArgs,
Run: runDaemonCommand, Run: runDaemonCommand,
} }
cmd.PersistentFlags().String("port", "", "The TCP port the daemon will listen to")
viper.BindPFlag("daemon.port", cmd.PersistentFlags().Lookup("port"))
cmd.Flags().BoolVar(&daemonize, "daemonize", false, "Do not terminate daemon process if the parent process dies") cmd.Flags().BoolVar(&daemonize, "daemonize", false, "Do not terminate daemon process if the parent process dies")
return cmd return cmd
} }
...@@ -56,6 +55,7 @@ func NewCommand() *cobra.Command { ...@@ -56,6 +55,7 @@ func NewCommand() *cobra.Command {
var daemonize bool var daemonize bool
func runDaemonCommand(cmd *cobra.Command, args []string) { func runDaemonCommand(cmd *cobra.Command, args []string) {
port := viper.GetString("daemon.port")
s := grpc.NewServer() s := grpc.NewServer()
// register the commands service // register the commands service
...@@ -82,11 +82,12 @@ func runDaemonCommand(cmd *cobra.Command, args []string) { ...@@ -82,11 +82,12 @@ func runDaemonCommand(cmd *cobra.Command, args []string) {
}() }()
} }
lis, err := net.Listen("tcp", port) logrus.Infof("Starting daemon on TCP port %s", port)
lis, err := net.Listen("tcp", fmt.Sprintf(":%s", port))
if err != nil { if err != nil {
log.Fatalf("failed to listen: %v", err) logrus.Fatalf("failed to listen: %v", err)
} }
if err := s.Serve(lis); err != nil { if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err) logrus.Fatalf("failed to serve: %v", err)
} }
} }
...@@ -34,4 +34,7 @@ func setDefaults(dataDir, sketchBookDir string) { ...@@ -34,4 +34,7 @@ func setDefaults(dataDir, sketchBookDir string) {
viper.SetDefault("directories.Packages", filepath.Join(dataDir, "packages")) viper.SetDefault("directories.Packages", filepath.Join(dataDir, "packages"))
viper.SetDefault("directories.SketchBook", sketchBookDir) viper.SetDefault("directories.SketchBook", sketchBookDir)
viper.SetDefault("directories.Libraries", filepath.Join(sketchBookDir, "libraries")) viper.SetDefault("directories.Libraries", filepath.Join(sketchBookDir, "libraries"))
// daemon settings
viper.SetDefault("daemon.port", "50051")
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment