Skip to content

Add gRPC interface function to write settings to file #1144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 20, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions commands/daemon/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,14 @@ func (s *SettingsService) SetValue(ctx context.Context, val *rpc.Value) (*rpc.Se

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
}
27 changes: 27 additions & 0 deletions commands/daemon/settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/arduino/arduino-cli/configuration"
rpc "github.com/arduino/arduino-cli/rpc/settings"
"github.com/arduino/go-paths-helper"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -80,3 +81,29 @@ func TestSetValue(t *testing.T) {
require.Nil(t, err)
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())
}
229 changes: 195 additions & 34 deletions rpc/settings/settings.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions rpc/settings/settings.proto
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ service Settings {
rpc GetValue(GetValueRequest) returns (Value);
// Set the value of a specific setting.
rpc SetValue(Value) returns (SetValueResponse);
// Writes to file settings currently stored in memory
rpc Write(WriteRequest) returns (WriteResponse);
}

message RawData {
Expand All @@ -51,3 +53,12 @@ message GetValueRequest {
}
message MergeResponse {}
message SetValueResponse {}

message WriteRequest {
// Path to settings file (e.g. /path/to/arduino-cli.yaml)
string filePath = 1;
}

message WriteResponse {
// TODO
}