-
-
Notifications
You must be signed in to change notification settings - Fork 377
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add gRPC interface to CLI settings (#521)
* define proto messages and service * run protoc on settings interface, regenerate code * add service implementation and tests * remove test leftovers
- Loading branch information
Massimiliano Pippi
authored
Dec 19, 2019
1 parent
d474176
commit 23e2a9d
Showing
17 changed files
with
1,015 additions
and
307 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// This file is part of arduino-cli. | ||
// | ||
// Copyright 2019 ARDUINO SA (http://www.arduino.cc/) | ||
// | ||
// This software is released under the GNU General Public License version 3, | ||
// which covers the main part of arduino-cli. | ||
// The terms of this license can be found at: | ||
// https://www.gnu.org/licenses/gpl-3.0.en.html | ||
// | ||
// You can be released from the requirements of the above licenses by purchasing | ||
// a commercial license. Buying such a license is mandatory if you want to | ||
// modify or otherwise use the software for commercial activities involving the | ||
// Arduino software without disclosing the source code of your own applications. | ||
// To purchase a commercial license, send an email to [email protected]. | ||
|
||
package daemon | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
|
||
rpc "github.com/arduino/arduino-cli/rpc/settings" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
// SettingsService implements the `Settings` service | ||
type SettingsService struct{} | ||
|
||
// GetAll 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.RawData, error) { | ||
b, err := json.Marshal(viper.AllSettings()) | ||
if err == nil { | ||
return &rpc.RawData{ | ||
JsonData: string(b), | ||
}, nil | ||
} | ||
|
||
return nil, err | ||
} | ||
|
||
// Merge applies multiple settings values at once. | ||
func (s *SettingsService) Merge(ctx context.Context, req *rpc.RawData) (*rpc.MergeResponse, error) { | ||
var toMerge map[string]interface{} | ||
if err := json.Unmarshal([]byte(req.GetJsonData()), &toMerge); err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := viper.MergeConfigMap(toMerge); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &rpc.MergeResponse{}, nil | ||
} | ||
|
||
// GetValue 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.Value, error) { | ||
key := req.GetKey() | ||
value := &rpc.Value{} | ||
|
||
fmt.Println(viper.AllKeys()) | ||
|
||
if !viper.InConfig(key) { | ||
return nil, errors.New("key not found in settings") | ||
} | ||
|
||
b, err := json.Marshal(viper.Get(key)) | ||
if err == nil { | ||
value.Key = key | ||
value.JsonData = string(b) | ||
} | ||
|
||
return value, err | ||
} | ||
|
||
// SetValue updates or set a value for a certain key. | ||
func (s *SettingsService) SetValue(ctx context.Context, val *rpc.Value) (*rpc.SetValueResponse, error) { | ||
key := val.GetKey() | ||
var value interface{} | ||
|
||
err := json.Unmarshal([]byte(val.GetJsonData()), &value) | ||
if err == nil { | ||
viper.Set(key, value) | ||
} | ||
|
||
return &rpc.SetValueResponse{}, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// This file is part of arduino-cli. | ||
// | ||
// Copyright 2019 ARDUINO SA (http://www.arduino.cc/) | ||
// | ||
// This software is released under the GNU General Public License version 3, | ||
// which covers the main part of arduino-cli. | ||
// The terms of this license can be found at: | ||
// https://www.gnu.org/licenses/gpl-3.0.en.html | ||
// | ||
// You can be released from the requirements of the above licenses by purchasing | ||
// a commercial license. Buying such a license is mandatory if you want to | ||
// modify or otherwise use the software for commercial activities involving the | ||
// Arduino software without disclosing the source code of your own applications. | ||
// To purchase a commercial license, send an email to [email protected]. | ||
|
||
package daemon | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/spf13/viper" | ||
|
||
"github.com/arduino/arduino-cli/configuration" | ||
rpc "github.com/arduino/arduino-cli/rpc/settings" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
var svc = SettingsService{} | ||
|
||
func init() { | ||
configuration.Init("testdata") | ||
} | ||
|
||
func reset() { | ||
viper.Reset() | ||
configuration.Init("testdata") | ||
} | ||
|
||
func TestGetAll(t *testing.T) { | ||
resp, err := svc.GetAll(context.Background(), &rpc.GetAllRequest{}) | ||
require.Nil(t, err) | ||
|
||
content, err := json.Marshal(viper.AllSettings()) | ||
require.Nil(t, err) | ||
|
||
require.Equal(t, string(content), resp.GetJsonData()) | ||
} | ||
|
||
func TestMerge(t *testing.T) { | ||
bulkSettings := `{"foo": "bar", "daemon":{"port":"420"}}` | ||
_, err := svc.Merge(context.Background(), &rpc.RawData{JsonData: bulkSettings}) | ||
require.Nil(t, err) | ||
|
||
fmt.Println(viper.AllSettings()) | ||
require.Equal(t, "420", viper.GetString("daemon.port")) | ||
require.Equal(t, "bar", viper.GetString("foo")) | ||
|
||
reset() | ||
} | ||
|
||
func TestGetValue(t *testing.T) { | ||
key := &rpc.GetValueRequest{Key: "daemon"} | ||
resp, err := svc.GetValue(context.Background(), key) | ||
require.Nil(t, err) | ||
require.Equal(t, `{"port":"50051"}`, resp.GetJsonData()) | ||
} | ||
|
||
func TestGetValueNotFound(t *testing.T) { | ||
key := &rpc.GetValueRequest{Key: "DOESNTEXIST"} | ||
_, err := svc.GetValue(context.Background(), key) | ||
require.NotNil(t, err) | ||
require.Equal(t, `key not found in settings`, err.Error()) | ||
} | ||
|
||
func TestSetValue(t *testing.T) { | ||
val := &rpc.Value{ | ||
Key: "foo", | ||
JsonData: `"bar"`, | ||
} | ||
_, err := svc.SetValue(context.Background(), val) | ||
require.Nil(t, err) | ||
require.Equal(t, "bar", viper.GetString("foo")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
board_manager: | ||
additional_urls: | ||
- http://foobar.com | ||
- http://example.com | ||
|
||
daemon: | ||
port: "50051" | ||
|
||
directories: | ||
data: /home/massi/.arduino15 | ||
downloads: /home/massi/.arduino15/staging | ||
|
||
logging: | ||
file: "" | ||
format: text | ||
level: info |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.