Skip to content

Commit 3e01128

Browse files
committed
fix: return catalog list data
Using --format json or yaml would return the entire contents of all the catalogs. Instead, return the data that is returned for human readable output.
1 parent ee42c1f commit 3e01128

File tree

3 files changed

+40
-43
lines changed

3 files changed

+40
-43
lines changed

pkg/catalog_next/catalog.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ type CatalogWithDigest struct {
2525
Digest string `yaml:"digest" json:"digest"`
2626
}
2727

28+
type CatalogSummary struct {
29+
Ref string `yaml:"ref" json:"ref"`
30+
Digest string `yaml:"digest" json:"digest"`
31+
Title string `yaml:"title" json:"title"`
32+
}
33+
2834
// Source prefixes must be of the form "<prefix>:"
2935
const (
3036
SourcePrefixWorkingSet = "profile:"

pkg/catalog_next/list.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,23 @@ func List(ctx context.Context, dao db.DAO, format workingset.OutputFormat) error
2323
return nil
2424
}
2525

26-
catalogs := make([]CatalogWithDigest, len(dbCatalogs))
26+
summaries := make([]CatalogSummary, len(dbCatalogs))
2727
for i, dbCatalog := range dbCatalogs {
28-
catalogs[i] = NewFromDb(&dbCatalog)
28+
summaries[i] = CatalogSummary{
29+
Ref: dbCatalog.Ref,
30+
Digest: dbCatalog.Digest,
31+
Title: dbCatalog.Title,
32+
}
2933
}
3034

3135
var data []byte
3236
switch format {
3337
case workingset.OutputFormatHumanReadable:
34-
data = []byte(printListHumanReadable(catalogs))
38+
data = []byte(printListHumanReadable(summaries))
3539
case workingset.OutputFormatJSON:
36-
data, err = json.MarshalIndent(catalogs, "", " ")
40+
data, err = json.MarshalIndent(summaries, "", " ")
3741
case workingset.OutputFormatYAML:
38-
data, err = yaml.Marshal(catalogs)
42+
data, err = yaml.Marshal(summaries)
3943
}
4044
if err != nil {
4145
return fmt.Errorf("failed to marshal catalogs: %w", err)
@@ -46,7 +50,7 @@ func List(ctx context.Context, dao db.DAO, format workingset.OutputFormat) error
4650
return nil
4751
}
4852

49-
func printListHumanReadable(catalogs []CatalogWithDigest) string {
53+
func printListHumanReadable(catalogs []CatalogSummary) string {
5054
lines := ""
5155
for _, catalog := range catalogs {
5256
lines += fmt.Sprintf("%s\t| %s\t| %s\n", catalog.Ref, catalog.Digest, catalog.Title)

pkg/catalog_next/list_test.go

Lines changed: 24 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -138,26 +138,20 @@ func TestListJSON(t *testing.T) {
138138
})
139139

140140
// Parse JSON output
141-
var catalogs []CatalogWithDigest
141+
var catalogs []CatalogSummary
142142
err = json.Unmarshal([]byte(output), &catalogs)
143143
require.NoError(t, err)
144144
assert.Len(t, catalogs, 2)
145145

146-
// Verify first catalog
146+
// Verify first catalog (summary only)
147+
assert.Equal(t, "test/catalog4:latest", catalogs[0].Ref)
147148
assert.Equal(t, "catalog-one", catalogs[0].Title)
148-
assert.Equal(t, "source-1", catalogs[0].Source)
149-
assert.Len(t, catalogs[0].Servers, 1)
150-
assert.Equal(t, workingset.ServerTypeImage, catalogs[0].Servers[0].Type)
151-
assert.Equal(t, "test:v1", catalogs[0].Servers[0].Image)
152-
assert.Equal(t, []string{"tool1"}, catalogs[0].Servers[0].Tools)
149+
assert.NotEmpty(t, catalogs[0].Digest)
153150

154-
// Verify second catalog
151+
// Verify second catalog (summary only)
152+
assert.Equal(t, "test/catalog5:latest", catalogs[1].Ref)
155153
assert.Equal(t, "catalog-two", catalogs[1].Title)
156-
assert.Equal(t, "source-2", catalogs[1].Source)
157-
assert.Len(t, catalogs[1].Servers, 1)
158-
assert.Equal(t, workingset.ServerTypeRegistry, catalogs[1].Servers[0].Type)
159-
assert.Equal(t, "https://example.com", catalogs[1].Servers[0].Source)
160-
assert.Equal(t, []string{"tool2", "tool3"}, catalogs[1].Servers[0].Tools)
154+
assert.NotEmpty(t, catalogs[1].Digest)
161155
}
162156

163157
func TestListJSONEmpty(t *testing.T) {
@@ -170,7 +164,7 @@ func TestListJSONEmpty(t *testing.T) {
170164
})
171165

172166
// Parse JSON output
173-
var catalogs []CatalogWithDigest
167+
var catalogs []CatalogSummary
174168
err := json.Unmarshal([]byte(output), &catalogs)
175169
require.NoError(t, err)
176170
assert.Empty(t, catalogs)
@@ -206,18 +200,15 @@ func TestListYAML(t *testing.T) {
206200
})
207201

208202
// Parse YAML output
209-
var catalogs []CatalogWithDigest
203+
var catalogs []CatalogSummary
210204
err = yaml.Unmarshal([]byte(output), &catalogs)
211205
require.NoError(t, err)
212206
assert.Len(t, catalogs, 1)
213207

214-
// Verify catalog
208+
// Verify catalog (summary only)
209+
assert.Equal(t, "test/catalog6:latest", catalogs[0].Ref)
215210
assert.Equal(t, "catalog-yaml", catalogs[0].Title)
216-
assert.Equal(t, "yaml-source", catalogs[0].Source)
217-
assert.Len(t, catalogs[0].Servers, 1)
218-
assert.Equal(t, workingset.ServerTypeImage, catalogs[0].Servers[0].Type)
219-
assert.Equal(t, "test:yaml", catalogs[0].Servers[0].Image)
220-
assert.Equal(t, []string{"tool1", "tool2"}, catalogs[0].Servers[0].Tools)
211+
assert.NotEmpty(t, catalogs[0].Digest)
221212
}
222213

223214
func TestListYAMLEmpty(t *testing.T) {
@@ -230,7 +221,7 @@ func TestListYAMLEmpty(t *testing.T) {
230221
})
231222

232223
// Parse YAML output
233-
var catalogs []CatalogWithDigest
224+
var catalogs []CatalogSummary
234225
err := yaml.Unmarshal([]byte(output), &catalogs)
235226
require.NoError(t, err)
236227
assert.Empty(t, catalogs)
@@ -271,15 +262,15 @@ func TestListWithSnapshot(t *testing.T) {
271262
require.NoError(t, err)
272263
})
273264

274-
var result []CatalogWithDigest
265+
var result []CatalogSummary
275266
err = json.Unmarshal([]byte(output), &result)
276267
require.NoError(t, err)
277268
assert.Len(t, result, 1)
278269

279-
// Verify snapshot is included
280-
require.NotNil(t, result[0].Servers[0].Snapshot)
281-
assert.Equal(t, "test-server", result[0].Servers[0].Snapshot.Server.Name)
282-
assert.Equal(t, "Test description", result[0].Servers[0].Snapshot.Server.Description)
270+
// Verify only summary fields are present
271+
assert.Equal(t, "test/catalog7:latest", result[0].Ref)
272+
assert.Equal(t, "snapshot-catalog", result[0].Title)
273+
assert.NotEmpty(t, result[0].Digest)
283274
}
284275

285276
func TestListWithMultipleServers(t *testing.T) {
@@ -319,19 +310,15 @@ func TestListWithMultipleServers(t *testing.T) {
319310
require.NoError(t, err)
320311
})
321312

322-
var result []CatalogWithDigest
313+
var result []CatalogSummary
323314
err = json.Unmarshal([]byte(output), &result)
324315
require.NoError(t, err)
325316
assert.Len(t, result, 1)
326-
assert.Len(t, result[0].Servers, 3)
327317

328-
// Just verify that all three server types are present, order may vary
329-
types := make(map[workingset.ServerType]int)
330-
for _, s := range result[0].Servers {
331-
types[s.Type]++
332-
}
333-
assert.Equal(t, 2, types[workingset.ServerTypeImage])
334-
assert.Equal(t, 1, types[workingset.ServerTypeRegistry])
318+
// Verify only summary fields are present
319+
assert.Equal(t, "test/catalog8:latest", result[0].Ref)
320+
assert.Equal(t, "multi-server-catalog", result[0].Title)
321+
assert.NotEmpty(t, result[0].Digest)
335322
}
336323

337324
func TestListHumanReadableEmptyDoesNotShowInJSON(t *testing.T) {
@@ -344,7 +331,7 @@ func TestListHumanReadableEmptyDoesNotShowInJSON(t *testing.T) {
344331
require.NoError(t, err)
345332
})
346333

347-
var catalogs []CatalogWithDigest
334+
var catalogs []CatalogSummary
348335
err := json.Unmarshal([]byte(outputJSON), &catalogs)
349336
require.NoError(t, err)
350337
assert.Empty(t, catalogs)

0 commit comments

Comments
 (0)