mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-01-10 03:47:03 +02:00
wip checksums
This commit is contained in:
parent
a38ebfcc81
commit
16cf195f9b
36
checksum/checksum.go
Normal file
36
checksum/checksum.go
Normal file
@ -0,0 +1,36 @@
|
||||
package checksum
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"hash"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
// MD5 sum of the given file
|
||||
func MD5(path string) (result string, err error) {
|
||||
return calculate(md5.New(), path)
|
||||
}
|
||||
|
||||
// SHA256 sum of the given file
|
||||
func SHA256(path string) (result string, err error) {
|
||||
return calculate(sha256.New(), path)
|
||||
}
|
||||
|
||||
func calculate(hash hash.Hash, path string) (result string, err error) {
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer func() { _ = file.Close() }()
|
||||
|
||||
_, err = io.Copy(hash, file)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
result = hex.EncodeToString(hash.Sum(nil))
|
||||
return
|
||||
}
|
@ -8,10 +8,10 @@ import (
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"github.com/goreleaser/goreleaser/checksum"
|
||||
"github.com/goreleaser/goreleaser/clients"
|
||||
"github.com/goreleaser/goreleaser/config"
|
||||
"github.com/goreleaser/goreleaser/context"
|
||||
"github.com/goreleaser/goreleaser/sha256sum"
|
||||
)
|
||||
|
||||
// ErrNoDarwin64Build when there is no build for darwin_amd64 (goos doesn't
|
||||
@ -137,7 +137,7 @@ func dataFor(ctx *context.Context, client clients.Client) (result templateData,
|
||||
if file == "" {
|
||||
return result, ErrNoDarwin64Build
|
||||
}
|
||||
sum, err := sha256sum.For(
|
||||
sum, err := checksum.SHA256(
|
||||
filepath.Join(
|
||||
ctx.Config.Dist,
|
||||
file+"."+ctx.Config.Archive.Format,
|
||||
|
@ -1,11 +1,15 @@
|
||||
package checksums
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"golang.org/x/sync/errgroup"
|
||||
|
||||
"github.com/goreleaser/goreleaser/checksum"
|
||||
"github.com/goreleaser/goreleaser/context"
|
||||
"github.com/goreleaser/goreleaser/sha256sum"
|
||||
)
|
||||
|
||||
// Pipe for checksums
|
||||
@ -18,24 +22,41 @@ func (Pipe) Description() string {
|
||||
|
||||
// Run the pipe
|
||||
func (Pipe) Run(ctx *context.Context) (err error) {
|
||||
var g errgroup.Group
|
||||
for _, artifact := range ctx.Artifacts {
|
||||
artifact := artifact
|
||||
g.Go(func() error {
|
||||
return checksums(ctx, artifact)
|
||||
})
|
||||
}
|
||||
return g.Wait()
|
||||
}
|
||||
|
||||
func checksums(ctx *context.Context, name string) error {
|
||||
log.Println("Checksumming", name)
|
||||
var artifact = filepath.Join(ctx.Config.Dist, name)
|
||||
var checksums = fmt.Sprintf("%v.%v", name, "checksums")
|
||||
sha, err := checksum.SHA256(artifact)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
md5, err := checksum.MD5(artifact)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
file, err := os.OpenFile(
|
||||
filepath.Join(ctx.Config.Dist, "CHECKSUMS.txt"),
|
||||
os.O_APPEND|os.O_WRONLY|os.O_CREATE,
|
||||
filepath.Join(ctx.Config.Dist, checksums),
|
||||
os.O_APPEND|os.O_WRONLY|os.O_CREATE|os.O_EXCL,
|
||||
0600,
|
||||
)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer func() { _ = file.Close() }()
|
||||
for _, artifact := range ctx.Artifacts {
|
||||
sha, err := sha256sum.For(filepath.Join(ctx.Config.Dist, artifact))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = file.WriteString(artifact + " sha256sum: " + sha + "\n"); err != nil {
|
||||
return err
|
||||
}
|
||||
var template = "%v %v\n"
|
||||
if _, err = file.WriteString(fmt.Sprintf(template, "md5sum", md5)); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = file.WriteString(fmt.Sprintf(template, "sha256sum", sha)); err != nil {
|
||||
return err
|
||||
}
|
||||
ctx.AddArtifact(file.Name())
|
||||
return
|
||||
return nil
|
||||
}
|
||||
|
@ -1,26 +0,0 @@
|
||||
package sha256sum
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
// For calculates the SHA256 sum for the given file
|
||||
func For(path string) (result string, err error) {
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer func() { _ = file.Close() }()
|
||||
|
||||
hash := sha256.New()
|
||||
_, err = io.Copy(hash, file)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
result = hex.EncodeToString(hash.Sum(nil))
|
||||
return
|
||||
}
|
Loading…
Reference in New Issue
Block a user