Skip to content

Commit

Permalink
Updated client_example
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaglie committed May 6, 2024
1 parent c6f07f6 commit b10536f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 90 deletions.
2 changes: 1 addition & 1 deletion commands/service_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
144 changes: 55 additions & 89 deletions rpc/internal/client_example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"io"
"log"
"os"
"path"
"path/filepath"
"strings"
"time"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit b10536f

Please sign in to comment.