Skip to content

Commit 76198da

Browse files
committed
Better client example
1 parent c226f62 commit 76198da

File tree

1 file changed

+36
-5
lines changed

1 file changed

+36
-5
lines changed

daemon/client/client.go

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,63 @@ import (
44
"context"
55
"fmt"
66
"log"
7+
"os"
78

89
"github.com/arduino/arduino-cli/rpc"
910
"google.golang.org/grpc"
1011
)
1112

1213
func main() {
14+
if len(os.Args) != 2 {
15+
fmt.Println("Please specify Arduino DATA_DIR as first argument")
16+
os.Exit(1)
17+
}
18+
datadir := os.Args[1]
1319
conn, err := grpc.Dial("127.0.0.1:50051", grpc.WithInsecure())
1420
if err != nil {
1521
log.Fatal(err)
1622
}
1723
client := rpc.NewArduinoCoreClient(conn)
1824

19-
resp, err := client.Init(context.Background(), &rpc.InitReq{})
25+
resp, err := client.Init(context.Background(), &rpc.InitReq{
26+
Configuration: &rpc.Configuration{
27+
DataDir: datadir,
28+
},
29+
})
2030
if err != nil {
2131
log.Fatal(err)
2232
}
23-
fmt.Println(resp.GetInstance())
33+
if resp.GetResult().GetFailed() {
34+
fmt.Println("Error opening server instance:", resp.GetResult().GetMessage())
35+
os.Exit(1)
36+
}
37+
instance := resp.GetInstance()
38+
fmt.Println("Opened new server instance:", instance)
2439

2540
details, err := client.BoardDetails(context.Background(), &rpc.BoardDetailsReq{
26-
Instance: resp.GetInstance(),
27-
Fqbn: "",
41+
Instance: instance,
42+
Fqbn: "arduino:samd:mkr1000",
43+
})
44+
if err != nil {
45+
log.Fatal(err)
46+
}
47+
if details.GetResult().GetFailed() {
48+
fmt.Println("Error getting board data:", details.GetResult().GetMessage())
49+
} else {
50+
fmt.Println("Board name: ", details.GetName())
51+
}
52+
53+
destroyResp, err := client.Destroy(context.Background(), &rpc.DestroyReq{
54+
Instance: instance,
2855
})
2956
if err != nil {
3057
log.Fatal(err)
3158
}
32-
fmt.Println(details.GetName())
59+
if destroyResp.GetResult().GetFailed() {
60+
fmt.Println("Error closing instance:", destroyResp.GetResult().GetMessage())
61+
} else {
62+
fmt.Println("Successfully closed server instance")
63+
}
3364
/*
3465
compile, err := client.Compile(context.Background(), &pb.CompileReq{})
3566
if err != nil {

0 commit comments

Comments
 (0)