Skip to content

Commit

Permalink
feat: version in the yaml file
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
  • Loading branch information
caarlos0 committed Oct 31, 2023
1 parent 3009ac1 commit 896366f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .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

Expand Down
2 changes: 2 additions & 0 deletions internal/static/config.yaml
Expand Up @@ -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.
Expand Down
25 changes: 25 additions & 0 deletions pkg/config/config.go
Expand Up @@ -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"`
Expand Down Expand Up @@ -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"`
Expand Down Expand Up @@ -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
}
Expand Down
21 changes: 21 additions & 0 deletions pkg/config/config_test.go
@@ -1,6 +1,7 @@
package config

import (
"bytes"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -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})
})
}

0 comments on commit 896366f

Please sign in to comment.