1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-04 03:11:55 +02:00

refactory

This commit is contained in:
Carlos Alexandro Becker 2017-01-11 14:47:56 -02:00
parent d13667d173
commit 8731ef7ece
No known key found for this signature in database
GPG Key ID: E61E2F7DC14AB940
9 changed files with 75 additions and 78 deletions

38
config/archive_config.go Normal file
View File

@ -0,0 +1,38 @@
package config
import (
"bytes"
"html/template"
"github.com/goreleaser/releaser/uname"
)
// ArchiveConfig config used for the archive
type ArchiveConfig struct {
Format string
NameTemplate string `yaml:"name_template"`
}
type archiveNameData struct {
Os string
Arch string
Version string
BinaryName string
}
// ArchiveName following the given template
func (config ProjectConfig) ArchiveName(goos, goarch string) (string, error) {
var data = archiveNameData{
Os: uname.FromGo(goos),
Arch: uname.FromGo(goarch),
Version: config.Git.CurrentTag,
BinaryName: config.BinaryName,
}
var out bytes.Buffer
t, err := template.New(data.BinaryName).Parse(config.Archive.NameTemplate)
if err != nil {
return "", err
}
err = t.Execute(&out, data)
return out.String(), err
}

View File

@ -0,0 +1,23 @@
package config
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestNameTemplate(t *testing.T) {
assert := assert.New(t)
var config = ProjectConfig{
BinaryName: "test",
Git: GitInfo{
CurrentTag: "v1.2.3",
},
Archive: ArchiveConfig{
NameTemplate: "{{.BinaryName}}_{{.Os}}_{{.Arch}}_{{.Version}}",
},
}
name, err := config.ArchiveName("darwin", "amd64")
assert.NoError(err)
assert.Equal("test_Darwin_x86_64_v1.2.3", name)
}

View File

@ -34,22 +34,16 @@ type GitInfo struct {
Diff string
}
// ArchiveConfig
type ArchiveConfig struct {
Format string
}
// ProjectConfig includes all project configuration
type ProjectConfig struct {
Repo string
BinaryName string `yaml:"binary_name"`
Files []string
Brew Homebrew
Token string `yaml:"-"`
Build BuildConfig
Git GitInfo `yaml:"-"`
NameTemplate string `yaml:"name_template"`
Archive ArchiveConfig
Repo string
BinaryName string `yaml:"binary_name"`
Files []string
Brew Homebrew
Token string `yaml:"-"`
Build BuildConfig
Git GitInfo `yaml:"-"`
Archive ArchiveConfig
}
// Load config file
@ -113,8 +107,8 @@ func (config *ProjectConfig) fillBasicData() {
if len(config.Build.Arches) == 0 {
config.Build.Arches = []string{"amd64", "386"}
}
if config.NameTemplate == "" {
config.NameTemplate = "{{.BinaryName}}_{{.Os}}_{{.Arch}}"
if config.Archive.NameTemplate == "" {
config.Archive.NameTemplate = "{{.BinaryName}}_{{.Os}}_{{.Arch}}"
}
if config.Archive.Format == "" {
config.Archive.Format = "tar.gz"

View File

@ -1,32 +0,0 @@
package name
import (
"bytes"
"text/template"
"github.com/goreleaser/releaser/config"
"github.com/goreleaser/releaser/uname"
)
type nameData struct {
Os string
Arch string
Version string
BinaryName string
}
func For(config config.ProjectConfig, goos, goarch string) (string, error) {
var data = nameData{
Os: uname.FromGo(goos),
Arch: uname.FromGo(goarch),
Version: config.Git.CurrentTag,
BinaryName: config.BinaryName,
}
var out bytes.Buffer
template, err := template.New(data.BinaryName).Parse(config.NameTemplate)
if err != nil {
return "", err
}
err = template.Execute(&out, data)
return out.String(), err
}

View File

@ -1,22 +0,0 @@
package name
import (
"testing"
"github.com/goreleaser/releaser/config"
"github.com/stretchr/testify/assert"
)
func TestNameTemplate(t *testing.T) {
assert := assert.New(t)
var config = config.ProjectConfig{
BinaryName: "test",
NameTemplate: "{{.BinaryName}}_{{.Os}}_{{.Arch}}_{{.Version}}",
Git: config.GitInfo{
CurrentTag: "v1.2.3",
},
}
name, err := For(config, "darwin", "amd64")
assert.NoError(err)
assert.Equal("test_Darwin_x86_64_v1.2.3", name)
}

View File

@ -11,7 +11,6 @@ import (
"github.com/google/go-github/github"
"github.com/goreleaser/releaser/config"
"github.com/goreleaser/releaser/name"
"github.com/goreleaser/releaser/sha256sum"
"github.com/goreleaser/releaser/split"
"golang.org/x/oauth2"
@ -122,7 +121,7 @@ func dataFor(config config.ProjectConfig, client *github.Client) (result templat
if err != nil {
return
}
file, err := name.For(config, "darwin", "amd64")
file, err := config.ArchiveName("darwin", "amd64")
if err != nil {
return
}

View File

@ -8,7 +8,6 @@ import (
"os/exec"
"github.com/goreleaser/releaser/config"
"github.com/goreleaser/releaser/name"
"golang.org/x/sync/errgroup"
)
@ -37,7 +36,7 @@ func (Pipe) Run(config config.ProjectConfig) error {
func build(system, arch string, config config.ProjectConfig) error {
log.Println("Building", system+"/"+arch, "...")
name, err := name.For(config, system, arch)
name, err := config.ArchiveName(system, arch)
if err != nil {
return err
}

View File

@ -5,7 +5,6 @@ import (
"os"
"github.com/goreleaser/releaser/config"
"github.com/goreleaser/releaser/name"
"github.com/goreleaser/releaser/pipeline/compress/tar"
"github.com/goreleaser/releaser/pipeline/compress/zip"
"golang.org/x/sync/errgroup"
@ -40,7 +39,7 @@ type Archive interface {
}
func create(system, arch string, config config.ProjectConfig) error {
name, err := name.For(config, system, arch)
name, err := config.ArchiveName(system, arch)
if err != nil {
return err
}

View File

@ -8,7 +8,6 @@ import (
"github.com/google/go-github/github"
"github.com/goreleaser/releaser/config"
"github.com/goreleaser/releaser/name"
"github.com/goreleaser/releaser/split"
"golang.org/x/oauth2"
"golang.org/x/sync/errgroup"
@ -65,7 +64,7 @@ func description(diff string) string {
func upload(client *github.Client, releaseID int, system, arch string, config config.ProjectConfig) error {
owner, repo := split.OnSlash(config.Repo)
name, err := name.For(config, system, arch)
name, err := config.ArchiveName(system, arch)
if err != nil {
return err
}