1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-24 04:16:27 +02:00

258 lines
5.8 KiB
Go
Raw Normal View History

// Package artifact provides the core artifact storage for goreleaser
package artifact
// nolint: gosec
2017-12-17 16:31:06 -02:00
import (
"crypto/md5"
"crypto/sha1"
2018-08-21 15:55:35 -03:00
"crypto/sha256"
"crypto/sha512"
2018-08-21 15:55:35 -03:00
"encoding/hex"
"fmt"
"hash"
"hash/crc32"
2018-08-21 15:55:35 -03:00
"io"
"os"
2017-12-17 16:31:06 -02:00
"sync"
2017-12-17 16:59:54 -02:00
"github.com/apex/log"
"github.com/pkg/errors"
2017-12-17 16:31:06 -02:00
)
// Type defines the type of an artifact
type Type int
const (
2017-12-17 16:10:40 -02:00
// UploadableArchive a tar.gz/zip archive to be uploaded
UploadableArchive Type = iota
// UploadableBinary is a binary file to be uploaded
UploadableBinary
// Binary is a binary (output of a gobuild)
Binary
2018-10-20 14:25:46 -03:00
// LinuxPackage is a linux package generated by nfpm
2017-12-17 17:11:08 -02:00
LinuxPackage
2018-10-20 14:25:46 -03:00
// PublishableSnapcraft is a snap package yet to be published
PublishableSnapcraft
// Snapcraft is a published snap package
Snapcraft
2018-10-20 13:45:31 -03:00
// PublishableDockerImage is a Docker image yet to be published
PublishableDockerImage
// DockerImage is a published Docker image
DockerImage
2017-12-17 17:25:04 -02:00
// Checksum is a checksums file
Checksum
2017-12-17 17:25:04 -02:00
// Signature is a signature file
Signature
)
2018-09-15 18:53:59 -03:00
func (t Type) String() string {
switch t {
case UploadableArchive:
return "Archive"
case UploadableBinary:
case Binary:
return "Binary"
case LinuxPackage:
2018-10-20 13:45:31 -03:00
return "Linux Package"
2018-09-15 18:53:59 -03:00
case DockerImage:
2018-10-20 13:45:31 -03:00
case PublishableDockerImage:
return "Docker Image"
2018-09-15 18:53:59 -03:00
case Checksum:
return "Checksum"
case Signature:
return "Signature"
}
return "unknown"
}
// Artifact represents an artifact and its relevant info
type Artifact struct {
Name string
2017-12-17 15:24:49 -02:00
Path string
Goos string
Goarch string
Goarm string
Type Type
Extra map[string]interface{}
}
// ExtraOr returns the Extra field with the given key or the or value specified
// if it is nil.
func (a Artifact) ExtraOr(key string, or interface{}) interface{} {
if a.Extra[key] == nil {
return or
}
return a.Extra[key]
}
// Checksum calculates the checksum of the artifact.
// nolint: gosec
func (a Artifact) Checksum(algorithm string) (string, error) {
log.Debugf("calculating checksum for %s", a.Path)
2018-08-21 15:55:35 -03:00
file, err := os.Open(a.Path)
if err != nil {
return "", errors.Wrap(err, "failed to checksum")
2018-08-21 15:55:35 -03:00
}
defer file.Close() // nolint: errcheck
var h hash.Hash
switch algorithm {
case "crc32":
h = crc32.NewIEEE()
case "md5":
h = md5.New()
case "sha224":
h = sha256.New224()
case "sha384":
h = sha512.New384()
case "sha256":
h = sha256.New()
case "sha1":
h = sha1.New()
case "sha512":
h = sha512.New()
default:
return "", fmt.Errorf("invalid algorith: %s", algorithm)
}
_, err = io.Copy(h, file)
2018-08-21 15:55:35 -03:00
if err != nil {
return "", errors.Wrap(err, "failed to checksum")
2018-08-21 15:55:35 -03:00
}
return hex.EncodeToString(h.Sum(nil)), nil
2018-08-21 15:55:35 -03:00
}
// Artifacts is a list of artifacts
type Artifacts struct {
items []*Artifact
lock *sync.Mutex
}
// New return a new list of artifacts
func New() Artifacts {
return Artifacts{
items: []*Artifact{},
lock: &sync.Mutex{},
}
}
2017-12-17 15:50:09 -02:00
// List return the actual list of artifacts
func (artifacts Artifacts) List() []*Artifact {
2017-12-17 15:50:09 -02:00
return artifacts.items
}
// GroupByPlatform groups the artifacts by their platform
func (artifacts Artifacts) GroupByPlatform() map[string][]*Artifact {
var result = map[string][]*Artifact{}
2017-12-17 15:50:09 -02:00
for _, a := range artifacts.items {
2017-12-18 09:19:02 -02:00
plat := a.Goos + a.Goarch + a.Goarm
result[plat] = append(result[plat], a)
2017-12-17 15:50:09 -02:00
}
return result
}
// Add safely adds a new artifact to an artifact list
func (artifacts *Artifacts) Add(a *Artifact) {
artifacts.lock.Lock()
defer artifacts.lock.Unlock()
2017-12-17 18:01:58 -02:00
log.WithFields(log.Fields{
2017-12-18 09:19:02 -02:00
"name": a.Name,
"path": a.Path,
"type": a.Type,
2018-10-05 09:48:00 -03:00
}).Debug("added new artifact")
artifacts.items = append(artifacts.items, a)
}
// Filter defines an artifact filter which can be used within the Filter
// function
type Filter func(a *Artifact) bool
// ByGoos is a predefined filter that filters by the given goos
func ByGoos(s string) Filter {
return func(a *Artifact) bool {
return a.Goos == s
}
}
// ByGoarch is a predefined filter that filters by the given goarch
func ByGoarch(s string) Filter {
return func(a *Artifact) bool {
return a.Goarch == s
}
}
// ByGoarm is a predefined filter that filters by the given goarm
func ByGoarm(s string) Filter {
return func(a *Artifact) bool {
return a.Goarm == s
}
}
// ByType is a predefined filter that filters by the given type
func ByType(t Type) Filter {
return func(a *Artifact) bool {
return a.Type == t
}
}
// ByFormats filters artifacts by a `Format` extra field.
func ByFormats(formats ...string) Filter {
var filters = make([]Filter, 0, len(formats))
for _, format := range formats {
format := format
filters = append(filters, func(a *Artifact) bool {
return a.ExtraOr("Format", "") == format
})
}
return Or(filters...)
}
// ByIDs filter artifacts by an `ID` extra field.
func ByIDs(ids ...string) Filter {
var filters = make([]Filter, 0, len(ids))
for _, id := range ids {
id := id
filters = append(filters, func(a *Artifact) bool {
// checksum are allways for all artifacts, so return always true.
return a.Type == Checksum || a.ExtraOr("ID", "") == id
})
}
return Or(filters...)
}
2017-12-17 16:59:54 -02:00
// Or performs an OR between all given filters
func Or(filters ...Filter) Filter {
return func(a *Artifact) bool {
2017-12-17 16:59:54 -02:00
for _, f := range filters {
if f(a) {
return true
}
}
return false
}
}
// And performs an AND between all given filters
func And(filters ...Filter) Filter {
return func(a *Artifact) bool {
2017-12-17 16:59:54 -02:00
for _, f := range filters {
if !f(a) {
return false
}
}
return true
}
}
// Filter filters the artifact list, returning a new instance.
// There are some pre-defined filters but anything of the Type Filter
// is accepted.
2017-12-17 16:59:54 -02:00
// You can compose filters by using the And and Or filters.
func (artifacts *Artifacts) Filter(filter Filter) Artifacts {
var result = New()
for _, a := range artifacts.items {
2017-12-17 16:59:54 -02:00
if filter(a) {
2017-12-17 18:01:58 -02:00
result.items = append(result.items, a)
}
}
return result
}