Skip to content

Commit 00693ba

Browse files
amrdbpashagolub
andauthored
[-] fix duplicate source names in YAML configs, fixes #630 (#633)
* [+] fix duplicate names in source file or source directory error handling * [+] add `Sources.Validate()` to check correctness of sources imported --------- Co-authored-by: Pavlo Golub <[email protected]>
1 parent 07c050c commit 00693ba

File tree

3 files changed

+61
-4
lines changed

3 files changed

+61
-4
lines changed

internal/sources/types.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package sources
22

33
import (
44
"context"
5+
"fmt"
56
"maps"
67
"reflect"
78
"slices"
@@ -65,6 +66,20 @@ type (
6566
Sources []Source
6667
)
6768

69+
func (srcs Sources) Validate() (Sources, error) {
70+
names := map[string]any{}
71+
for _, src := range srcs {
72+
if _, ok := names[src.Name]; ok {
73+
return nil, fmt.Errorf("duplicate source with name '%s' found", src.Name)
74+
}
75+
names[src.Name] = nil
76+
if src.Kind == "" {
77+
src.Kind = SourcePostgres
78+
}
79+
}
80+
return srcs, nil
81+
}
82+
6883
func (s *Source) GetDatabaseName() string {
6984
if с, err := pgx.ParseConfig(s.ConnStr); err == nil {
7085
return с.Database

internal/sources/yaml.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func (fcr *fileSourcesReaderWriter) GetSources() (dbs Sources, err error) {
8181
if err != nil {
8282
return nil, err
8383
}
84-
return
84+
return dbs.Validate()
8585
}
8686

8787
func (fcr *fileSourcesReaderWriter) getSources(configFilePath string) (dbs Sources, err error) {
@@ -94,9 +94,6 @@ func (fcr *fileSourcesReaderWriter) getSources(configFilePath string) (dbs Sourc
9494
return
9595
}
9696
for _, v := range c {
97-
if v.Kind == "" {
98-
v.Kind = SourcePostgres
99-
}
10097
dbs = append(dbs, fcr.expandEnvVars(v))
10198
}
10299
return

internal/sources/yaml_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,51 @@ func TestYAMLGetMonitoredDatabases(t *testing.T) {
7272
a.Error(err)
7373
a.Nil(dbs)
7474
})
75+
76+
t.Run("duplicate in single file", func(t *testing.T) {
77+
tmpFile := filepath.Join(t.TempDir(), "duplicate.yaml")
78+
yamlContent := `
79+
- name: test1
80+
conn_str: postgresql://localhost/test1
81+
- name: test2
82+
conn_str: postgresql://localhost/test2
83+
- name: test1
84+
conn_str: postgresql://localhost/test1_duplicate
85+
`
86+
err := os.WriteFile(tmpFile, []byte(yamlContent), 0644)
87+
a.NoError(err)
88+
yamlrw, err := sources.NewYAMLSourcesReaderWriter(ctx, tmpFile)
89+
a.NoError(err)
90+
91+
dbs, err := yamlrw.GetSources()
92+
a.Error(err)
93+
a.Nil(dbs)
94+
})
95+
96+
t.Run("duplicates across files", func(t *testing.T) {
97+
tmpDir := t.TempDir()
98+
yamlContent1 := `
99+
- name: test1
100+
conn_str: postgresql://localhost/test1
101+
- name: test2
102+
conn_str: postgresql://localhost/test2
103+
`
104+
err := os.WriteFile(filepath.Join(tmpDir, "sources1.yaml"), []byte(yamlContent1), 0644)
105+
a.NoError(err)
106+
107+
yamlContent2 := `
108+
- name: test1
109+
conn_str: postgresql://localhost/test1_duplicate
110+
`
111+
err = os.WriteFile(filepath.Join(tmpDir, "sources2.yaml"), []byte(yamlContent2), 0644)
112+
a.NoError(err)
113+
yamlrw, err := sources.NewYAMLSourcesReaderWriter(ctx, tmpDir)
114+
a.NoError(err)
115+
116+
dbs, err := yamlrw.GetSources()
117+
a.Error(err)
118+
a.Nil(dbs)
119+
})
75120
}
76121

77122
func TestYAMLDeleteDatabase(t *testing.T) {

0 commit comments

Comments
 (0)