1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-10 03:47:03 +02:00
goreleaser/internal/artifact/artifact.go

172 lines
3.8 KiB
Go
Raw Normal View History

// Package artifact provides the core artifact storage for goreleaser
package artifact
2017-12-17 20:31:06 +02:00
import (
2018-08-21 20:55:35 +02:00
"crypto/sha256"
"encoding/hex"
"io"
"os"
2017-12-17 20:31:06 +02:00
"sync"
2017-12-17 20:59:54 +02:00
"github.com/apex/log"
2017-12-17 20:31:06 +02:00
)
// Type defines the type of an artifact
2017-12-18 13:19:02 +02:00
//go:generate stringer -type=Type
type Type int
const (
2017-12-17 20: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
// LinuxPackage is a linux package generated by nfpm or snapcraft
2017-12-17 21:11:08 +02:00
LinuxPackage
// DockerImage is a docker image
DockerImage
2017-12-17 21:25:04 +02:00
// Checksum is a checksums file
Checksum
2017-12-17 21:25:04 +02:00
// Signature is a signature file
Signature
)
// Artifact represents an artifact and its relevant info
type Artifact struct {
Name string
2017-12-17 19:24:49 +02:00
Path string
Goos string
Goarch string
Goarm string
Type Type
2017-12-17 22:01:58 +02:00
Extra map[string]string
}
2018-08-21 20:55:35 +02:00
// Checksum calculates the SHA256 checksum of the artifact.
func (a Artifact) Checksum() (string, error) {
log.Debugf("calculating sha256sum for %s", a.Path)
file, err := os.Open(a.Path)
if err != nil {
return "", err
}
defer file.Close() // nolint: errcheck
var hash = sha256.New()
_, err = io.Copy(hash, file)
if err != nil {
return "", err
}
return hex.EncodeToString(hash.Sum(nil)), nil
}
// 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 19:50:09 +02:00
// List return the actual list of artifacts
2017-12-17 20:10:40 +02:00
func (artifacts Artifacts) List() []Artifact {
2017-12-17 19:50:09 +02:00
return artifacts.items
}
// GroupByPlatform groups the artifacts by their platform
2017-12-17 21:11:08 +02:00
func (artifacts Artifacts) GroupByPlatform() map[string][]Artifact {
2017-12-17 19:50:09 +02:00
var result = map[string][]Artifact{}
for _, a := range artifacts.items {
2017-12-18 13:19:02 +02:00
plat := a.Goos + a.Goarch + a.Goarm
result[plat] = append(result[plat], a)
2017-12-17 19: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 22:01:58 +02:00
log.WithFields(log.Fields{
2017-12-18 13:19:02 +02:00
"name": a.Name,
"path": a.Path,
"type": a.Type,
2017-12-17 22:01:58 +02:00
}).Info("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
}
}
2017-12-17 20:59:54 +02:00
// Or performs an OR between all given filters
func Or(filters ...Filter) Filter {
return func(a Artifact) bool {
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 {
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 20: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 20:59:54 +02:00
if filter(a) {
2017-12-17 22:01:58 +02:00
result.items = append(result.items, a)
}
}
return result
}