Unverified Commit f61c1bc3 authored by Cristian Maglie's avatar Cristian Maglie Committed by GitHub

[skip-changelog] Simplified getUserFields method (#1412)

parent 9cc4eea7
......@@ -20,7 +20,6 @@ import (
"fmt"
"io"
"path/filepath"
"strconv"
"strings"
"github.com/arduino/arduino-cli/arduino/cores"
......@@ -67,13 +66,8 @@ func SupportedUserFields(ctx context.Context, req *rpc.SupportedUserFieldsReques
return nil, err
}
userFields, err := getUserFields(toolID, platformRelease)
if err != nil {
return nil, err
}
return &rpc.SupportedUserFieldsResponse{
UserFields: userFields,
UserFields: getUserFields(toolID, platformRelease),
}, nil
}
......@@ -95,7 +89,7 @@ func getToolID(props *properties.Map, action, protocol string) (string, error) {
// getUserFields return all user fields supported by the tools specified.
// Returns error only in case the secret property is not a valid boolean.
func getUserFields(toolID string, platformRelease *cores.PlatformRelease) ([]*rpc.UserField, error) {
func getUserFields(toolID string, platformRelease *cores.PlatformRelease) []*rpc.UserField {
userFields := []*rpc.UserField{}
fields := platformRelease.Properties.SubTree(fmt.Sprintf("tools.%s.upload.field", toolID))
keys := fields.FirstLevelKeys()
......@@ -105,15 +99,7 @@ func getUserFields(toolID string, platformRelease *cores.PlatformRelease) ([]*rp
if len(value) > 50 {
value = fmt.Sprintf("%s…", value[:49])
}
secretProp := fmt.Sprintf("%s.secret", key)
secret, ok := fields.GetOk(secretProp)
if !ok {
secret = "false"
}
isSecret, err := strconv.ParseBool(secret)
if err != nil {
return nil, fmt.Errorf(tr("parsing %s, property is not a boolean"), fmt.Sprintf(`"tools.%s.upload.field.%s.secret"`, toolID, key))
}
isSecret := fields.GetBoolean(fmt.Sprintf("%s.secret", key))
userFields = append(userFields, &rpc.UserField{
ToolId: toolID,
Name: key,
......@@ -122,7 +108,7 @@ func getUserFields(toolID string, platformRelease *cores.PlatformRelease) ([]*rp
})
}
return userFields, nil
return userFields
}
// Upload FIXMEDOC
......
......@@ -282,8 +282,7 @@ tools.arduino_ota.upload.field.password.secret=true`))
platformRelease.Properties = props
userFields, err := getUserFields("avrdude", platformRelease)
require.NoError(t, err)
userFields := getUserFields("avrdude", platformRelease)
require.Len(t, userFields, 2)
require.Equal(t, userFields[0].ToolId, "avrdude")
require.Equal(t, userFields[0].Name, "username")
......@@ -300,17 +299,12 @@ tools.arduino_ota.upload.field.password.secret=THIS_IS_NOT_A_BOOLEAN`))
require.NoError(t, err)
platformRelease.Properties = props
userFields, err = getUserFields("arduino_ota", platformRelease)
require.Nil(t, userFields)
require.EqualError(t, err, `parsing "tools.arduino_ota.upload.field.password.secret", property is not a boolean`)
props, err = properties.LoadFromBytes([]byte(`
tools.arduino_ota.upload.field.some_field=This is a really long label that ideally must never be set by any platform
`))
require.NoError(t, err)
platformRelease.Properties = props
userFields, err = getUserFields("arduino_ota", platformRelease)
require.NoError(t, err)
userFields = getUserFields("arduino_ota", platformRelease)
require.Len(t, userFields, 1)
require.Equal(t, userFields[0].ToolId, "arduino_ota")
require.Equal(t, userFields[0].Name, "some_field")
......
This diff is collapsed.
This source diff could not be displayed because it is too large. You can view the blob instead.
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