diff --git a/Taskfile.yml b/Taskfile.yml index 40dd5b9..97c57c2 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -126,3 +126,19 @@ tasks: general:prepare-deps: desc: Prepare project dependencies for license check # No preparation is needed for Go module-based projects. + + protoc:compile: + desc: Compile protobuf definitions + cmds: + - buf dep update + - buf generate + + protoc:check: + desc: Perform linting of the protobuf definitions + cmds: + - buf lint + + protoc:format: + desc: Perform formatting of the protobuf definitions + cmds: + - buf format --write diff --git a/buf.gen.yaml b/buf.gen.yaml new file mode 100644 index 0000000..b00e81d --- /dev/null +++ b/buf.gen.yaml @@ -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 diff --git a/buf.lock b/buf.lock new file mode 100644 index 0000000..4f98143 --- /dev/null +++ b/buf.lock @@ -0,0 +1,2 @@ +# Generated by buf. DO NOT EDIT. +version: v2 diff --git a/buf.yaml b/buf.yaml new file mode 100644 index 0000000..55b5b39 --- /dev/null +++ b/buf.yaml @@ -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 diff --git a/cmd/arduino-flasher-cli/daemon/daemon.go b/cmd/arduino-flasher-cli/daemon/daemon.go new file mode 100644 index 0000000..1fef1ec --- /dev/null +++ b/cmd/arduino-flasher-cli/daemon/daemon.go @@ -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" +) + +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", + 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)) +} diff --git a/cmd/arduino-flasher-cli/main.go b/cmd/arduino-flasher-cli/main.go index d783968..5083ce5 100644 --- a/cmd/arduino-flasher-cli/main.go +++ b/cmd/arduino-flasher-cli/main.go @@ -23,6 +23,7 @@ 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" @@ -30,6 +31,7 @@ import ( "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 @@ -37,6 +39,8 @@ var Version string = "0.0.0-dev" var format string func main() { + srv := service.NewFlasherServer() + 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), ) ctx := context.Background() diff --git a/go.mod b/go.mod index cc0dada..8f03af7 100644 --- a/go.mod +++ b/go.mod @@ -16,6 +16,8 @@ require ( go.bug.st/cleanup v1.0.0 go.bug.st/f v0.4.0 go.bug.st/relaxed-semver v0.15.0 + google.golang.org/grpc v1.77.0 + google.golang.org/protobuf v1.36.10 ) require ( @@ -71,12 +73,13 @@ require ( github.com/xanzy/ssh-agent v0.3.3 // indirect github.com/yusufpapurcu/wmi v1.2.4 // indirect github.com/zeebo/xxh3 v1.0.2 // indirect - golang.org/x/crypto v0.37.0 // indirect - golang.org/x/net v0.39.0 // indirect - golang.org/x/sync v0.16.0 // indirect + golang.org/x/crypto v0.43.0 // indirect + golang.org/x/net v0.46.1-0.20251013234738-63d1a5100f82 // indirect + golang.org/x/sync v0.17.0 // indirect golang.org/x/sys v0.37.0 // indirect - golang.org/x/term v0.35.0 // indirect - golang.org/x/text v0.24.0 // indirect + golang.org/x/term v0.36.0 // indirect + golang.org/x/text v0.30.0 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20251022142026-3a174f9686a8 // indirect gopkg.in/warnings.v0 v0.1.2 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect mvdan.cc/sh/v3 v3.12.0 // indirect diff --git a/go.sum b/go.sum index 50ec8ea..b35710a 100644 --- a/go.sum +++ b/go.sum @@ -65,6 +65,10 @@ github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399 h1:eMj github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399/go.mod h1:1OCfN199q1Jm3HZlxleg+Dw/mwps2Wbk9frAWm+4FII= github.com/go-git/go-git/v5 v5.16.2 h1:fT6ZIOjE5iEnkzKyxTHK1W4HGAsPhqEqiSAssSO77hM= github.com/go-git/go-git/v5 v5.16.2/go.mod h1:4Ge4alE/5gPs30F2H1esi2gPd69R0C39lolkucHBOp8= +github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI= +github.com/go-logr/logr v1.4.3/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= +github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= +github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= github.com/go-ole/go-ole v1.3.0 h1:Dt6ye7+vXGIKZ7Xtk4s6/xVdGDQynvom7xCFEdWr6uE= github.com/go-ole/go-ole v1.3.0/go.mod h1:5LS6F96DhAwUc7C+1HLexzMXY1xGRSryjyPPKW6zv78= @@ -78,6 +82,8 @@ github.com/go-task/template v0.2.0 h1:xW7ek0o65FUSTbKcSNeg2Vyf/I7wYXFgLUznptvviB github.com/go-task/template v0.2.0/go.mod h1:dbdoUb6qKnHQi1y6o+IdIrs0J4o/SEhSTA6bbzZmdtc= github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 h1:f+oWsMOmNPc8JmEHVZIycC7hBoQxHH9pNKQORJNozsQ= github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8/go.mod h1:wcDNUvekVysuuOpQKo3191zZyTpiI6se1N1ULghS0sw= +github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= +github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= @@ -181,16 +187,28 @@ go.bug.st/f v0.4.0 h1:Vstqb950nMA+PhAlRxUw8QL1ntHy/gXHNyyzjkQLJ10= go.bug.st/f v0.4.0/go.mod h1:bMo23205ll7UW63KwO1ut5RdlJ9JK8RyEEr88CmOF5Y= go.bug.st/relaxed-semver v0.15.0 h1:w37+SYQPxF53RQO7QZZuPIMaPouOifdaP0B1ktst2nA= go.bug.st/relaxed-semver v0.15.0/go.mod h1:bwHiCtYuD2m716tBk2OnOBjelsbXw9el5EIuyxT/ksU= +go.opentelemetry.io/auto/sdk v1.2.1 h1:jXsnJ4Lmnqd11kwkBV2LgLoFMZKizbCi5fNZ/ipaZ64= +go.opentelemetry.io/auto/sdk v1.2.1/go.mod h1:KRTj+aOaElaLi+wW1kO/DZRXwkF4C5xPbEe3ZiIhN7Y= +go.opentelemetry.io/otel v1.38.0 h1:RkfdswUDRimDg0m2Az18RKOsnI8UDzppJAtj01/Ymk8= +go.opentelemetry.io/otel v1.38.0/go.mod h1:zcmtmQ1+YmQM9wrNsTGV/q/uyusom3P8RxwExxkZhjM= +go.opentelemetry.io/otel/metric v1.38.0 h1:Kl6lzIYGAh5M159u9NgiRkmoMKjvbsKtYRwgfrA6WpA= +go.opentelemetry.io/otel/metric v1.38.0/go.mod h1:kB5n/QoRM8YwmUahxvI3bO34eVtQf2i4utNVLr9gEmI= +go.opentelemetry.io/otel/sdk v1.38.0 h1:l48sr5YbNf2hpCUj/FoGhW9yDkl+Ma+LrVl8qaM5b+E= +go.opentelemetry.io/otel/sdk v1.38.0/go.mod h1:ghmNdGlVemJI3+ZB5iDEuk4bWA3GkTpW+DOoZMYBVVg= +go.opentelemetry.io/otel/sdk/metric v1.38.0 h1:aSH66iL0aZqo//xXzQLYozmWrXxyFkBJ6qT5wthqPoM= +go.opentelemetry.io/otel/sdk/metric v1.38.0/go.mod h1:dg9PBnW9XdQ1Hd6ZnRz689CbtrUp0wMMs9iPcgT9EZA= +go.opentelemetry.io/otel/trace v1.38.0 h1:Fxk5bKrDZJUH+AMyyIXGcFAPah0oRcT+LuNtJrmcNLE= +go.opentelemetry.io/otel/trace v1.38.0/go.mod h1:j1P9ivuFsTceSWe1oY+EeW3sc+Pp42sO++GHkg4wwhs= golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= -golang.org/x/crypto v0.37.0 h1:kJNSjF/Xp7kU0iB2Z+9viTPMW4EqqsrywMXLJOOsXSE= -golang.org/x/crypto v0.37.0/go.mod h1:vg+k43peMZ0pUMhYmVAWysMK35e6ioLh3wB8ZCAfbVc= +golang.org/x/crypto v0.43.0 h1:dduJYIi3A3KOfdGOHX8AVZ/jGiyPa3IbBozJ5kNuE04= +golang.org/x/crypto v0.43.0/go.mod h1:BFbav4mRNlXJL4wNeejLpWxB7wMbc79PdRGhWKncxR0= golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 h1:2dVuKD2vS7b0QIHQbpyTISPd0LeHDbnYEryqj5Q1ug8= golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56/go.mod h1:M4RDyNAINzryxdtnbRXRL/OHtkFuWGRjvuhBJpk2IlY= golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= -golang.org/x/net v0.39.0 h1:ZCu7HMWDxpXpaiKdhzIfaltL9Lp31x/3fCP11bc6/fY= -golang.org/x/net v0.39.0/go.mod h1:X7NRbYVEA+ewNkCNyJ513WmMdQ3BineSwVtN2zD/d+E= -golang.org/x/sync v0.16.0 h1:ycBJEhp9p4vXvUZNszeOq0kGTPghopOL8q0fq3vstxw= -golang.org/x/sync v0.16.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA= +golang.org/x/net v0.46.1-0.20251013234738-63d1a5100f82 h1:6/3JGEh1C88g7m+qzzTbl3A0FtsLguXieqofVLU/JAo= +golang.org/x/net v0.46.1-0.20251013234738-63d1a5100f82/go.mod h1:Q9BGdFy1y4nkUwiLvT5qtyhAnEHgnQ/zd8PfU6nc210= +golang.org/x/sync v0.17.0 h1:l60nONMj9l5drqw6jlhIELNv9I0A4OFgRsG9k2oT9Ug= +golang.org/x/sync v0.17.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI= golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -206,12 +224,20 @@ golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.37.0 h1:fdNQudmxPjkdUTPnLn5mdQv7Zwvbvpaxqs831goi9kQ= golang.org/x/sys v0.37.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/term v0.35.0 h1:bZBVKBudEyhRcajGcNc3jIfWPqV4y/Kt2XcoigOWtDQ= -golang.org/x/term v0.35.0/go.mod h1:TPGtkTLesOwf2DE8CgVYiZinHAOuy5AYUYT1lENIZnA= +golang.org/x/term v0.36.0 h1:zMPR+aF8gfksFprF/Nc/rd1wRS1EI6nDBGyWAvDzx2Q= +golang.org/x/term v0.36.0/go.mod h1:Qu394IJq6V6dCBRgwqshf3mPF85AqzYEzofzRdZkWss= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.24.0 h1:dd5Bzh4yt5KYA8f9CJHCP4FB4D51c2c6JvN37xJJkJ0= -golang.org/x/text v0.24.0/go.mod h1:L8rBsPeo2pSS+xqN0d5u2ikmjtmoJbDBT1b7nHvFCdU= +golang.org/x/text v0.30.0 h1:yznKA/E9zq54KzlzBEAWn1NXSQ8DIp/NYMy88xJjl4k= +golang.org/x/text v0.30.0/go.mod h1:yDdHFIX9t+tORqspjENWgzaCVXgk0yYnYuSZ8UzzBVM= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +gonum.org/v1/gonum v0.16.0 h1:5+ul4Swaf3ESvrOnidPp4GZbzf0mxVQpDCYUQE7OJfk= +gonum.org/v1/gonum v0.16.0/go.mod h1:fef3am4MQ93R2HHpKnLk4/Tbh/s0+wqD5nfa6Pnwy4E= +google.golang.org/genproto/googleapis/rpc v0.0.0-20251022142026-3a174f9686a8 h1:M1rk8KBnUsBDg1oPGHNCxG4vc1f49epmTO7xscSajMk= +google.golang.org/genproto/googleapis/rpc v0.0.0-20251022142026-3a174f9686a8/go.mod h1:7i2o+ce6H/6BluujYR+kqX3GKH+dChPTQU19wjRPiGk= +google.golang.org/grpc v1.77.0 h1:wVVY6/8cGA6vvffn+wWK5ToddbgdU3d8MNENr4evgXM= +google.golang.org/grpc v1.77.0/go.mod h1:z0BY1iVj0q8E1uSQCjL9cppRj+gnZjzDnzV0dHhrNig= +google.golang.org/protobuf v1.36.10 h1:AYd7cD/uASjIL6Q9LiTjz8JLcrh/88q5UObnmY3aOOE= +google.golang.org/protobuf v1.36.10/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= diff --git a/rpc/cc/arduino/flasher/v1/commands.pb.go b/rpc/cc/arduino/flasher/v1/commands.pb.go new file mode 100644 index 0000000..494ac7e --- /dev/null +++ b/rpc/cc/arduino/flasher/v1/commands.pb.go @@ -0,0 +1,299 @@ +// 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. + +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.34.2 +// protoc (unknown) +// source: cc/arduino/flasher/v1/commands.proto + +package flasher + +import ( + reflect "reflect" + sync "sync" + + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type ListRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields +} + +func (x *ListRequest) Reset() { + *x = ListRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_cc_arduino_flasher_v1_commands_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *ListRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ListRequest) ProtoMessage() {} + +func (x *ListRequest) ProtoReflect() protoreflect.Message { + mi := &file_cc_arduino_flasher_v1_commands_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use ListRequest.ProtoReflect.Descriptor instead. +func (*ListRequest) Descriptor() ([]byte, []int) { + return file_cc_arduino_flasher_v1_commands_proto_rawDescGZIP(), []int{0} +} + +type ListResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Releases []*Release `protobuf:"bytes,1,rep,name=releases,proto3" json:"releases,omitempty"` +} + +func (x *ListResponse) Reset() { + *x = ListResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_cc_arduino_flasher_v1_commands_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *ListResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ListResponse) ProtoMessage() {} + +func (x *ListResponse) ProtoReflect() protoreflect.Message { + mi := &file_cc_arduino_flasher_v1_commands_proto_msgTypes[1] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use ListResponse.ProtoReflect.Descriptor instead. +func (*ListResponse) Descriptor() ([]byte, []int) { + return file_cc_arduino_flasher_v1_commands_proto_rawDescGZIP(), []int{1} +} + +func (x *ListResponse) GetReleases() []*Release { + if x != nil { + return x.Releases + } + return nil +} + +type Release struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + BuildId string `protobuf:"bytes,1,opt,name=build_id,json=buildId,proto3" json:"build_id,omitempty"` + Latest bool `protobuf:"varint,2,opt,name=latest,proto3" json:"latest,omitempty"` +} + +func (x *Release) Reset() { + *x = Release{} + if protoimpl.UnsafeEnabled { + mi := &file_cc_arduino_flasher_v1_commands_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Release) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Release) ProtoMessage() {} + +func (x *Release) ProtoReflect() protoreflect.Message { + mi := &file_cc_arduino_flasher_v1_commands_proto_msgTypes[2] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Release.ProtoReflect.Descriptor instead. +func (*Release) Descriptor() ([]byte, []int) { + return file_cc_arduino_flasher_v1_commands_proto_rawDescGZIP(), []int{2} +} + +func (x *Release) GetBuildId() string { + if x != nil { + return x.BuildId + } + return "" +} + +func (x *Release) GetLatest() bool { + if x != nil { + return x.Latest + } + return false +} + +var File_cc_arduino_flasher_v1_commands_proto protoreflect.FileDescriptor + +var file_cc_arduino_flasher_v1_commands_proto_rawDesc = []byte{ + 0x0a, 0x24, 0x63, 0x63, 0x2f, 0x61, 0x72, 0x64, 0x75, 0x69, 0x6e, 0x6f, 0x2f, 0x66, 0x6c, 0x61, + 0x73, 0x68, 0x65, 0x72, 0x2f, 0x76, 0x31, 0x2f, 0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x73, + 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x15, 0x63, 0x63, 0x2e, 0x61, 0x72, 0x64, 0x75, 0x69, + 0x6e, 0x6f, 0x2e, 0x66, 0x6c, 0x61, 0x73, 0x68, 0x65, 0x72, 0x2e, 0x76, 0x31, 0x22, 0x0d, 0x0a, + 0x0b, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x22, 0x4a, 0x0a, 0x0c, + 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x3a, 0x0a, 0x08, + 0x72, 0x65, 0x6c, 0x65, 0x61, 0x73, 0x65, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x1e, + 0x2e, 0x63, 0x63, 0x2e, 0x61, 0x72, 0x64, 0x75, 0x69, 0x6e, 0x6f, 0x2e, 0x66, 0x6c, 0x61, 0x73, + 0x68, 0x65, 0x72, 0x2e, 0x76, 0x31, 0x2e, 0x52, 0x65, 0x6c, 0x65, 0x61, 0x73, 0x65, 0x52, 0x08, + 0x72, 0x65, 0x6c, 0x65, 0x61, 0x73, 0x65, 0x73, 0x22, 0x3c, 0x0a, 0x07, 0x52, 0x65, 0x6c, 0x65, + 0x61, 0x73, 0x65, 0x12, 0x19, 0x0a, 0x08, 0x62, 0x75, 0x69, 0x6c, 0x64, 0x5f, 0x69, 0x64, 0x18, + 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x62, 0x75, 0x69, 0x6c, 0x64, 0x49, 0x64, 0x12, 0x16, + 0x0a, 0x06, 0x6c, 0x61, 0x74, 0x65, 0x73, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x08, 0x52, 0x06, + 0x6c, 0x61, 0x74, 0x65, 0x73, 0x74, 0x32, 0x5c, 0x0a, 0x07, 0x46, 0x6c, 0x61, 0x73, 0x68, 0x65, + 0x72, 0x12, 0x51, 0x0a, 0x04, 0x4c, 0x69, 0x73, 0x74, 0x12, 0x22, 0x2e, 0x63, 0x63, 0x2e, 0x61, + 0x72, 0x64, 0x75, 0x69, 0x6e, 0x6f, 0x2e, 0x66, 0x6c, 0x61, 0x73, 0x68, 0x65, 0x72, 0x2e, 0x76, + 0x31, 0x2e, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x23, 0x2e, + 0x63, 0x63, 0x2e, 0x61, 0x72, 0x64, 0x75, 0x69, 0x6e, 0x6f, 0x2e, 0x66, 0x6c, 0x61, 0x73, 0x68, + 0x65, 0x72, 0x2e, 0x76, 0x31, 0x2e, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, + 0x73, 0x65, 0x22, 0x00, 0x42, 0x4a, 0x5a, 0x48, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, + 0x6f, 0x6d, 0x2f, 0x61, 0x72, 0x64, 0x75, 0x69, 0x6e, 0x6f, 0x2f, 0x61, 0x72, 0x64, 0x75, 0x69, + 0x6e, 0x6f, 0x2d, 0x66, 0x6c, 0x61, 0x73, 0x68, 0x65, 0x72, 0x2d, 0x63, 0x6c, 0x69, 0x2f, 0x72, + 0x70, 0x63, 0x2f, 0x63, 0x63, 0x2f, 0x61, 0x72, 0x64, 0x75, 0x69, 0x6e, 0x6f, 0x2f, 0x66, 0x6c, + 0x61, 0x73, 0x68, 0x65, 0x72, 0x2f, 0x76, 0x31, 0x3b, 0x66, 0x6c, 0x61, 0x73, 0x68, 0x65, 0x72, + 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_cc_arduino_flasher_v1_commands_proto_rawDescOnce sync.Once + file_cc_arduino_flasher_v1_commands_proto_rawDescData = file_cc_arduino_flasher_v1_commands_proto_rawDesc +) + +func file_cc_arduino_flasher_v1_commands_proto_rawDescGZIP() []byte { + file_cc_arduino_flasher_v1_commands_proto_rawDescOnce.Do(func() { + file_cc_arduino_flasher_v1_commands_proto_rawDescData = protoimpl.X.CompressGZIP(file_cc_arduino_flasher_v1_commands_proto_rawDescData) + }) + return file_cc_arduino_flasher_v1_commands_proto_rawDescData +} + +var file_cc_arduino_flasher_v1_commands_proto_msgTypes = make([]protoimpl.MessageInfo, 3) +var file_cc_arduino_flasher_v1_commands_proto_goTypes = []any{ + (*ListRequest)(nil), // 0: cc.arduino.flasher.v1.ListRequest + (*ListResponse)(nil), // 1: cc.arduino.flasher.v1.ListResponse + (*Release)(nil), // 2: cc.arduino.flasher.v1.Release +} +var file_cc_arduino_flasher_v1_commands_proto_depIdxs = []int32{ + 2, // 0: cc.arduino.flasher.v1.ListResponse.releases:type_name -> cc.arduino.flasher.v1.Release + 0, // 1: cc.arduino.flasher.v1.Flasher.List:input_type -> cc.arduino.flasher.v1.ListRequest + 1, // 2: cc.arduino.flasher.v1.Flasher.List:output_type -> cc.arduino.flasher.v1.ListResponse + 2, // [2:3] is the sub-list for method output_type + 1, // [1:2] is the sub-list for method input_type + 1, // [1:1] is the sub-list for extension type_name + 1, // [1:1] is the sub-list for extension extendee + 0, // [0:1] is the sub-list for field type_name +} + +func init() { file_cc_arduino_flasher_v1_commands_proto_init() } +func file_cc_arduino_flasher_v1_commands_proto_init() { + if File_cc_arduino_flasher_v1_commands_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_cc_arduino_flasher_v1_commands_proto_msgTypes[0].Exporter = func(v any, i int) any { + switch v := v.(*ListRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_cc_arduino_flasher_v1_commands_proto_msgTypes[1].Exporter = func(v any, i int) any { + switch v := v.(*ListResponse); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_cc_arduino_flasher_v1_commands_proto_msgTypes[2].Exporter = func(v any, i int) any { + switch v := v.(*Release); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_cc_arduino_flasher_v1_commands_proto_rawDesc, + NumEnums: 0, + NumMessages: 3, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_cc_arduino_flasher_v1_commands_proto_goTypes, + DependencyIndexes: file_cc_arduino_flasher_v1_commands_proto_depIdxs, + MessageInfos: file_cc_arduino_flasher_v1_commands_proto_msgTypes, + }.Build() + File_cc_arduino_flasher_v1_commands_proto = out.File + file_cc_arduino_flasher_v1_commands_proto_rawDesc = nil + file_cc_arduino_flasher_v1_commands_proto_goTypes = nil + file_cc_arduino_flasher_v1_commands_proto_depIdxs = nil +} diff --git a/rpc/cc/arduino/flasher/v1/commands.proto b/rpc/cc/arduino/flasher/v1/commands.proto new file mode 100644 index 0000000..c2de4d4 --- /dev/null +++ b/rpc/cc/arduino/flasher/v1/commands.proto @@ -0,0 +1,35 @@ +// 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. + +syntax = "proto3"; + +package cc.arduino.flasher.v1; + +option go_package = "github.com/arduino/arduino-flasher-cli/rpc/cc/arduino/flasher/v1;flasher"; + +service Flasher { + rpc List(ListRequest) returns (ListResponse) {}; +} + +message ListRequest {} + +message ListResponse { + repeated Release releases = 1; +} + +message Release { + string build_id = 1; + bool latest = 2; +} diff --git a/rpc/cc/arduino/flasher/v1/commands_grpc.pb.go b/rpc/cc/arduino/flasher/v1/commands_grpc.pb.go new file mode 100644 index 0000000..9ed20e7 --- /dev/null +++ b/rpc/cc/arduino/flasher/v1/commands_grpc.pb.go @@ -0,0 +1,137 @@ +// 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. + +// Code generated by protoc-gen-go-grpc. DO NOT EDIT. +// versions: +// - protoc-gen-go-grpc v1.5.1 +// - protoc (unknown) +// source: cc/arduino/flasher/v1/commands.proto + +package flasher + +import ( + context "context" + + grpc "google.golang.org/grpc" + codes "google.golang.org/grpc/codes" + status "google.golang.org/grpc/status" +) + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the grpc package it is being compiled against. +// Requires gRPC-Go v1.64.0 or later. +const _ = grpc.SupportPackageIsVersion9 + +const ( + Flasher_List_FullMethodName = "/cc.arduino.flasher.v1.Flasher/List" +) + +// FlasherClient is the client API for Flasher service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. +type FlasherClient interface { + List(ctx context.Context, in *ListRequest, opts ...grpc.CallOption) (*ListResponse, error) +} + +type flasherClient struct { + cc grpc.ClientConnInterface +} + +func NewFlasherClient(cc grpc.ClientConnInterface) FlasherClient { + return &flasherClient{cc} +} + +func (c *flasherClient) List(ctx context.Context, in *ListRequest, opts ...grpc.CallOption) (*ListResponse, error) { + cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) + out := new(ListResponse) + err := c.cc.Invoke(ctx, Flasher_List_FullMethodName, in, out, cOpts...) + if err != nil { + return nil, err + } + return out, nil +} + +// FlasherServer is the server API for Flasher service. +// All implementations must embed UnimplementedFlasherServer +// for forward compatibility. +type FlasherServer interface { + List(context.Context, *ListRequest) (*ListResponse, error) + mustEmbedUnimplementedFlasherServer() +} + +// UnimplementedFlasherServer must be embedded to have +// forward compatible implementations. +// +// NOTE: this should be embedded by value instead of pointer to avoid a nil +// pointer dereference when methods are called. +type UnimplementedFlasherServer struct{} + +func (UnimplementedFlasherServer) List(context.Context, *ListRequest) (*ListResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method List not implemented") +} +func (UnimplementedFlasherServer) mustEmbedUnimplementedFlasherServer() {} +func (UnimplementedFlasherServer) testEmbeddedByValue() {} + +// UnsafeFlasherServer may be embedded to opt out of forward compatibility for this service. +// Use of this interface is not recommended, as added methods to FlasherServer will +// result in compilation errors. +type UnsafeFlasherServer interface { + mustEmbedUnimplementedFlasherServer() +} + +func RegisterFlasherServer(s grpc.ServiceRegistrar, srv FlasherServer) { + // If the following call pancis, it indicates UnimplementedFlasherServer was + // embedded by pointer and is nil. This will cause panics if an + // unimplemented method is ever invoked, so we test this at initialization + // time to prevent it from happening at runtime later due to I/O. + if t, ok := srv.(interface{ testEmbeddedByValue() }); ok { + t.testEmbeddedByValue() + } + s.RegisterService(&Flasher_ServiceDesc, srv) +} + +func _Flasher_List_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(ListRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(FlasherServer).List(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: Flasher_List_FullMethodName, + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(FlasherServer).List(ctx, req.(*ListRequest)) + } + return interceptor(ctx, in, info, handler) +} + +// Flasher_ServiceDesc is the grpc.ServiceDesc for Flasher service. +// It's only intended for direct use with grpc.RegisterService, +// and not to be introspected or modified (even as a copy) +var Flasher_ServiceDesc = grpc.ServiceDesc{ + ServiceName: "cc.arduino.flasher.v1.Flasher", + HandlerType: (*FlasherServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "List", + Handler: _Flasher_List_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "cc/arduino/flasher/v1/commands.proto", +} diff --git a/service/service.go b/service/service.go new file mode 100644 index 0000000..bd9ba05 --- /dev/null +++ b/service/service.go @@ -0,0 +1,26 @@ +// 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 service + +import flasher "github.com/arduino/arduino-flasher-cli/rpc/cc/arduino/flasher/v1" + +type flasherServerImpl struct { + flasher.UnsafeFlasherServer // Force compile error for unimplemented methods +} + +func NewFlasherServer() flasher.FlasherServer { + return &flasherServerImpl{} +} diff --git a/service/service_list.go b/service/service_list.go new file mode 100644 index 0000000..6b69576 --- /dev/null +++ b/service/service_list.go @@ -0,0 +1,43 @@ +// 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 service + +import ( + "context" + + "github.com/arduino/arduino-flasher-cli/internal/updater" + flasher "github.com/arduino/arduino-flasher-cli/rpc/cc/arduino/flasher/v1" +) + +func (s *flasherServerImpl) List(ctx context.Context, req *flasher.ListRequest) (*flasher.ListResponse, error) { + client := updater.NewClient() + + manifest, err := client.GetInfoManifest(ctx) + if err != nil { + return nil, err + } + + resp := &flasher.ListResponse{} + for i := len(manifest.Releases) - 1; i >= 0; i-- { + latest := false + if manifest.Releases[i].Version == manifest.Latest.Version { + latest = true + } + resp.Releases = append(resp.Releases, &flasher.Release{BuildId: manifest.Releases[i].Version, Latest: latest}) + } + + return resp, nil +}