diff --git a/internal/ids/ids.go b/internal/ids/ids.go index b1c06e481..74df3ea7e 100644 --- a/internal/ids/ids.go +++ b/internal/ids/ids.go @@ -4,23 +4,32 @@ package ids import "fmt" // IDs is the IDs type -type IDs map[string]int +type IDs struct { + ids map[string]int + kind string +} // New IDs -func New() IDs { - return IDs(map[string]int{}) +func New(kind string) IDs { + return IDs{ + ids: map[string]int{}, + kind: kind, + } } // Inc increment the counter of the given id -func (ids IDs) Inc(id string) { - ids[id]++ +func (i IDs) Inc(id string) { + i.ids[id]++ } // Validate errors if there are any ids with counter > 1 -func (ids IDs) Validate() error { - for id, cont := range ids { - if cont > 1 { - return fmt.Errorf("found %d items with the ID '%s', please fix your config", cont, id) +func (i IDs) Validate() error { + for id, count := range i.ids { + if count > 1 { + return fmt.Errorf( + "found %d %s with the ID '%s', please fix your config", + count, i.kind, id, + ) } } return nil diff --git a/internal/ids/ids_test.go b/internal/ids/ids_test.go index 6c2642d29..67218a520 100644 --- a/internal/ids/ids_test.go +++ b/internal/ids/ids_test.go @@ -7,16 +7,16 @@ import ( ) func TestIDs(t *testing.T) { - var ids = New() + var ids = New("foos") ids.Inc("foo") ids.Inc("bar") require.NoError(t, ids.Validate()) } func TestIDsError(t *testing.T) { - var ids = New() + var ids = New("builds") ids.Inc("foo") ids.Inc("bar") ids.Inc("foo") - require.EqualError(t, ids.Validate(), "found 2 items with the ID 'foo', please fix your config") + require.EqualError(t, ids.Validate(), "found 2 builds with the ID 'foo', please fix your config") } diff --git a/internal/pipe/archive/archive.go b/internal/pipe/archive/archive.go index 70be78ec8..e8e6619c5 100644 --- a/internal/pipe/archive/archive.go +++ b/internal/pipe/archive/archive.go @@ -42,7 +42,7 @@ func (Pipe) String() string { // Default sets the pipe defaults func (Pipe) Default(ctx *context.Context) error { - var ids = ids.New() + var ids = ids.New("archives") if len(ctx.Config.Archives) == 0 { ctx.Config.Archives = append(ctx.Config.Archives, ctx.Config.Archive) if !reflect.DeepEqual(ctx.Config.Archive, config.Archive{}) { diff --git a/internal/pipe/archive/archive_test.go b/internal/pipe/archive/archive_test.go index 9987caf39..b685e8f61 100644 --- a/internal/pipe/archive/archive_test.go +++ b/internal/pipe/archive/archive_test.go @@ -678,5 +678,5 @@ func TestSeveralArchivesWithTheSameID(t *testing.T) { }, }, } - require.EqualError(t, Pipe{}.Default(ctx), "found 2 items with the ID 'a', please fix your config") + require.EqualError(t, Pipe{}.Default(ctx), "found 2 archives with the ID 'a', please fix your config") } diff --git a/internal/pipe/build/build.go b/internal/pipe/build/build.go index cd382ec64..10159274c 100644 --- a/internal/pipe/build/build.go +++ b/internal/pipe/build/build.go @@ -42,7 +42,7 @@ func (Pipe) Run(ctx *context.Context) error { // Default sets the pipe defaults func (Pipe) Default(ctx *context.Context) error { - var ids = ids.New() + var ids = ids.New("builds") for i, build := range ctx.Config.Builds { ctx.Config.Builds[i] = buildWithDefaults(ctx, build) ids.Inc(ctx.Config.Builds[i].ID) diff --git a/internal/pipe/build/build_test.go b/internal/pipe/build/build_test.go index 301e79b66..9441df6cf 100644 --- a/internal/pipe/build/build_test.go +++ b/internal/pipe/build/build_test.go @@ -253,7 +253,7 @@ func TestDefaultBuildID(t *testing.T) { }, }, } - assert.EqualError(t, Pipe{}.Default(ctx), "found 2 items with the ID 'foo', please fix your config") + assert.EqualError(t, Pipe{}.Default(ctx), "found 2 builds with the ID 'foo', please fix your config") var build = ctx.Config.Builds[0] assert.Equal(t, ctx.Config.ProjectName, build.ID) } @@ -273,7 +273,7 @@ func TestSeveralBuildsWithTheSameID(t *testing.T) { }, }, } - assert.EqualError(t, Pipe{}.Default(ctx), "found 2 items with the ID 'a', please fix your config") + assert.EqualError(t, Pipe{}.Default(ctx), "found 2 builds with the ID 'a', please fix your config") } func TestDefaultPartialBuilds(t *testing.T) { diff --git a/internal/pipe/nfpm/nfpm.go b/internal/pipe/nfpm/nfpm.go index 206c34487..78222e323 100644 --- a/internal/pipe/nfpm/nfpm.go +++ b/internal/pipe/nfpm/nfpm.go @@ -41,7 +41,7 @@ func (Pipe) Default(ctx *context.Context) error { deprecate.Notice("nfpm") } } - var ids = ids.New() + var ids = ids.New("nfpms") for i := range ctx.Config.NFPMs { var fpm = &ctx.Config.NFPMs[i] if fpm.ID == "" { diff --git a/internal/pipe/nfpm/nfpm_test.go b/internal/pipe/nfpm/nfpm_test.go index fb94a9645..88cc8a05b 100644 --- a/internal/pipe/nfpm/nfpm_test.go +++ b/internal/pipe/nfpm/nfpm_test.go @@ -356,5 +356,5 @@ func TestSeveralNFPMsWithTheSameID(t *testing.T) { }, }, } - require.EqualError(t, Pipe{}.Default(ctx), "found 2 items with the ID 'a', please fix your config") + require.EqualError(t, Pipe{}.Default(ctx), "found 2 nfpms with the ID 'a', please fix your config") } diff --git a/internal/pipe/snapcraft/snapcraft.go b/internal/pipe/snapcraft/snapcraft.go index 01f653a6e..f3f357a35 100644 --- a/internal/pipe/snapcraft/snapcraft.go +++ b/internal/pipe/snapcraft/snapcraft.go @@ -73,7 +73,7 @@ func (Pipe) Default(ctx *context.Context) error { deprecate.Notice("snapcraft") } } - var ids = ids.New() + var ids = ids.New("snapcrafts") for i := range ctx.Config.Snapcrafts { var snap = &ctx.Config.Snapcrafts[i] if snap.NameTemplate == "" { diff --git a/internal/pipe/snapcraft/snapcraft_test.go b/internal/pipe/snapcraft/snapcraft_test.go index d8b8042ae..33a359aea 100644 --- a/internal/pipe/snapcraft/snapcraft_test.go +++ b/internal/pipe/snapcraft/snapcraft_test.go @@ -402,5 +402,5 @@ func TestSeveralSnapssWithTheSameID(t *testing.T) { }, }, } - require.EqualError(t, Pipe{}.Default(ctx), "found 2 items with the ID 'a', please fix your config") + require.EqualError(t, Pipe{}.Default(ctx), "found 2 snapcrafts with the ID 'a', please fix your config") }