mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-01-24 04:16:27 +02:00
refactor: using tmpl on checksums as well
This commit is contained in:
parent
4557346fc6
commit
0311214cbe
@ -3,11 +3,9 @@
|
|||||||
package checksums
|
package checksums
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"text/template"
|
|
||||||
|
|
||||||
"github.com/apex/log"
|
"github.com/apex/log"
|
||||||
"golang.org/x/sync/errgroup"
|
"golang.org/x/sync/errgroup"
|
||||||
@ -15,6 +13,7 @@ import (
|
|||||||
"github.com/goreleaser/goreleaser/checksum"
|
"github.com/goreleaser/goreleaser/checksum"
|
||||||
"github.com/goreleaser/goreleaser/context"
|
"github.com/goreleaser/goreleaser/context"
|
||||||
"github.com/goreleaser/goreleaser/internal/artifact"
|
"github.com/goreleaser/goreleaser/internal/artifact"
|
||||||
|
"github.com/goreleaser/goreleaser/internal/tmpl"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Pipe for checksums
|
// Pipe for checksums
|
||||||
@ -34,7 +33,7 @@ func (Pipe) Default(ctx *context.Context) error {
|
|||||||
|
|
||||||
// Run the pipe
|
// Run the pipe
|
||||||
func (Pipe) Run(ctx *context.Context) (err error) {
|
func (Pipe) Run(ctx *context.Context) (err error) {
|
||||||
filename, err := filenameFor(ctx)
|
filename, err := tmpl.New(ctx).Apply(ctx.Config.Checksum.NameTemplate)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -83,25 +82,3 @@ func checksums(file *os.File, artifact artifact.Artifact) error {
|
|||||||
_, err = file.WriteString(fmt.Sprintf("%v %v\n", sha, artifact.Name))
|
_, err = file.WriteString(fmt.Sprintf("%v %v\n", sha, artifact.Name))
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func filenameFor(ctx *context.Context) (string, error) {
|
|
||||||
var out bytes.Buffer
|
|
||||||
t, err := template.New("checksums").
|
|
||||||
Option("missingkey=error").
|
|
||||||
Parse(ctx.Config.Checksum.NameTemplate)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
err = t.Execute(&out, struct {
|
|
||||||
ProjectName string
|
|
||||||
Tag string
|
|
||||||
Version string
|
|
||||||
Env map[string]string
|
|
||||||
}{
|
|
||||||
ProjectName: ctx.Config.ProjectName,
|
|
||||||
Tag: ctx.Git.CurrentTag,
|
|
||||||
Version: ctx.Version,
|
|
||||||
Env: ctx.Env,
|
|
||||||
})
|
|
||||||
return out.String(), err
|
|
||||||
}
|
|
||||||
|
@ -31,6 +31,7 @@ func TestPipe(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
ctx.Git.CurrentTag = "1.2.3"
|
||||||
ctx.Env = map[string]string{"FOO": "bar"}
|
ctx.Env = map[string]string{"FOO": "bar"}
|
||||||
ctx.Artifacts.Add(artifact.Artifact{
|
ctx.Artifacts.Add(artifact.Artifact{
|
||||||
Name: binary,
|
Name: binary,
|
||||||
@ -65,6 +66,7 @@ func TestPipeFileNotExist(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
ctx.Git.CurrentTag = "1.2.3"
|
||||||
ctx.Artifacts.Add(artifact.Artifact{
|
ctx.Artifacts.Add(artifact.Artifact{
|
||||||
Name: "nope",
|
Name: "nope",
|
||||||
Path: "/nope",
|
Path: "/nope",
|
||||||
@ -77,8 +79,8 @@ func TestPipeFileNotExist(t *testing.T) {
|
|||||||
|
|
||||||
func TestPipeInvalidNameTemplate(t *testing.T) {
|
func TestPipeInvalidNameTemplate(t *testing.T) {
|
||||||
for template, eerr := range map[string]string{
|
for template, eerr := range map[string]string{
|
||||||
"{{ .Pro }_checksums.txt": `template: checksums:1: unexpected "}" in operand`,
|
"{{ .Pro }_checksums.txt": `template: tmpl:1: unexpected "}" in operand`,
|
||||||
"{{.Env.NOPE}}": `template: checksums:1:6: executing "checksums" at <.Env.NOPE>: map has no entry for key "NOPE"`,
|
"{{.Env.NOPE}}": `template: tmpl:1:6: executing "tmpl" at <.Env.NOPE>: map has no entry for key "NOPE"`,
|
||||||
} {
|
} {
|
||||||
t.Run(template, func(tt *testing.T) {
|
t.Run(template, func(tt *testing.T) {
|
||||||
folder, err := ioutil.TempDir("", "goreleasertest")
|
folder, err := ioutil.TempDir("", "goreleasertest")
|
||||||
@ -92,6 +94,7 @@ func TestPipeInvalidNameTemplate(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
ctx.Git.CurrentTag = "1.2.3"
|
||||||
ctx.Artifacts.Add(artifact.Artifact{
|
ctx.Artifacts.Add(artifact.Artifact{
|
||||||
Name: "whatever",
|
Name: "whatever",
|
||||||
Type: artifact.UploadableBinary,
|
Type: artifact.UploadableBinary,
|
||||||
@ -116,6 +119,7 @@ func TestPipeCouldNotOpenChecksumsTxt(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
ctx.Git.CurrentTag = "1.2.3"
|
||||||
ctx.Artifacts.Add(artifact.Artifact{
|
ctx.Artifacts.Add(artifact.Artifact{
|
||||||
Name: "whatever",
|
Name: "whatever",
|
||||||
Type: artifact.UploadableBinary,
|
Type: artifact.UploadableBinary,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user