Skip to content

Commit d7a6bb0

Browse files
committed
Add gRPC interface function to write settings to file
1 parent c6be6fa commit d7a6bb0

File tree

4 files changed

+244
-34
lines changed

4 files changed

+244
-34
lines changed

commands/daemon/settings.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,14 @@ func (s *SettingsService) SetValue(ctx context.Context, val *rpc.Value) (*rpc.Se
8686

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

commands/daemon/settings_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
"github.com/arduino/arduino-cli/configuration"
2525
rpc "github.com/arduino/arduino-cli/rpc/settings"
26+
"github.com/arduino/go-paths-helper"
2627
"github.com/stretchr/testify/require"
2728
)
2829

@@ -80,3 +81,29 @@ func TestSetValue(t *testing.T) {
8081
require.Nil(t, err)
8182
require.Equal(t, "bar", configuration.Settings.GetString("foo"))
8283
}
84+
85+
func TestWrite(t *testing.T) {
86+
// Writes some settings
87+
val := &rpc.Value{
88+
Key: "foo",
89+
JsonData: `"bar"`,
90+
}
91+
_, err := svc.SetValue(context.Background(), val)
92+
require.NoError(t, err)
93+
94+
tempDir := paths.TempDir()
95+
testFolder, _ := tempDir.MkTempDir("testdata")
96+
97+
// Verifies config files doesn't exist
98+
configFile := testFolder.Join("arduino-cli.yml")
99+
require.True(t, configFile.NotExist())
100+
101+
_, err = svc.Write(context.Background(), &rpc.WriteRequest{
102+
FilePath: configFile.String(),
103+
})
104+
require.NoError(t, err)
105+
106+
// Verifies config file is created.
107+
// We don't verify the content since we expect config library, Viper, to work
108+
require.True(t, configFile.Exist())
109+
}

rpc/settings/settings.pb.go

Lines changed: 195 additions & 34 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rpc/settings/settings.proto

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ service Settings {
3030
rpc GetValue(GetValueRequest) returns (Value);
3131
// Set the value of a specific setting.
3232
rpc SetValue(Value) returns (SetValueResponse);
33+
// Writes to file settings currently stored in memory
34+
rpc Write(WriteRequest) returns (WriteResponse);
3335
}
3436

3537
message RawData {
@@ -51,3 +53,12 @@ message GetValueRequest {
5153
}
5254
message MergeResponse {}
5355
message SetValueResponse {}
56+
57+
message WriteRequest {
58+
// Path to settings file (e.g. /path/to/arduino-cli.yaml)
59+
string filePath = 1;
60+
}
61+
62+
message WriteResponse {
63+
// TODO
64+
}

0 commit comments

Comments
 (0)