-
Notifications
You must be signed in to change notification settings - Fork 3
feat: add daemon command and gRPC list support
#46
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
base: main
Are you sure you want to change the base?
Changes from all commits
a295189
0014075
3c5a404
8e2e127
60f327a
d3b72e1
ac721be
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| version: v2 | ||
| plugins: | ||
| # Use protoc-gen-go | ||
| - remote: buf.build/protocolbuffers/go:v1.34.2 | ||
| out: ./rpc | ||
| opt: | ||
| - paths=source_relative | ||
| # Use of protoc-gen-go-grpc | ||
| - remote: buf.build/grpc/go:v1.5.1 | ||
| out: ./rpc | ||
| opt: | ||
| - paths=source_relative | ||
| inputs: | ||
| - directory: ./rpc |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| # Generated by buf. DO NOT EDIT. | ||
| version: v2 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| version: v2 | ||
| breaking: | ||
| use: | ||
| - FILE | ||
| lint: | ||
| use: | ||
| - STANDARD | ||
| - COMMENT_ENUM | ||
| - COMMENT_ENUM_VALUE | ||
| - COMMENT_FIELD | ||
| - COMMENT_RPC | ||
| - COMMENT_SERVICE | ||
| modules: | ||
| - path: rpc | ||
| name: buf.build/arduino/arduino-flasher-cli |
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,125 @@ | ||||||||||||||||
| // This file is part of arduino-flasher-cli. | ||||||||||||||||
| // | ||||||||||||||||
| // Copyright 2025 ARDUINO SA (http://www.arduino.cc/) | ||||||||||||||||
| // | ||||||||||||||||
| // This software is released under the GNU General Public License version 3, | ||||||||||||||||
| // which covers the main part of arduino-flasher-cli. | ||||||||||||||||
| // The terms of this license can be found at: | ||||||||||||||||
| // https://www.gnu.org/licenses/gpl-3.0.en.html | ||||||||||||||||
| // | ||||||||||||||||
| // You can be released from the requirements of the above licenses by purchasing | ||||||||||||||||
| // a commercial license. Buying such a license is mandatory if you want to | ||||||||||||||||
| // modify or otherwise use the software for commercial activities involving the | ||||||||||||||||
| // Arduino software without disclosing the source code of your own applications. | ||||||||||||||||
| // To purchase a commercial license, send an email to license@arduino.cc. | ||||||||||||||||
|
|
||||||||||||||||
| package daemon | ||||||||||||||||
|
|
||||||||||||||||
| import ( | ||||||||||||||||
| "encoding/json" | ||||||||||||||||
| "errors" | ||||||||||||||||
| "fmt" | ||||||||||||||||
| "net" | ||||||||||||||||
| "os" | ||||||||||||||||
| "strings" | ||||||||||||||||
| "syscall" | ||||||||||||||||
|
|
||||||||||||||||
| "github.com/spf13/cobra" | ||||||||||||||||
| "google.golang.org/grpc" | ||||||||||||||||
|
|
||||||||||||||||
| "github.com/arduino/arduino-flasher-cli/cmd/feedback" | ||||||||||||||||
| "github.com/arduino/arduino-flasher-cli/cmd/i18n" | ||||||||||||||||
|
|
||||||||||||||||
| flasher "github.com/arduino/arduino-flasher-cli/rpc/cc/arduino/flasher/v1" | ||||||||||||||||
|
Comment on lines
+30
to
+33
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||
| ) | ||||||||||||||||
|
|
||||||||||||||||
| func NewDaemonCommand(srv flasher.FlasherServer) *cobra.Command { | ||||||||||||||||
| var daemonPort string | ||||||||||||||||
| var maxGRPCRecvMsgSize int | ||||||||||||||||
| daemonCommand := &cobra.Command{ | ||||||||||||||||
| Use: "daemon", | ||||||||||||||||
| Short: i18n.Tr("Run the Arduino Flasher CLI as a gRPC daemon."), | ||||||||||||||||
| Example: " " + os.Args[0] + " daemon", | ||||||||||||||||
| Args: cobra.NoArgs, | ||||||||||||||||
| Run: func(cmd *cobra.Command, args []string) { | ||||||||||||||||
| runDaemonCommand(srv, daemonPort, maxGRPCRecvMsgSize) | ||||||||||||||||
| }, | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| daemonCommand.Flags().StringVar(&daemonPort, | ||||||||||||||||
| "port", "50052", | ||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would use a random port by default
Suggested change
|
||||||||||||||||
| i18n.Tr("The TCP port the daemon will listen to")) | ||||||||||||||||
|
|
||||||||||||||||
| daemonCommand.Flags().IntVar(&maxGRPCRecvMsgSize, | ||||||||||||||||
| "max-grpc-recv-message-size", 16*1024*1024, | ||||||||||||||||
| i18n.Tr("Sets the maximum message size in bytes the daemon can receive")) | ||||||||||||||||
|
|
||||||||||||||||
| return daemonCommand | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| func runDaemonCommand(srv flasher.FlasherServer, daemonPort string, maxGRPCRecvMsgSize int) { | ||||||||||||||||
| gRPCOptions := []grpc.ServerOption{} | ||||||||||||||||
| gRPCOptions = append(gRPCOptions, grpc.MaxRecvMsgSize(maxGRPCRecvMsgSize)) | ||||||||||||||||
| s := grpc.NewServer(gRPCOptions...) | ||||||||||||||||
|
|
||||||||||||||||
| // register the commands service | ||||||||||||||||
| flasher.RegisterFlasherServer(s, srv) | ||||||||||||||||
|
|
||||||||||||||||
| daemonIP := "127.0.0.1" | ||||||||||||||||
| lis, err := net.Listen("tcp", fmt.Sprintf("%s:%s", daemonIP, daemonPort)) | ||||||||||||||||
| if err != nil { | ||||||||||||||||
| // Invalid port, such as "Foo" | ||||||||||||||||
| var dnsError *net.DNSError | ||||||||||||||||
| if errors.As(err, &dnsError) { | ||||||||||||||||
| feedback.Fatal(i18n.Tr("Failed to listen on TCP port: %[1]s. %[2]s is unknown name.", daemonPort, dnsError.Name), feedback.ErrBadTCPPortArgument) | ||||||||||||||||
| } | ||||||||||||||||
| // Invalid port number, such as -1 | ||||||||||||||||
| var addrError *net.AddrError | ||||||||||||||||
| if errors.As(err, &addrError) { | ||||||||||||||||
| feedback.Fatal(i18n.Tr("Failed to listen on TCP port: %[1]s. %[2]s is an invalid port.", daemonPort, addrError.Addr), feedback.ErrBadTCPPortArgument) | ||||||||||||||||
| } | ||||||||||||||||
| // Port is already in use | ||||||||||||||||
| var syscallErr *os.SyscallError | ||||||||||||||||
| if errors.As(err, &syscallErr) && errors.Is(syscallErr.Err, syscall.EADDRINUSE) { | ||||||||||||||||
| feedback.Fatal(i18n.Tr("Failed to listen on TCP port: %s. Address already in use.", daemonPort), feedback.ErrFailedToListenToTCPPort) | ||||||||||||||||
| } | ||||||||||||||||
| feedback.Fatal(i18n.Tr("Failed to listen on TCP port: %[1]s. Unexpected error: %[2]v", daemonPort, err), feedback.ErrFailedToListenToTCPPort) | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| // We need to retrieve the port used only if the user did not specify it | ||||||||||||||||
| // and let the OS choose it randomly, in all other cases we already know | ||||||||||||||||
| // which port is used. | ||||||||||||||||
| if daemonPort == "0" { | ||||||||||||||||
| address := lis.Addr() | ||||||||||||||||
| split := strings.Split(address.String(), ":") | ||||||||||||||||
|
|
||||||||||||||||
| if len(split) <= 1 { | ||||||||||||||||
| feedback.Fatal(i18n.Tr("Invalid TCP address: port is missing"), feedback.ErrBadTCPPortArgument) | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| daemonPort = split[1] | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| feedback.PrintResult(daemonResult{ | ||||||||||||||||
| IP: daemonIP, | ||||||||||||||||
| Port: daemonPort, | ||||||||||||||||
| }) | ||||||||||||||||
|
|
||||||||||||||||
| if err := s.Serve(lis); err != nil { | ||||||||||||||||
| feedback.Fatal(fmt.Sprintf("Failed to serve: %v", err), feedback.ErrFailedToListenToTCPPort) | ||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| type daemonResult struct { | ||||||||||||||||
| IP string | ||||||||||||||||
| Port string | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| func (r daemonResult) Data() interface{} { | ||||||||||||||||
| return r | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| func (r daemonResult) String() string { | ||||||||||||||||
| j, _ := json.Marshal(r) | ||||||||||||||||
| return fmt.Sprintln(i18n.Tr("Daemon is now listening on %s:%s", r.IP, r.Port)) + fmt.Sprintln(string(j)) | ||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are you including the JSON?
Suggested change
|
||||||||||||||||
| } | ||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -23,20 +23,24 @@ import ( | |||||
| "github.com/spf13/cobra" | ||||||
| "go.bug.st/cleanup" | ||||||
|
|
||||||
| "github.com/arduino/arduino-flasher-cli/cmd/arduino-flasher-cli/daemon" | ||||||
| "github.com/arduino/arduino-flasher-cli/cmd/arduino-flasher-cli/download" | ||||||
| "github.com/arduino/arduino-flasher-cli/cmd/arduino-flasher-cli/drivers" | ||||||
| "github.com/arduino/arduino-flasher-cli/cmd/arduino-flasher-cli/flash" | ||||||
| "github.com/arduino/arduino-flasher-cli/cmd/arduino-flasher-cli/list" | ||||||
| "github.com/arduino/arduino-flasher-cli/cmd/arduino-flasher-cli/version" | ||||||
| "github.com/arduino/arduino-flasher-cli/cmd/feedback" | ||||||
| "github.com/arduino/arduino-flasher-cli/cmd/i18n" | ||||||
| "github.com/arduino/arduino-flasher-cli/service" | ||||||
| ) | ||||||
|
|
||||||
| // Version will be set a build time with -ldflags | ||||||
| var Version string = "0.0.0-dev" | ||||||
| var format string | ||||||
|
|
||||||
| func main() { | ||||||
| srv := service.NewFlasherServer() | ||||||
|
|
||||||
|
Comment on lines
+42
to
+43
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| rootCmd := &cobra.Command{ | ||||||
| Use: "arduino-flasher-cli", | ||||||
| Short: "A CLI to update your Arduino UNO Q board, by downloading and flashing the latest Arduino Linux image", | ||||||
|
|
@@ -61,6 +65,7 @@ func main() { | |||||
| list.NewListCmd(), | ||||||
| download.NewDownloadCmd(), | ||||||
| version.NewVersionCmd(Version), | ||||||
| daemon.NewDaemonCommand(srv), | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| ) | ||||||
|
|
||||||
| ctx := context.Background() | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would install buf as a go tool. You can run
go get --tool github.com/bufbuild/bufto add the latest buf version in the go.mod, and then here you can write