From 896366f3dc4872c81d33ad16c68e726b9b6165c6 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Tue, 31 Oct 2023 12:28:54 +0000 Subject: [PATCH] feat: version in the yaml file Signed-off-by: Carlos Alexandro Becker --- .goreleaser.yaml | 2 ++ internal/static/config.yaml | 2 ++ pkg/config/config.go | 25 +++++++++++++++++++++++++ pkg/config/config_test.go | 21 +++++++++++++++++++++ 4 files changed, 50 insertions(+) diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 2b98510ce26..4e95aef4476 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -1,5 +1,7 @@ # yaml-language-server: $schema=https://goreleaser.com/static/schema.json # vim: set ts=2 sw=2 tw=0 fo=jcroql +version: 1 + env: - GO111MODULE=on diff --git a/internal/static/config.yaml b/internal/static/config.yaml index d3fb70f3b1c..b5fd22b2abc 100644 --- a/internal/static/config.yaml +++ b/internal/static/config.yaml @@ -6,6 +6,8 @@ # yaml-language-server: $schema=https://goreleaser.com/static/schema.json # vim: set ts=2 sw=2 tw=0 fo=cnqoj +version: 1 + before: hooks: # You may remove this if you don't use go modules. diff --git a/pkg/config/config.go b/pkg/config/config.go index d36d8959865..5f2bfbc3987 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -11,11 +11,16 @@ import ( "strings" "time" + "github.com/goreleaser/goreleaser/internal/logext" "github.com/goreleaser/goreleaser/internal/yaml" "github.com/goreleaser/nfpm/v2/files" "github.com/invopop/jsonschema" ) +type Versioned struct { + Version int +} + // Git configs. type Git struct { TagSort string `yaml:"tag_sort,omitempty" json:"tag_sort,omitempty" jsonschema:"enum=-version:refname,enum=-version:creatordate,default=-version:refname"` @@ -1144,6 +1149,7 @@ type Source struct { // Project includes all project configuration. type Project struct { + Version int `yaml:"version" json:"version" jsonschema:"enum=1,default=1"` ProjectName string `yaml:"project_name,omitempty" json:"project_name,omitempty"` Env []string `yaml:"env,omitempty" json:"env,omitempty"` Release Release `yaml:"release,omitempty" json:"release,omitempty"` @@ -1339,12 +1345,31 @@ func Load(file string) (config Project, err error) { return LoadReader(f) } +type VersionError struct { + current int +} + +func (e VersionError) Error() string { + return fmt.Sprintf( + "only configurations files on %s are supported, yours is %s, please update your configuration", + logext.Keyword("version: 1"), + logext.Keyword(fmt.Sprintf("version: %d", e.current)), + ) +} + // LoadReader config via io.Reader. func LoadReader(fd io.Reader) (config Project, err error) { data, err := io.ReadAll(fd) if err != nil { return config, err } + + var versioned Versioned + _ = yaml.Unmarshal(data, &versioned) + if versioned.Version != 0 && versioned.Version != 1 { + return config, VersionError{versioned.Version} + } + err = yaml.UnmarshalStrict(data, &config) return config, err } diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 703f6e129fd..e32d0c75baf 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -1,6 +1,7 @@ package config import ( + "bytes" "fmt" "os" "path/filepath" @@ -72,3 +73,23 @@ func TestConfigWithAnchors(t *testing.T) { _, err := Load("testdata/anchor.yaml") require.NoError(t, err) } + +func TestVersion(t *testing.T) { + t.Run("allow no version", func(t *testing.T) { + _, err := LoadReader(bytes.NewReader(nil)) + require.NoError(t, err) + }) + t.Run("allow v0", func(t *testing.T) { + _, err := LoadReader(strings.NewReader("version: 0")) + require.NoError(t, err) + }) + t.Run("allow v1", func(t *testing.T) { + _, err := LoadReader(strings.NewReader("version: 1")) + require.NoError(t, err) + }) + t.Run("do not allow v2", func(t *testing.T) { + _, err := LoadReader(strings.NewReader("version: 2")) + require.Error(t, err) + require.ErrorIs(t, err, VersionError{2}) + }) +}