Skip to content

Commit e387859

Browse files
committed
Added json output to 'core update index'
1 parent 7955539 commit e387859

File tree

4 files changed

+87
-5
lines changed

4 files changed

+87
-5
lines changed

Diff for: internal/cli/core/update_index.go

+19-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
"github.com/arduino/arduino-cli/commands"
2323
"github.com/arduino/arduino-cli/internal/cli/feedback"
24+
"github.com/arduino/arduino-cli/internal/cli/feedback/result"
2425
"github.com/arduino/arduino-cli/internal/cli/instance"
2526
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2627
"github.com/sirupsen/logrus"
@@ -42,13 +43,28 @@ func initUpdateIndexCommand() *cobra.Command {
4243
func runUpdateIndexCommand(cmd *cobra.Command, args []string) {
4344
inst := instance.CreateAndInit()
4445
logrus.Info("Executing `arduino-cli core update-index`")
45-
UpdateIndex(inst)
46+
resp := UpdateIndex(inst)
47+
48+
feedback.PrintResult(&updateIndexResult{result.NewUpdateIndexResponse_ResultResult(resp)})
4649
}
4750

4851
// UpdateIndex updates the index of platforms.
49-
func UpdateIndex(inst *rpc.Instance) {
50-
_, err := commands.UpdateIndex(context.Background(), &rpc.UpdateIndexRequest{Instance: inst}, feedback.ProgressBar())
52+
func UpdateIndex(inst *rpc.Instance) *rpc.UpdateIndexResponse_Result {
53+
res, err := commands.UpdateIndex(context.Background(), &rpc.UpdateIndexRequest{Instance: inst}, feedback.ProgressBar())
5154
if err != nil {
5255
feedback.FatalError(err, feedback.ErrGeneric)
5356
}
57+
return res
58+
}
59+
60+
type updateIndexResult struct {
61+
*result.UpdateIndexResponse_ResultResult
62+
}
63+
64+
func (r *updateIndexResult) Data() interface{} {
65+
return r
66+
}
67+
68+
func (r *updateIndexResult) String() string {
69+
return ""
5470
}

Diff for: internal/cli/feedback/result/rpc.go

+49
Original file line numberDiff line numberDiff line change
@@ -1065,3 +1065,52 @@ func NewIsDebugSupportedResponse(resp *rpc.IsDebugSupportedResponse) *IsDebugSup
10651065
DebugFQBN: resp.GetDebugFqbn(),
10661066
}
10671067
}
1068+
1069+
type UpdateIndexResponse_ResultResult struct {
1070+
UpdatedIndexes []*IndexUpdateReportResult `json:"updated_indexes,omitempty"`
1071+
}
1072+
1073+
func NewUpdateIndexResponse_ResultResult(resp *rpc.UpdateIndexResponse_Result) *UpdateIndexResponse_ResultResult {
1074+
return &UpdateIndexResponse_ResultResult{
1075+
UpdatedIndexes: f.Map(resp.GetUpdatedIndexes(), NewIndexUpdateReportResult),
1076+
}
1077+
}
1078+
1079+
type IndexUpdateReportResult struct {
1080+
IndexURL string `json:"index_url"`
1081+
Status IndexUpdateReport_Status `json:"status"`
1082+
}
1083+
1084+
func NewIndexUpdateReportResult(resp *rpc.IndexUpdateReport) *IndexUpdateReportResult {
1085+
return &IndexUpdateReportResult{
1086+
IndexURL: resp.GetIndexUrl(),
1087+
Status: NewIndexUpdateReport_Status(resp.GetStatus()),
1088+
}
1089+
}
1090+
1091+
type IndexUpdateReport_Status string
1092+
1093+
const (
1094+
IndexUpdateReport_StatusUnspecified IndexUpdateReport_Status = "unspecified"
1095+
IndexUpdateReport_StatusAlreadyUpToDate IndexUpdateReport_Status = "already-up-to-date"
1096+
IndexUpdateReport_StatusFailed IndexUpdateReport_Status = "failed"
1097+
IndexUpdateReport_StatusSkipped IndexUpdateReport_Status = "skipped"
1098+
IndexUpdateReport_StatusUpdated IndexUpdateReport_Status = "updated"
1099+
)
1100+
1101+
func NewIndexUpdateReport_Status(r rpc.IndexUpdateReport_Status) IndexUpdateReport_Status {
1102+
switch r {
1103+
case rpc.IndexUpdateReport_STATUS_UNSPECIFIED:
1104+
return IndexUpdateReport_StatusUnspecified
1105+
case rpc.IndexUpdateReport_STATUS_UPDATED:
1106+
return IndexUpdateReport_StatusUpdated
1107+
case rpc.IndexUpdateReport_STATUS_ALREADY_UP_TO_DATE:
1108+
return IndexUpdateReport_StatusAlreadyUpToDate
1109+
case rpc.IndexUpdateReport_STATUS_FAILED:
1110+
return IndexUpdateReport_StatusFailed
1111+
case rpc.IndexUpdateReport_STATUS_SKIPPED:
1112+
return IndexUpdateReport_StatusSkipped
1113+
default:
1114+
return IndexUpdateReport_StatusUnspecified
1115+
}
1116+
}

Diff for: internal/cli/feedback/result/rpc_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,14 @@ func TestAllFieldAreMapped(t *testing.T) {
221221
isDebugSupportedResponseRpc := &rpc.IsDebugSupportedResponse{}
222222
isDebugSupportedResponseResult := result.NewIsDebugSupportedResponse(isDebugSupportedResponseRpc)
223223
mustContainsAllPropertyOfRpcStruct(t, isDebugSupportedResponseRpc, isDebugSupportedResponseResult)
224+
225+
updateIndexResponse_ResultRpc := &rpc.UpdateIndexResponse_Result{}
226+
updateIndexResponse_ResultResult := result.NewUpdateIndexResponse_ResultResult(updateIndexResponse_ResultRpc)
227+
mustContainsAllPropertyOfRpcStruct(t, updateIndexResponse_ResultRpc, updateIndexResponse_ResultResult)
228+
229+
indexUpdateReportRpc := &rpc.IndexUpdateReport{}
230+
indexUpdateReportResult := result.NewIndexUpdateReportResult(indexUpdateReportRpc)
231+
mustContainsAllPropertyOfRpcStruct(t, indexUpdateReportRpc, indexUpdateReportResult)
224232
}
225233

226234
func TestEnumsMapsEveryRpcCounterpart(t *testing.T) {
@@ -251,6 +259,15 @@ func TestEnumsMapsEveryRpcCounterpart(t *testing.T) {
251259
require.Len(t, results, len(rpc.LibrarySearchStatus_name))
252260
require.True(t, isUnique(results))
253261
})
262+
t.Run("IndexUpdateReport_Status enums maps every element", func(t *testing.T) {
263+
results := make([]result.IndexUpdateReport_Status, 0, len(rpc.IndexUpdateReport_Status_name))
264+
for key := range rpc.IndexUpdateReport_Status_name {
265+
results = append(results, result.NewIndexUpdateReport_Status(rpc.IndexUpdateReport_Status(key)))
266+
}
267+
require.NotEmpty(t, results)
268+
require.Len(t, results, len(rpc.IndexUpdateReport_Status_name))
269+
require.True(t, isUnique(results))
270+
})
254271
}
255272

256273
func isUnique[T comparable](s []T) bool {

Diff for: internal/integrationtest/core/core_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -445,9 +445,9 @@ func TestCoreUpdateWithLocalUrl(t *testing.T) {
445445
testIndex = "/" + strings.ReplaceAll(testIndex, "\\", "/")
446446
}
447447

448-
stdout, _, err := cli.Run("core", "update-index", "--additional-urls=file://"+testIndex)
448+
stdout, _, err := cli.Run("core", "update-index", "--additional-urls=file://"+testIndex, "--format", "json")
449449
require.NoError(t, err)
450-
require.Contains(t, string(stdout), "Downloading index: test_index.json downloaded")
450+
requirejson.Parse(t, stdout).MustContain(`{"updated_indexes":[{"index_url":"file://` + testIndex + `","status":"skipped"}]}`)
451451
}
452452

453453
func TestCoreSearchManuallyInstalledCoresNotPrinted(t *testing.T) {

0 commit comments

Comments
 (0)