Skip to content

Commit e551d6e

Browse files
committed
Updated client_example
1 parent 53eeaba commit e551d6e

File tree

2 files changed

+56
-90
lines changed

2 files changed

+56
-90
lines changed

commands/service_settings.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ func (s *arduinoCoreServerImpl) ConfigurationSave(ctx context.Context, req *rpc.
177177
}
178178
return &rpc.ConfigurationSaveResponse{EncodedSettings: string(data)}, nil
179179
case "json":
180-
data, err := json.Marshal(s.settings)
180+
data, err := json.MarshalIndent(s.settings, "", " ")
181181
if err != nil {
182182
return nil, fmt.Errorf("error marshalling settings: %v", err)
183183
}

rpc/internal/client_example/main.go

+55-89
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"io"
2424
"log"
2525
"os"
26+
"path"
2627
"path/filepath"
2728
"strings"
2829
"time"
@@ -71,40 +72,24 @@ func main() {
7172
callLoadSketch(client)
7273

7374
// Use SetValue to configure the arduino-cli directories.
74-
log.Println("calling SetValue")
75-
callSetValue(client)
75+
callSetValue(client, "directories.data", `"`+dataDir+`"`)
76+
callSetValue(client, "directories.downloads", `"`+path.Join(dataDir, "staging")+`"`)
77+
callSetValue(client, "directories.user", `"`+path.Join(dataDir, "sketchbook")+`"`)
7678

7779
// List all settings
78-
log.Println("calling SettingsGetAll()")
79-
callGetAll(client)
80+
callConfigurationSave(client)
8081

8182
// Merge applies multiple settings values at once.
82-
log.Println("calling SettingsMerge()")
83-
callMerge(client, `{"foo": {"value": "bar"}, "daemon":{"port":"422"}, "board_manager": {"additional_urls":["https://example.com"]}}`)
83+
callSetValue(client, "daemon.port", `"422"`)
84+
callSetValue(client, "board_manager.additional_urls", `[ "https://example.com" ]`)
8485

85-
log.Println("calling SettingsGetAll()")
86-
callGetAll(client)
86+
callConfigurationSave(client)
8787

88-
log.Println("calling SettingsMerge()")
89-
callMerge(client, `{"foo": {} }`)
88+
callSetValue(client, "daemon.port", "")
89+
callGetValue(client, "daemon.port")
90+
callGetValue(client, "directories.data")
9091

91-
log.Println("calling SettingsGetAll()")
92-
callGetAll(client)
93-
94-
log.Println("calling SettingsMerge()")
95-
callMerge(client, `{"foo": "bar" }`)
96-
97-
// Get the value of the foo key.
98-
log.Println("calling SettingsGetValue(foo)")
99-
callGetValue(client)
100-
101-
// List all settings
102-
log.Println("calling SettingsGetAll()")
103-
callGetAll(client)
104-
105-
// Write settings to file.
106-
log.Println("calling Write()")
107-
callWrite(client)
92+
callConfigurationSave(client)
10893

10994
// Before we can do anything with the CLI, an "instance" must be created.
11095
// We keep a reference to the created instance because we will need it to
@@ -243,85 +228,66 @@ func callVersion(client rpc.ArduinoCoreServiceClient) {
243228
log.Printf("arduino-cli version: %v", versionResp.GetVersion())
244229
}
245230

246-
func callSetValue(client rpc.ArduinoCoreServiceClient) {
247-
// _, err := client.SettingsSetValue(context.Background(),
248-
// &rpc.SettingsSetValueRequest{
249-
// Key: "directories",
250-
// JsonData: `{"data": "` + dataDir + `", "downloads": "` + path.Join(dataDir, "staging") + `", "user": "` + path.Join(dataDir, "sketchbook") + `"}`,
251-
// })
231+
func callSetValue(client rpc.ArduinoCoreServiceClient, key, jsonValue string) {
232+
log.Printf("Calling SetValue: %s = %s", key, jsonValue)
233+
_, err := client.SettingsSetValue(context.Background(),
234+
&rpc.SettingsSetValueRequest{
235+
Key: key,
236+
EncodedValue: jsonValue,
237+
})
252238

253-
// if err != nil {
254-
// log.Fatalf("Error setting settings value: %s", err)
255-
// }
239+
if err != nil {
240+
log.Fatalf("Error setting settings value: %s", err)
241+
}
256242
}
257243

258244
func callSetProxy(client rpc.ArduinoCoreServiceClient) {
259-
// _, err := client.SettingsSetValue(context.Background(),
260-
// &rpc.SettingsSetValueRequest{
261-
// Key: "network.proxy",
262-
// JsonData: `"http://localhost:3128"`,
263-
// })
245+
_, err := client.SettingsSetValue(context.Background(),
246+
&rpc.SettingsSetValueRequest{
247+
Key: "network.proxy",
248+
EncodedValue: `"http://localhost:3128"`,
249+
})
264250

265-
// if err != nil {
266-
// log.Fatalf("Error setting settings value: %s", err)
267-
// }
251+
if err != nil {
252+
log.Fatalf("Error setting settings value: %s", err)
253+
}
268254
}
269255

270256
func callUnsetProxy(client rpc.ArduinoCoreServiceClient) {
271-
// _, err := client.SettingsSetValue(context.Background(),
272-
// &rpc.SettingsSetValueRequest{
273-
// Key: "network.proxy",
274-
// JsonData: `""`,
275-
// })
276-
277-
// if err != nil {
278-
// log.Fatalf("Error setting settings value: %s", err)
279-
// }
280-
}
281-
282-
func callMerge(client rpc.ArduinoCoreServiceClient, jsonData string) {
283-
// _, err := client.SettingsMerge(context.Background(),
284-
// &rpc.SettingsMergeRequest{
285-
// JsonData: jsonData,
286-
// })
257+
_, err := client.SettingsSetValue(context.Background(),
258+
&rpc.SettingsSetValueRequest{
259+
Key: "network.proxy",
260+
})
287261

288-
// if err != nil {
289-
// log.Fatalf("Error merging settings: %s", err)
290-
// }
262+
if err != nil {
263+
log.Fatalf("Error setting settings value: %s", err)
264+
}
291265
}
292266

293-
func callGetValue(client rpc.ArduinoCoreServiceClient) {
294-
// getValueResp, err := client.SettingsGetValue(context.Background(),
295-
// &rpc.SettingsGetValueRequest{
296-
// Key: "foo",
297-
// })
267+
func callGetValue(client rpc.ArduinoCoreServiceClient, key string) {
268+
getValueResp, err := client.SettingsGetValue(context.Background(),
269+
&rpc.SettingsGetValueRequest{
270+
Key: key,
271+
})
298272

299-
// if err != nil {
300-
// log.Fatalf("Error getting settings value: %s", err)
301-
// }
273+
if err != nil {
274+
log.Fatalf("Error getting settings value: %s", err)
275+
}
302276

303-
// log.Printf("Value: %s: %s", getValueResp.GetKey(), getValueResp.GetJsonData())
277+
log.Printf("%s = %s", key, getValueResp.GetEncodedValue())
304278
}
305279

306-
func callGetAll(client rpc.ArduinoCoreServiceClient) {
307-
// getAllResp, err := client.SettingsGetAll(context.Background(), &rpc.SettingsGetAllRequest{})
308-
309-
// if err != nil {
310-
// log.Fatalf("Error getting settings: %s", err)
311-
// }
312-
313-
// log.Printf("Settings: %s", getAllResp.GetJsonData())
314-
}
280+
func callConfigurationSave(client rpc.ArduinoCoreServiceClient) {
281+
log.Println("calling ConfigurationSave() >>")
282+
getAllResp, err := client.ConfigurationSave(context.Background(), &rpc.ConfigurationSaveRequest{
283+
SettingsFormat: "json",
284+
})
315285

316-
func callWrite(client rpc.ArduinoCoreServiceClient) {
317-
// _, err := client.SettingsWrite(context.Background(),
318-
// &rpc.SettingsWriteRequest{
319-
// FilePath: path.Join(dataDir, "written-rpc.Settingsyml"),
320-
// })
286+
if err != nil {
287+
log.Fatalf("Error getting settings: %s", err)
288+
}
321289

322-
// if err != nil {
323-
// log.Fatalf("Error writing settings: %s", err)
324-
// }
290+
log.Printf("Settings follow:\n\n%s", getAllResp.GetEncodedSettings())
325291
}
326292

327293
func createInstance(client rpc.ArduinoCoreServiceClient) *rpc.Instance {

0 commit comments

Comments
 (0)