8000 [BREAKING] gRPC / golang Configuration API refactoring by cmaglie · Pull Request #2565 · arduino/arduino-cli · GitHub
[go: up one dir, main page]

Skip to content

[BREAKING] gRPC / golang Configuration API refactoring #2565

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 56 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
8c07823
Moved a batch of function from commands/* subpackage to commands
cmaglie Mar 13, 2024
3d8f50a
Moved a batch of function from commands/* subpackage to commands (par…
cmaglie Mar 13, 2024
3ff8e09
Moved a batch of function from commands/* subpackage to commands (par…
cmaglie Mar 13, 2024
8615985
Moved a batch of function from commands/* subpackage to commands (par…
cmaglie Mar 13, 2024
61e5c46
Moved a batch of function from commands/* subpackage to commands (par…
cmaglie Mar 13, 2024
9e75b4d
Removed unused parameters
cmaglie Mar 13, 2024
f1a015c
Removed unused file
cmaglie Mar 13, 2024
c4ca852
Removed ToRPCStatus in favor of standardized GRPCStatus() interface
cmaglie Mar 13, 2024
23ccd43
Inlining methods in ArduinoCoreServiceImpl (part 1: BoardListAll)
cmaglie Mar 13, 2024
53f03e5
Inlining methods in ArduinoCoreServiceImpl (part 2: BoardDetails)
cmaglie Mar 13, 2024
0df2149
Inlining methods in ArduinoCoreServiceImpl (part 3: BoardList)
cmaglie Mar 13, 2024
f65eff2
Inlining methods in ArduinoCoreServiceImpl (part 4: BoardListWatch)
cmaglie Mar 14, 2024
06d6842
Inlining methods in ArduinoCoreServiceImpl (part 5: Create and Destroy)
cmaglie Mar 14, 2024
8bd0eef
Follow convetion where context must be the first argument
cmaglie Mar 14, 2024
a81069d
Inlining methods in ArduinoCoreServiceImpl (part 6: Init)
cmaglie Mar 14, 2024
917dcc5
Inlining methods in ArduinoCoreServiceImpl (part 7: UpdateIndex)
cmaglie Mar 14, 2024
bb815cf
Inlining methods in ArduinoCoreServiceImpl (part 8: Monitor)
cmaglie Mar 15, 2024
0e434f2
Inlining methods in ArduinoCoreServiceImpl (part 9: Compile)
cmaglie Mar 18, 2024
12cf52e
Inlining methods in ArduinoCoreServiceImpl (part 10: PlatformInstall,…
cmaglie Mar 18, 2024
cfe4b27
Inlining methods in ArduinoCoreServiceImpl (part 11: LibraryDownload,…
cmaglie Mar 18, 2024
a3800fa
Inlining methods in ArduinoCoreServiceImpl (part 12: UpdateLibrariesI…
cmaglie Mar 18, 2024
738ad7c
Inlining methods in ArduinoCoreServiceImpl (part 13: NewSketch, LoadS…
cmaglie Mar 18, 2024
0442319
Inlining methods in ArduinoCoreServiceImpl (part 14: Upload, UploadUs…
cmaglie Mar 18, 2024
65ecbaf
Inlining methods in ArduinoCoreServiceImpl (part 15: SupportedUserFie…
cmaglie Mar 18, 2024
0bdc07a
Inlining methods in ArduinoCoreServiceImpl (part 16: CleanDownloadCac…
cmaglie Mar 25, 2024
3193b7c
Made utility functions private and put them in their own file
cmaglie Mar 18, 2024
c08c59e
Run tests forcing the english language
cmaglie Mar 20, 2024
e8e1574
Use correct env var to force configuration in integration test
cmaglie Apr 19, 2024
9bab3c5
Fixed markdown link
cmaglie Apr 19, 2024
6320f6e
Always use arduino-cli in user-agent string
cmaglie Apr 19, 2024
1fddba7
Removed global *viper.Viper settings instance
cmaglie Mar 20, 2024
4c50868
Implementation of Viper replacement library
cmaglie Apr 17, 2024
0453677
Use custom type to avoid context-values collisions
cmaglie Apr 22, 2024
61b66a7
Fixed test
cmaglie Apr 19, 2024
087a6f5
Fixed FindConfigFileInArgsFallbackOnEnv unit test
cmaglie Apr 19, 2024
9cd9c97
Fixed TestUserAgentHeader and TestProxy
cmaglie Apr 19, 2024
384c30c
Improved error message
cmaglie Apr 21, 2024
6aa742f
Config set adds only unique values
cmaglie Apr 21, 2024
0aafda3
Fixed initialization sequence for 'config init' command
cmaglie Apr 21, 2024
921fa61
Config search now looks into ARDUINO_DIRECTORIES_DATA env too
cmaglie Apr 21, 2024
ad8dbd9
Fixed all config* integration test
cmaglie Apr 21, 2024
4061131
Added missing type schema
cmaglie Apr 21, 2024
fa835de
Added docs
cmaglie Apr 23, 2024
72e05ee
Updated documentation about configuration file format
cmaglie Apr 29, 2024
89a79cb
Fixed a small imperfection in test
cmaglie Apr 30, 2024
240c8b3
Pass context in upload and burn-bootloader
cmaglie Apr 30, 2024
24ac040
Pass down context in all places where it's needed
cmaglie Apr 30, 2024
6cf4eb7
Removed unused parameter
cmaglie Apr 30, 2024
27e9c3e
Tolerate errors in invalid config files, and report them as warnings
cmaglie May 2, 2024
7658db6
Improved handling of incomplete configs array
cmaglie May 2, 2024
a58c350
Disable logging until it is setup in the arduino-cli pre-run
cmaglie May 6, 2024
9585728
Show config-load warning after feedback package initialization
cmaglie May 6, 2024
b279d4b
Fixed 'feedback' package initialization order
cmaglie May 6, 2024
278a331
Applied changes from code review
cmaglie May 6, 2024
c355478
Updated client_example
cmaglie May 6, 2024
cec1fdb
Improved array handling in configmap, and unit-tests
cmaglie May 6, 2024
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
Prev Previous commit
Next Next commit
Tolerate errors in invalid config files, and report them as warnings
  • Loading branch information
cmaglie committed May 6, 2024
commit 27e9c3e4dcb31961ca60911032b89bb90c59a642
14 changes: 11 additions & 3 deletions commands/service_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ import (
"reflect"

"github.com/arduino/arduino-cli/commands/cmderrors"
f "github.com/arduino/arduino-cli/internal/algorithms"
"github.com/arduino/arduino-cli/internal/cli/configuration"
"github.com/arduino/arduino-cli/internal/go-configmap"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
"google.golang.org/protobuf/proto"
"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -187,23 +189,29 @@ func (s *arduinoCoreServerImpl) ConfigurationSave(ctx context.Context, req *rpc.

// SettingsReadFromFile read settings from a YAML file and replace the settings currently stored in memory.
func (s *arduinoCoreServerImpl) ConfigurationOpen(ctx context.Context, req *rpc.ConfigurationOpenRequest) (*rpc.ConfigurationOpenResponse, error) {
warnings := []string{}

switch req.GetSettingsFormat() {
case "yaml":
err := yaml.Unmarshal([]byte(req.GetEncodedSettings()), s.settings)
if err != nil {
if errs, ok := err.(*configmap.UnmarshalErrors); ok {
warnings = f.Map(errs.WrappedErrors(), (error).Error)
} else if err != nil {
return nil, fmt.Errorf("error unmarshalling settings: %v", err)
}
case "json":
err := json.Unmarshal([]byte(req.GetEncodedSettings()), s.settings)
if err != nil {
if errs, ok := err.(*configmap.UnmarshalErrors); ok {
warnings = f.Map(errs.WrappedErrors(), (error).Error)
} else if err != nil {
return nil, fmt.Errorf("error unmarshalling settings: %v", err)
}
default:
return nil, &cmderrors.InvalidArgumentError{Message: fmt.Sprintf("unsupported format: %s", req.GetSettingsFormat())}
}

configuration.InjectEnvVars(s.settings)
return &rpc.ConfigurationOpenResponse{}, nil
return &rpc.ConfigurationOpenResponse{Warnings: warnings}, nil
}

// SettingsEnumerate returns the list of all the settings keys.
Expand Down
54 changes: 54 additions & 0 deletions internal/go-configmap/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// This file is part of arduino-cli.
//
// Copyright 2024 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 license@arduino.cc.

package configmap

import "strings"

// UnmarshalErrors is a collection of errors that occurred during unmarshalling.
// Do not return this type directly, but use its result() method instead.
type UnmarshalErrors struct {
wrapped []error
}

func (e *UnmarshalErrors) append(err error) {
e.wrapped = append(e.wrapped, err)
}

func (e *UnmarshalErrors) result() error {
if len(e.wrapped) == 0 {
return nil
}
return e
}

func (e *UnmarshalErrors) Error() string {
if len(e.wrapped) == 1 {
return e.wrapped[0].Error()
}
errs := []string{"multiple errors:"}
for _, err := range e.wrapped {
errs = append(errs, "- "+err.Error())
}
return strings.Join(errs, "\n")
}

// WrappedErrors returns the list of errors that occurred during unmarshalling.
func (e *UnmarshalErrors) WrappedErrors() []error {
if e == nil {
return nil
}
return e.wrapped
}
5 changes: 3 additions & 2 deletions internal/go-configmap/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ func (c *Map) UnmarshalYAML(node *yaml.Node) error {
return err
}

errs := &UnmarshalErrors{}
for k, v := range flattenMap(in) {
if err := c.Set(k, v); err != nil {
return err
errs.append(err)
}
}
return nil
return errs.result()
}
27 changes: 27 additions & 0 deletions internal/integrationtest/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package config_test

import (
"path/filepath"
"strings"
"testing"

"github.com/arduino/arduino-cli/internal/integrationtest"
Expand Down Expand Up @@ -867,3 +868,29 @@ func TestInitializationOrderOfConfigThroughFlagAndEnv(t *testing.T) {
require.NoError(t, err)
requirejson.Contains(t, stdout, `{"config":{ "locale": "test" }}`)
}

func TestConfigLoadWithUnknownOrInvalidKeys(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()

tmp := t.TempDir()
unkwnownConfig := paths.New(filepath.Join(tmp, "unknown.yaml"))
unkwnownConfig.WriteFile([]byte(`
unk: "test"
build.unk: 123
`))
t.Cleanup(func() { unkwnownConfig.Remove() })

// Run "config get" with a configuration containing an unknown key
out, _, err := cli.Run("config", "get", "locale", "--config-file", unkwnownConfig.String())
require.NoError(t, err)
require.Equal(t, "en", strings.TrimSpace(string(out)))

// Run "config get" with a configuration containing an invalid key
invalidConfig := paths.New(filepath.Join(tmp, "invalid.yaml"))
invalidConfig.WriteFile([]byte(`locale: 123`))
t.Cleanup(func() { invalidConfig.Remove() })
out, _, err = cli.Run("config", "get", "locale", "--config-file", invalidConfig.String())
require.NoError(t, err)
require.Equal(t, "en", strings.TrimSpace(string(out)))
}
6 changes: 5 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,12 @@ func main() {
} else if !os.IsNotExist(err) {
feedback.FatalError(fmt.Errorf("couldn't read configuration file: %w", err), feedback.ErrGeneric)
}
if _, err := srv.ConfigurationOpen(ctx, openReq); err != nil {
if resp, err := srv.ConfigurationOpen(ctx, openReq); err != nil {
feedback.FatalError(fmt.Errorf("couldn't load configuration: %w", err), feedback.ErrGeneric)
} else if warnings := resp.GetWarnings(); len(warnings) > 0 {
for _, warning := range warnings {
feedback.Warning(warning)
}
}

// Get the current settings from the server
Expand Down
87 changes: 50 additions & 37 deletions rpc/cc/arduino/cli/commands/v1/settings.pb.go

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

6 changes: 5 additions & 1 deletion rpc/cc/arduino/cli/commands/v1/settings.proto
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,11 @@ message ConfigurationOpenRequest {
string settings_format = 2;
}

message ConfigurationOpenResponse {}
message ConfigurationOpenResponse {
// Warnings that occurred while opening the configuration (e.g. unknown keys,
// or invalid values)
repeated string warnings = 1;
}

message SettingsGetValueRequest {
// The key to get
Expand Down
0