Skip to content

Commit 48ca919

Browse files
authored
feat: add registry cleaning (#201)
1 parent 9f25c80 commit 48ca919

2 files changed

Lines changed: 80 additions & 0 deletions

File tree

internal/command/updater.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ func (cmd Updater) Run() error {
5252
return err
5353
}
5454

55+
if cmd.registry != nil {
56+
validKeys := validSourceKeys(configurations)
57+
for key := range cmd.registry.Sources {
58+
if !validKeys[key] {
59+
delete(cmd.registry.Sources, key)
60+
fmt.Fprintf(cmd.Logger, "Removing stale registry entry: %s\n", key)
61+
}
62+
}
63+
}
64+
5565
tmpDir, err := os.MkdirTemp("", "output")
5666
if err != nil {
5767
return fmt.Errorf("failed to create temp dir: %w", err)
@@ -154,6 +164,14 @@ func splitConfigurations(configurations []configuration.Configuration) []configu
154164
return updated
155165
}
156166

167+
func validSourceKeys(configs []configuration.Configuration) map[string]bool {
168+
valid := make(map[string]bool)
169+
for _, cfg := range splitConfigurations(configs) {
170+
valid[cfg.Name] = true
171+
}
172+
return valid
173+
}
174+
157175
// merge will move created files in generated into the schema repository,
158176
// and will attempt to write a file to schema repository if moving it fails
159177
func merge(generatedRepository, schemaRepository string) error {

internal/command/updater_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,33 @@ func TestSplittingConfiguration(t *testing.T) {
100100
}
101101
}
102102

103+
func TestValidSourceKeys(t *testing.T) {
104+
assert.Empty(t, validSourceKeys(nil))
105+
assert.Empty(t, validSourceKeys([]configuration.Configuration{}))
106+
107+
configs := []configuration.Configuration{{Name: "foo"}}
108+
keys := validSourceKeys(configs)
109+
assert.True(t, keys["foo"])
110+
assert.Len(t, keys, 1)
111+
112+
configs = []configuration.Configuration{{Name: "bar", Entries: []string{"a", "b"}}}
113+
keys = validSourceKeys(configs)
114+
assert.True(t, keys["bar.a"])
115+
assert.True(t, keys["bar.b"])
116+
assert.Len(t, keys, 2)
117+
118+
configs = []configuration.Configuration{
119+
{Name: "single"},
120+
{Name: "multi", Entries: []string{"x", "y", "z"}},
121+
}
122+
keys = validSourceKeys(configs)
123+
assert.True(t, keys["single"])
124+
assert.True(t, keys["multi.x"])
125+
assert.True(t, keys["multi.y"])
126+
assert.True(t, keys["multi.z"])
127+
assert.Len(t, keys, 4)
128+
}
129+
103130
func TestReadConfiguration(t *testing.T) {
104131
_, err := readConfiguration("testdata/does-not.exist")
105132
assert.NotNil(t, err)
@@ -221,6 +248,41 @@ func TestRunWithRegistrySavesUpdates(t *testing.T) {
221248
assert.Contains(t, string(content), "1.0.0")
222249
}
223250

251+
func TestRunWithRegistryRemovesStaleEntries(t *testing.T) {
252+
server, config, tmpDir := setup(t)
253+
defer server.Close()
254+
255+
registryPath := path.Join(tmpDir, "registry.yaml")
256+
initialContent := `sources:
257+
http:
258+
kind: http
259+
version: 1.0.0
260+
lastUpdated: "2026-01-01T00:00:00Z"
261+
stale1:
262+
kind: helm
263+
version: v1.0.0
264+
lastUpdated: "2026-01-01T00:00:00Z"
265+
stale2:
266+
kind: git
267+
version: v1.0.0
268+
lastUpdated: "2026-01-01T00:00:00Z"
269+
`
270+
os.WriteFile(registryPath, []byte(initialContent), 0664)
271+
272+
updater := NewUpdater(config, tmpDir, tmpDir, registryPath, bytes.NewBuffer([]byte{}), nil)
273+
274+
err := updater.Run()
275+
assert.Nil(t, err)
276+
277+
content, err := os.ReadFile(registryPath)
278+
assert.Nil(t, err)
279+
280+
assert.Contains(t, string(content), "http")
281+
assert.Contains(t, string(content), "1.0.0")
282+
assert.NotContains(t, string(content), "stale1")
283+
assert.NotContains(t, string(content), "stale2")
284+
}
285+
224286
// Test is skipped during unit testing and meant for step-debugging a local check
225287
func TestCheckLocal(t *testing.T) {
226288
output := "../../build/ephemeral/schema"

0 commit comments

Comments
 (0)