Skip to content

Commit

Permalink
[breaking] Remove gRPC settings service (#2411)
Browse files Browse the repository at this point in the history
* Removed setting service

* Updated docs

* Update mkdocs configuration
  • Loading branch information
cmaglie committed Nov 8, 2023
1 parent 0e5f629 commit 50a8bf5
Show file tree
Hide file tree
Showing 17 changed files with 1,899 additions and 1,958 deletions.
2 changes: 0 additions & 2 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -237,13 +237,11 @@ tasks:
desc: Compile protobuf definitions
cmds:
- '{{ default "protoc" .PROTOC_BINARY }} --proto_path=rpc --go_out=./rpc --go_opt=paths=source_relative --go-grpc_out=./rpc --go-grpc_opt=paths=source_relative ./rpc/cc/arduino/cli/commands/v1/*.proto'
- '{{ default "protoc" .PROTOC_BINARY }} --proto_path=rpc --go_out=./rpc --go_opt=paths=source_relative --go-grpc_out=./rpc --go-grpc_opt=paths=source_relative ./rpc/cc/arduino/cli/settings/v1/*.proto'

protoc:docs:
desc: Generate docs for protobuf definitions
cmds:
- '{{ default "protoc" .PROTOC_BINARY }} --doc_out=./docs/rpc --doc_opt=markdown,commands.md --proto_path=rpc ./rpc/cc/arduino/cli/commands/v1/*.proto'
- '{{ default "protoc" .PROTOC_BINARY }} --doc_out=./docs/rpc --doc_opt=markdown,settings.md --proto_path=rpc ./rpc/cc/arduino/cli/settings/v1/*.proto'

docs:include-configuration-json-schema:
desc: Copy configuration JSON schema to make it available in documentation
Expand Down
88 changes: 42 additions & 46 deletions client_example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"time"

rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
"github.com/arduino/arduino-cli/rpc/cc/arduino/cli/settings/v1"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
Expand Down Expand Up @@ -63,8 +62,6 @@ func main() {
// Create an instance of the gRPC clients.
client := rpc.NewArduinoCoreServiceClient(conn)

settingsClient := settings.NewSettingsServiceClient(conn)

// Now we can call various methods of the API...

// `Version` can be called without any setup or init procedure.
Expand All @@ -76,39 +73,39 @@ func main() {

// Use SetValue to configure the arduino-cli directories.
log.Println("calling SetValue")
callSetValue(settingsClient)
callSetValue(client)

// List all the settings.
log.Println("calling GetAll()")
callGetAll(settingsClient)
// List all settings
log.Println("calling SettingsGetAll()")
callGetAll(client)

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

log.Println("calling GetAll()")
callGetAll(settingsClient)
log.Println("calling SettingsGetAll()")
callGetAll(client)

log.Println("calling Merge()")
callMerge(settingsClient, `{"foo": {} }`)
log.Println("calling SettingsMerge()")
callMerge(client, `{"foo": {} }`)

log.Println("calling GetAll()")
callGetAll(settingsClient)
log.Println("calling SettingsGetAll()")
callGetAll(client)

log.Println("calling Merge()")
callMerge(settingsClient, `{"foo": "bar" }`)
log.Println("calling SettingsMerge()")
callMerge(client, `{"foo": "bar" }`)

// Get the value of the foo key.
log.Println("calling GetValue(foo)")
callGetValue(settingsClient)
log.Println("calling SettingsGetValue(foo)")
callGetValue(client)

// List all the settings.
log.Println("calling GetAll()")
callGetAll(settingsClient)
// List all settings
log.Println("calling SettingsGetAll()")
callGetAll(client)

// Write settings to file.
log.Println("calling Write()")
callWrite(settingsClient)
callWrite(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 All @@ -121,7 +118,7 @@ func main() {

// We set up the proxy and then run the update to verify that the proxy settings are currently used
log.Println("calling setProxy")
callSetProxy(settingsClient)
callSetProxy(client)

// With a brand new instance, the first operation should always be updating
// the index.
Expand Down Expand Up @@ -247,22 +244,21 @@ func callVersion(client rpc.ArduinoCoreServiceClient) {
log.Printf("arduino-cli version: %v", versionResp.GetVersion())
}

func callSetValue(client settings.SettingsServiceClient) {
_, err := client.SetValue(context.Background(),
&settings.SetValueRequest{
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") + `"}`,
})

if err != nil {
log.Fatalf("Error setting settings value: %s", err)

}
}

func callSetProxy(client settings.SettingsServiceClient) {
_, err := client.SetValue(context.Background(),
&settings.SetValueRequest{
func callSetProxy(client rpc.ArduinoCoreServiceClient) {
_, err := client.SettingsSetValue(context.Background(),
&rpc.SettingsSetValueRequest{
Key: "network.proxy",
JsonData: `"http://localhost:3128"`,
})
Expand All @@ -272,9 +268,9 @@ func callSetProxy(client settings.SettingsServiceClient) {
}
}

func callUnsetProxy(client settings.SettingsServiceClient) {
_, err := client.SetValue(context.Background(),
&settings.SetValueRequest{
func callUnsetProxy(client rpc.ArduinoCoreServiceClient) {
_, err := client.SettingsSetValue(context.Background(),
&rpc.SettingsSetValueRequest{
Key: "network.proxy",
JsonData: `""`,
})
Expand All @@ -284,9 +280,9 @@ func callUnsetProxy(client settings.SettingsServiceClient) {
}
}

func callMerge(client settings.SettingsServiceClient, jsonData string) {
_, err := client.Merge(context.Background(),
&settings.MergeRequest{
func callMerge(client rpc.ArduinoCoreServiceClient, jsonData string) {
_, err := client.SettingsMerge(context.Background(),
&rpc.SettingsMergeRequest{
JsonData: jsonData,
})

Expand All @@ -295,9 +291,9 @@ func callMerge(client settings.SettingsServiceClient, jsonData string) {
}
}

func callGetValue(client settings.SettingsServiceClient) {
getValueResp, err := client.GetValue(context.Background(),
&settings.GetValueRequest{
func callGetValue(client rpc.ArduinoCoreServiceClient) {
getValueResp, err := client.SettingsGetValue(context.Background(),
&rpc.SettingsGetValueRequest{
Key: "foo",
})

Expand All @@ -308,8 +304,8 @@ func callGetValue(client settings.SettingsServiceClient) {
log.Printf("Value: %s: %s", getValueResp.GetKey(), getValueResp.GetJsonData())
}

func callGetAll(client settings.SettingsServiceClient) {
getAllResp, err := client.GetAll(context.Background(), &settings.GetAllRequest{})
func callGetAll(client rpc.ArduinoCoreServiceClient) {
getAllResp, err := client.SettingsGetAll(context.Background(), &rpc.SettingsGetAllRequest{})

if err != nil {
log.Fatalf("Error getting settings: %s", err)
Expand All @@ -318,10 +314,10 @@ func callGetAll(client settings.SettingsServiceClient) {
log.Printf("Settings: %s", getAllResp.GetJsonData())
}

func callWrite(client settings.SettingsServiceClient) {
_, err := client.Write(context.Background(),
&settings.WriteRequest{
FilePath: path.Join(dataDir, "written-settings.yml"),
func callWrite(client rpc.ArduinoCoreServiceClient) {
_, err := client.SettingsWrite(context.Background(),
&rpc.SettingsWriteRequest{
FilePath: path.Join(dataDir, "written-rpc.Settingsyml"),
})

if err != nil {
Expand Down
43 changes: 19 additions & 24 deletions commands/daemon/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,15 @@ import (
"strings"

"github.com/arduino/arduino-cli/configuration"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/settings/v1"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
)

// SettingsService implements the `Settings` service
type SettingsService struct {
rpc.UnimplementedSettingsServiceServer
}

// GetAll returns a message with a string field containing all the settings
// SettingsGetAll returns a message with a string field containing all the settings
// currently in use, marshalled in JSON format.
func (s *SettingsService) GetAll(ctx context.Context, req *rpc.GetAllRequest) (*rpc.GetAllResponse, error) {
func (s *ArduinoCoreServerImpl) SettingsGetAll(ctx context.Context, req *rpc.SettingsGetAllRequest) (*rpc.SettingsGetAllResponse, error) {
b, err := json.Marshal(configuration.Settings.AllSettings())
if err == nil {
return &rpc.GetAllResponse{
return &rpc.SettingsGetAllResponse{
JsonData: string(b),
}, nil
}
Expand Down Expand Up @@ -72,8 +67,8 @@ func mapper(toMap map[string]interface{}) map[string]interface{} {
return res
}

// Merge applies multiple settings values at once.
func (s *SettingsService) Merge(ctx context.Context, req *rpc.MergeRequest) (*rpc.MergeResponse, error) {
// SettingsMerge applies multiple settings values at once.
func (s *ArduinoCoreServerImpl) SettingsMerge(ctx context.Context, req *rpc.SettingsMergeRequest) (*rpc.SettingsMergeResponse, error) {
var toMerge map[string]interface{}
if err := json.Unmarshal([]byte(req.GetJsonData()), &toMerge); err != nil {
return nil, err
Expand All @@ -88,13 +83,13 @@ func (s *SettingsService) Merge(ctx context.Context, req *rpc.MergeRequest) (*rp
configuration.Settings.Set(k, v)
}

return &rpc.MergeResponse{}, nil
return &rpc.SettingsMergeResponse{}, nil
}

// GetValue returns a settings value given its key. If the key is not present
// SettingsGetValue returns a settings value given its key. If the key is not present
// an error will be returned, so that we distinguish empty settings from missing
// ones.
func (s *SettingsService) GetValue(ctx context.Context, req *rpc.GetValueRequest) (*rpc.GetValueResponse, error) {
func (s *ArduinoCoreServerImpl) SettingsGetValue(ctx context.Context, req *rpc.SettingsGetValueRequest) (*rpc.SettingsGetValueResponse, error) {
key := req.GetKey()

// Check if settings key actually existing, we don't use Viper.InConfig()
Expand All @@ -112,7 +107,7 @@ func (s *SettingsService) GetValue(ctx context.Context, req *rpc.GetValueRequest
}

b, err := json.Marshal(configuration.Settings.Get(key))
value := &rpc.GetValueResponse{}
value := &rpc.SettingsGetValueResponse{}
if err == nil {
value.Key = key
value.JsonData = string(b)
Expand All @@ -121,8 +116,8 @@ func (s *SettingsService) GetValue(ctx context.Context, req *rpc.GetValueRequest
return value, err
}

// SetValue updates or set a value for a certain key.
func (s *SettingsService) SetValue(ctx context.Context, val *rpc.SetValueRequest) (*rpc.SetValueResponse, error) {
// SettingsSetValue updates or set a value for a certain key.
func (s *ArduinoCoreServerImpl) SettingsSetValue(ctx context.Context, val *rpc.SettingsSetValueRequest) (*rpc.SettingsSetValueResponse, error) {
key := val.GetKey()
var value interface{}

Expand All @@ -131,22 +126,22 @@ func (s *SettingsService) SetValue(ctx context.Context, val *rpc.SetValueRequest
configuration.Settings.Set(key, value)
}

return &rpc.SetValueResponse{}, err
return &rpc.SettingsSetValueResponse{}, err
}

// Write to file set in request the settings currently stored in memory.
// SettingsWrite to file set in request the settings currently stored in memory.
// We don't have a Read() function, that's not necessary since we only want one config file to be used
// and that's picked up when the CLI is run as daemon, either using the default path or a custom one
// set with the --config-file flag.
func (s *SettingsService) Write(ctx context.Context, req *rpc.WriteRequest) (*rpc.WriteResponse, error) {
func (s *ArduinoCoreServerImpl) SettingsWrite(ctx context.Context, req *rpc.SettingsWriteRequest) (*rpc.SettingsWriteResponse, error) {
if err := configuration.Settings.WriteConfigAs(req.FilePath); err != nil {
return nil, err
}
return &rpc.WriteResponse{}, nil
return &rpc.SettingsWriteResponse{}, nil
}

// Delete removes a key from the config file
func (s *SettingsService) Delete(ctx context.Context, req *rpc.DeleteRequest) (*rpc.DeleteResponse, error) {
// SettingsDelete removes a key from the config file
func (s *ArduinoCoreServerImpl) SettingsDelete(ctx context.Context, req *rpc.SettingsDeleteRequest) (*rpc.SettingsDeleteResponse, error) {
toDelete := req.GetKey()

// Check if settings key actually existing, we don't use Viper.InConfig()
Expand Down Expand Up @@ -175,5 +170,5 @@ func (s *SettingsService) Delete(ctx context.Context, req *rpc.DeleteRequest) (*
updatedSettings.SetConfigFile(configPath)
configuration.Settings = updatedSettings

return &rpc.DeleteResponse{}, nil
return &rpc.SettingsDeleteResponse{}, nil
}

0 comments on commit 50a8bf5

Please sign in to comment.