mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-02-07 13:31:37 +02:00
refactor: id validations (#1015)
This commit is contained in:
parent
19ed7564b4
commit
c98d3881f7
27
internal/ids/ids.go
Normal file
27
internal/ids/ids.go
Normal file
@ -0,0 +1,27 @@
|
||||
// Package ids provides id validation code used my multiple pipes.
|
||||
package ids
|
||||
|
||||
import "fmt"
|
||||
|
||||
// IDs is the IDs type
|
||||
type IDs map[string]int
|
||||
|
||||
// New IDs
|
||||
func New() IDs {
|
||||
return IDs(map[string]int{})
|
||||
}
|
||||
|
||||
// Inc increment the counter of the given id
|
||||
func (ids IDs) Inc(id string) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
22
internal/ids/ids_test.go
Normal file
22
internal/ids/ids_test.go
Normal file
@ -0,0 +1,22 @@
|
||||
package ids
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestIDs(t *testing.T) {
|
||||
var ids = New()
|
||||
ids.Inc("foo")
|
||||
ids.Inc("bar")
|
||||
require.NoError(t, ids.Validate())
|
||||
}
|
||||
|
||||
func TestIDsError(t *testing.T) {
|
||||
var ids = New()
|
||||
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")
|
||||
}
|
@ -11,11 +11,11 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/goreleaser/goreleaser/internal/deprecate"
|
||||
|
||||
"github.com/apex/log"
|
||||
"github.com/campoy/unique"
|
||||
"github.com/goreleaser/goreleaser/internal/artifact"
|
||||
"github.com/goreleaser/goreleaser/internal/deprecate"
|
||||
"github.com/goreleaser/goreleaser/internal/ids"
|
||||
"github.com/goreleaser/goreleaser/internal/semerrgroup"
|
||||
"github.com/goreleaser/goreleaser/internal/tmpl"
|
||||
"github.com/goreleaser/goreleaser/pkg/archive"
|
||||
@ -42,7 +42,7 @@ func (Pipe) String() string {
|
||||
|
||||
// Default sets the pipe defaults
|
||||
func (Pipe) Default(ctx *context.Context) error {
|
||||
var ids = map[string]int{}
|
||||
var ids = ids.New()
|
||||
if len(ctx.Config.Archives) == 0 {
|
||||
ctx.Config.Archives = append(ctx.Config.Archives, ctx.Config.Archive)
|
||||
if !reflect.DeepEqual(ctx.Config.Archive, config.Archive{}) {
|
||||
@ -80,15 +80,9 @@ func (Pipe) Default(ctx *context.Context) error {
|
||||
archive.Builds = append(archive.Builds, build.ID)
|
||||
}
|
||||
}
|
||||
ids[archive.ID]++
|
||||
ids.Inc(archive.ID)
|
||||
}
|
||||
|
||||
for id, cont := range ids {
|
||||
if cont > 1 {
|
||||
return fmt.Errorf("found %d archives with the ID '%s', please fix your config", cont, id)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
return ids.Validate()
|
||||
}
|
||||
|
||||
// Run the pipe
|
||||
|
@ -676,5 +676,5 @@ func TestSeveralArchivesWithTheSameID(t *testing.T) {
|
||||
},
|
||||
},
|
||||
}
|
||||
require.EqualError(t, Pipe{}.Default(ctx), "found 2 archives with the ID 'a', please fix your config")
|
||||
require.EqualError(t, Pipe{}.Default(ctx), "found 2 items with the ID 'a', please fix your config")
|
||||
}
|
||||
|
@ -3,13 +3,13 @@
|
||||
package build
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/apex/log"
|
||||
"github.com/goreleaser/goreleaser/internal/ids"
|
||||
"github.com/goreleaser/goreleaser/internal/semerrgroup"
|
||||
"github.com/goreleaser/goreleaser/internal/tmpl"
|
||||
builders "github.com/goreleaser/goreleaser/pkg/build"
|
||||
@ -41,22 +41,17 @@ func (Pipe) Run(ctx *context.Context) error {
|
||||
|
||||
// Default sets the pipe defaults
|
||||
func (Pipe) Default(ctx *context.Context) error {
|
||||
var ids = map[string]int{}
|
||||
var ids = ids.New()
|
||||
for i, build := range ctx.Config.Builds {
|
||||
ctx.Config.Builds[i] = buildWithDefaults(ctx, build)
|
||||
ids[ctx.Config.Builds[i].ID]++
|
||||
ids.Inc(ctx.Config.Builds[i].ID)
|
||||
}
|
||||
if len(ctx.Config.Builds) == 0 {
|
||||
ctx.Config.Builds = []config.Build{
|
||||
buildWithDefaults(ctx, ctx.Config.SingleBuild),
|
||||
}
|
||||
}
|
||||
for id, cont := range ids {
|
||||
if cont > 1 {
|
||||
return fmt.Errorf("found %d builds with the ID '%s', please fix your config", cont, id)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
return ids.Validate()
|
||||
}
|
||||
|
||||
func buildWithDefaults(ctx *context.Context, build config.Build) config.Build {
|
||||
|
@ -235,7 +235,7 @@ func TestSeveralBuildsWithTheSameID(t *testing.T) {
|
||||
},
|
||||
},
|
||||
}
|
||||
assert.EqualError(t, Pipe{}.Default(ctx), "found 2 builds with the ID 'a', please fix your config")
|
||||
assert.EqualError(t, Pipe{}.Default(ctx), "found 2 items with the ID 'a', please fix your config")
|
||||
}
|
||||
|
||||
func TestDefaultPartialBuilds(t *testing.T) {
|
||||
|
@ -10,6 +10,7 @@ import (
|
||||
"github.com/apex/log"
|
||||
"github.com/goreleaser/goreleaser/internal/artifact"
|
||||
"github.com/goreleaser/goreleaser/internal/deprecate"
|
||||
"github.com/goreleaser/goreleaser/internal/ids"
|
||||
"github.com/goreleaser/goreleaser/internal/linux"
|
||||
"github.com/goreleaser/goreleaser/internal/pipe"
|
||||
"github.com/goreleaser/goreleaser/internal/semerrgroup"
|
||||
@ -40,8 +41,12 @@ func (Pipe) Default(ctx *context.Context) error {
|
||||
deprecate.Notice("nfpm")
|
||||
}
|
||||
}
|
||||
var ids = ids.New()
|
||||
for i := range ctx.Config.NFPMs {
|
||||
var fpm = &ctx.Config.NFPMs[i]
|
||||
if fpm.ID == "" {
|
||||
fpm.ID = "default"
|
||||
}
|
||||
if fpm.Bindir == "" {
|
||||
fpm.Bindir = "/usr/local/bin"
|
||||
}
|
||||
@ -56,8 +61,9 @@ func (Pipe) Default(ctx *context.Context) error {
|
||||
fpm.Builds = append(fpm.Builds, b.ID)
|
||||
}
|
||||
}
|
||||
ids.Inc(fpm.ID)
|
||||
}
|
||||
return nil
|
||||
return ids.Validate()
|
||||
}
|
||||
|
||||
// Run the pipe
|
||||
|
@ -342,3 +342,19 @@ func TestOverrides(t *testing.T) {
|
||||
require.Equal(t, "bar", ctx.Config.NFPMs[0].Overrides["deb"].NameTemplate)
|
||||
require.Equal(t, "bar", merged.NameTemplate)
|
||||
}
|
||||
|
||||
func TestSeveralNFPMsWithTheSameID(t *testing.T) {
|
||||
var ctx = &context.Context{
|
||||
Config: config.Project{
|
||||
NFPMs: []config.NFPM{
|
||||
{
|
||||
ID: "a",
|
||||
},
|
||||
{
|
||||
ID: "a",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
require.EqualError(t, Pipe{}.Default(ctx), "found 2 items with the ID 'a', please fix your config")
|
||||
}
|
||||
|
@ -172,6 +172,7 @@ type NFPM struct {
|
||||
NFPMOverridables `yaml:",inline"`
|
||||
Overrides map[string]NFPMOverridables `yaml:"overrides,omitempty"`
|
||||
|
||||
ID string `yaml:",omitempty"`
|
||||
Builds []string `yaml:",omitempty"`
|
||||
Formats []string `yaml:",omitempty"`
|
||||
Vendor string `yaml:",omitempty"`
|
||||
@ -298,14 +299,15 @@ type S3 struct {
|
||||
|
||||
// Put HTTP upload configuration
|
||||
type Put struct {
|
||||
Name string `yaml:",omitempty"`
|
||||
Target string `yaml:",omitempty"`
|
||||
Username string `yaml:",omitempty"`
|
||||
Mode string `yaml:",omitempty"`
|
||||
ChecksumHeader string `yaml:"checksum_header,omitempty"`
|
||||
TrustedCerts string `yaml:"trusted_certificates,omitempty"`
|
||||
Checksum bool `yaml:",omitempty"`
|
||||
Signature bool `yaml:",omitempty"`
|
||||
Name string `yaml:",omitempty"`
|
||||
IDs []string `yaml:"ids,omitempty"`
|
||||
Target string `yaml:",omitempty"`
|
||||
Username string `yaml:",omitempty"`
|
||||
Mode string `yaml:",omitempty"`
|
||||
ChecksumHeader string `yaml:"checksum_header,omitempty"`
|
||||
TrustedCerts string `yaml:"trusted_certificates,omitempty"`
|
||||
Checksum bool `yaml:",omitempty"`
|
||||
Signature bool `yaml:",omitempty"`
|
||||
}
|
||||
|
||||
// Project includes all project configuration
|
||||
|
@ -15,6 +15,9 @@ Available options:
|
||||
nfpms:
|
||||
# note that this is an array of nfpm configs
|
||||
-
|
||||
# ID of the nfpm config, must be unique.
|
||||
# Defaults to "default".
|
||||
id: foo
|
||||
# You can change the name of the package.
|
||||
# Default: `{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}`
|
||||
name_template: "{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}"
|
||||
|
Loading…
x
Reference in New Issue
Block a user