From b10536f9e4c17746ed3e03fdc0c491e6be0dace1 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Mon, 6 May 2024 15:41:11 +0200 Subject: [PATCH] Updated client_example --- commands/service_settings.go | 2 +- rpc/internal/client_example/main.go | 144 +++++++++++----------------- 2 files changed, 56 insertions(+), 90 deletions(-) diff --git a/commands/service_settings.go b/commands/service_settings.go index cb6e00fbe91..c63b5be1877 100644 --- a/commands/service_settings.go +++ b/commands/service_settings.go @@ -177,7 +177,7 @@ func (s *arduinoCoreServerImpl) ConfigurationSave(ctx context.Context, req *rpc. } return &rpc.ConfigurationSaveResponse{EncodedSettings: string(data)}, nil case "json": - data, err := json.Marshal(s.settings) + data, err := json.MarshalIndent(s.settings, "", " ") if err != nil { return nil, fmt.Errorf("error marshalling settings: %v", err) } diff --git a/rpc/internal/client_example/main.go b/rpc/internal/client_example/main.go index eabf6bfd2d9..9fe750f36ea 100644 --- a/rpc/internal/client_example/main.go +++ b/rpc/internal/client_example/main.go @@ -23,6 +23,7 @@ import ( "io" "log" "os" + "path" "path/filepath" "strings" "time" @@ -71,40 +72,24 @@ func main() { callLoadSketch(client) // Use SetValue to configure the arduino-cli directories. - log.Println("calling SetValue") - callSetValue(client) + callSetValue(client, "directories.data", `"`+dataDir+`"`) + callSetValue(client, "directories.downloads", `"`+path.Join(dataDir, "staging")+`"`) + callSetValue(client, "directories.user", `"`+path.Join(dataDir, "sketchbook")+`"`) // List all settings - log.Println("calling SettingsGetAll()") - callGetAll(client) + callConfigurationSave(client) // Merge applies multiple settings values at once. - log.Println("calling SettingsMerge()") - callMerge(client, `{"foo": {"value": "bar"}, "daemon":{"port":"422"}, "board_manager": {"additional_urls":["https://example.com"]}}`) + callSetValue(client, "daemon.port", `"422"`) + callSetValue(client, "board_manager.additional_urls", `[ "https://example.com" ]`) - log.Println("calling SettingsGetAll()") - callGetAll(client) + callConfigurationSave(client) - log.Println("calling SettingsMerge()") - callMerge(client, `{"foo": {} }`) + callSetValue(client, "daemon.port", "") + callGetValue(client, "daemon.port") + callGetValue(client, "directories.data") - log.Println("calling SettingsGetAll()") - callGetAll(client) - - log.Println("calling SettingsMerge()") - callMerge(client, `{"foo": "bar" }`) - - // Get the value of the foo key. - log.Println("calling SettingsGetValue(foo)") - callGetValue(client) - - // List all settings - log.Println("calling SettingsGetAll()") - callGetAll(client) - - // Write settings to file. - log.Println("calling Write()") - callWrite(client) + callConfigurationSave(client) // Before we can do anything with the CLI, an "instance" must be created. // We keep a reference to the created instance because we will need it to @@ -243,85 +228,66 @@ func callVersion(client rpc.ArduinoCoreServiceClient) { log.Printf("arduino-cli version: %v", versionResp.GetVersion()) } -func callSetValue(client rpc.ArduinoCoreServiceClient) { - // _, err := client.SettingsSetValue(context.Background(), - // &rpc.SettingsSetValueRequest{ - // Key: "directories", - // JsonData: `{"data": "` + dataDir + `", "downloads": "` + path.Join(dataDir, "staging") + `", "user": "` + path.Join(dataDir, "sketchbook") + `"}`, - // }) +func callSetValue(client rpc.ArduinoCoreServiceClient, key, jsonValue string) { + log.Printf("Calling SetValue: %s = %s", key, jsonValue) + _, err := client.SettingsSetValue(context.Background(), + &rpc.SettingsSetValueRequest{ + Key: key, + EncodedValue: jsonValue, + }) - // if err != nil { - // log.Fatalf("Error setting settings value: %s", err) - // } + if err != nil { + log.Fatalf("Error setting settings value: %s", err) + } } func callSetProxy(client rpc.ArduinoCoreServiceClient) { - // _, err := client.SettingsSetValue(context.Background(), - // &rpc.SettingsSetValueRequest{ - // Key: "network.proxy", - // JsonData: `"http://localhost:3128"`, - // }) + _, err := client.SettingsSetValue(context.Background(), + &rpc.SettingsSetValueRequest{ + Key: "network.proxy", + EncodedValue: `"http://localhost:3128"`, + }) - // if err != nil { - // log.Fatalf("Error setting settings value: %s", err) - // } + if err != nil { + log.Fatalf("Error setting settings value: %s", err) + } } func callUnsetProxy(client rpc.ArduinoCoreServiceClient) { - // _, err := client.SettingsSetValue(context.Background(), - // &rpc.SettingsSetValueRequest{ - // Key: "network.proxy", - // JsonData: `""`, - // }) - - // if err != nil { - // log.Fatalf("Error setting settings value: %s", err) - // } -} - -func callMerge(client rpc.ArduinoCoreServiceClient, jsonData string) { - // _, err := client.SettingsMerge(context.Background(), - // &rpc.SettingsMergeRequest{ - // JsonData: jsonData, - // }) + _, err := client.SettingsSetValue(context.Background(), + &rpc.SettingsSetValueRequest{ + Key: "network.proxy", + }) - // if err != nil { - // log.Fatalf("Error merging settings: %s", err) - // } + if err != nil { + log.Fatalf("Error setting settings value: %s", err) + } } -func callGetValue(client rpc.ArduinoCoreServiceClient) { - // getValueResp, err := client.SettingsGetValue(context.Background(), - // &rpc.SettingsGetValueRequest{ - // Key: "foo", - // }) +func callGetValue(client rpc.ArduinoCoreServiceClient, key string) { + getValueResp, err := client.SettingsGetValue(context.Background(), + &rpc.SettingsGetValueRequest{ + Key: key, + }) - // if err != nil { - // log.Fatalf("Error getting settings value: %s", err) - // } + if err != nil { + log.Fatalf("Error getting settings value: %s", err) + } - // log.Printf("Value: %s: %s", getValueResp.GetKey(), getValueResp.GetJsonData()) + log.Printf("%s = %s", key, getValueResp.GetEncodedValue()) } -func callGetAll(client rpc.ArduinoCoreServiceClient) { - // getAllResp, err := client.SettingsGetAll(context.Background(), &rpc.SettingsGetAllRequest{}) - - // if err != nil { - // log.Fatalf("Error getting settings: %s", err) - // } - - // log.Printf("Settings: %s", getAllResp.GetJsonData()) -} +func callConfigurationSave(client rpc.ArduinoCoreServiceClient) { + log.Println("calling ConfigurationSave() >>") + getAllResp, err := client.ConfigurationSave(context.Background(), &rpc.ConfigurationSaveRequest{ + SettingsFormat: "json", + }) -func callWrite(client rpc.ArduinoCoreServiceClient) { - // _, err := client.SettingsWrite(context.Background(), - // &rpc.SettingsWriteRequest{ - // FilePath: path.Join(dataDir, "written-rpc.Settingsyml"), - // }) + if err != nil { + log.Fatalf("Error getting settings: %s", err) + } - // if err != nil { - // log.Fatalf("Error writing settings: %s", err) - // } + log.Printf("Settings follow:\n\n%s", getAllResp.GetEncodedSettings()) } func createInstance(client rpc.ArduinoCoreServiceClient) *rpc.Instance {