Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions cli/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"os"
Expand All @@ -31,31 +30,32 @@ import (
"github.com/arduino/arduino-cli/commands/daemon"
srv_commands "github.com/arduino/arduino-cli/rpc/commands"
srv_monitor "github.com/arduino/arduino-cli/rpc/monitor"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"google.golang.org/grpc"
)

const (
port = ":50051"
)

// NewCommand created a new `daemon` command
func NewCommand() *cobra.Command {
cmd := &cobra.Command{
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.",
Example: " " + os.Args[0] + " daemon",
Args: cobra.NoArgs,
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")
return cmd
}

var daemonize bool

func runDaemonCommand(cmd *cobra.Command, args []string) {
port := viper.GetString("daemon.port")
s := grpc.NewServer()

// register the commands service
Expand All @@ -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 {
log.Fatalf("failed to listen: %v", err)
logrus.Fatalf("failed to listen: %v", err)
}
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
logrus.Fatalf("failed to serve: %v", err)
}
}
3 changes: 3 additions & 0 deletions configuration/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,7 @@ func setDefaults(dataDir, sketchBookDir string) {
viper.SetDefault("directories.Packages", filepath.Join(dataDir, "packages"))
viper.SetDefault("directories.SketchBook", sketchBookDir)
viper.SetDefault("directories.Libraries", filepath.Join(sketchBookDir, "libraries"))

// daemon settings
viper.SetDefault("daemon.port", "50051")
}