Unverified Commit 6328430d authored by Silvano Cerza's avatar Silvano Cerza Committed by GitHub

Add gRPC interface function to write settings to file (#1144)

* Add gRPC interface function to write settings to file

* Add example of gRPC config write
parent c8f9efba
This diff is collapsed.
...@@ -90,6 +90,10 @@ func main() { ...@@ -90,6 +90,10 @@ func main() {
log.Println("calling GetValue(foo)") log.Println("calling GetValue(foo)")
callGetValue(settingsClient) callGetValue(settingsClient)
// Write settings to file.
log.Println("calling Write()")
callWrite(settingsClient)
// Before we can do anything with the CLI, an "instance" must be created. // 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 // We keep a reference to the created instance because we will need it to
// run subsequent commands. // run subsequent commands.
...@@ -256,6 +260,17 @@ func callGetAll(client settings.SettingsClient) { ...@@ -256,6 +260,17 @@ func callGetAll(client settings.SettingsClient) {
log.Printf("Settings: %s", getAllResp.GetJsonData()) log.Printf("Settings: %s", getAllResp.GetJsonData())
} }
func callWrite(client settings.SettingsClient) {
_, err := client.Write(context.Background(),
&settings.WriteRequest{
FilePath: path.Join(dataDir, "written-settings.yml"),
})
if err != nil {
log.Fatalf("Error writing settings: %s", err)
}
}
func initInstance(client rpc.ArduinoCoreClient) *rpc.Instance { func initInstance(client rpc.ArduinoCoreClient) *rpc.Instance {
// The configuration for this example client only contains the path to // The configuration for this example client only contains the path to
// the data folder. // the data folder.
......
...@@ -86,3 +86,14 @@ func (s *SettingsService) SetValue(ctx context.Context, val *rpc.Value) (*rpc.Se ...@@ -86,3 +86,14 @@ func (s *SettingsService) SetValue(ctx context.Context, val *rpc.Value) (*rpc.Se
return &rpc.SetValueResponse{}, err return &rpc.SetValueResponse{}, err
} }
// Write 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) {
if err := configuration.Settings.WriteConfigAs(req.FilePath); err != nil {
return nil, err
}
return &rpc.WriteResponse{}, nil
}
...@@ -23,6 +23,7 @@ import ( ...@@ -23,6 +23,7 @@ import (
"github.com/arduino/arduino-cli/configuration" "github.com/arduino/arduino-cli/configuration"
rpc "github.com/arduino/arduino-cli/rpc/settings" rpc "github.com/arduino/arduino-cli/rpc/settings"
"github.com/arduino/go-paths-helper"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
...@@ -80,3 +81,29 @@ func TestSetValue(t *testing.T) { ...@@ -80,3 +81,29 @@ func TestSetValue(t *testing.T) {
require.Nil(t, err) require.Nil(t, err)
require.Equal(t, "bar", configuration.Settings.GetString("foo")) require.Equal(t, "bar", configuration.Settings.GetString("foo"))
} }
func TestWrite(t *testing.T) {
// Writes some settings
val := &rpc.Value{
Key: "foo",
JsonData: `"bar"`,
}
_, err := svc.SetValue(context.Background(), val)
require.NoError(t, err)
tempDir := paths.TempDir()
testFolder, _ := tempDir.MkTempDir("testdata")
// Verifies config files doesn't exist
configFile := testFolder.Join("arduino-cli.yml")
require.True(t, configFile.NotExist())
_, err = svc.Write(context.Background(), &rpc.WriteRequest{
FilePath: configFile.String(),
})
require.NoError(t, err)
// Verifies config file is created.
// We don't verify the content since we expect config library, Viper, to work
require.True(t, configFile.Exist())
}
This diff is collapsed.
...@@ -30,6 +30,8 @@ service Settings { ...@@ -30,6 +30,8 @@ service Settings {
rpc GetValue(GetValueRequest) returns (Value); rpc GetValue(GetValueRequest) returns (Value);
// Set the value of a specific setting. // Set the value of a specific setting.
rpc SetValue(Value) returns (SetValueResponse); rpc SetValue(Value) returns (SetValueResponse);
// Writes to file settings currently stored in memory
rpc Write(WriteRequest) returns (WriteResponse);
} }
message RawData { message RawData {
...@@ -51,3 +53,12 @@ message GetValueRequest { ...@@ -51,3 +53,12 @@ message GetValueRequest {
} }
message MergeResponse {} message MergeResponse {}
message SetValueResponse {} message SetValueResponse {}
message WriteRequest {
// Path to settings file (e.g. /path/to/arduino-cli.yaml)
string filePath = 1;
}
message WriteResponse {
// TODO
}
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment