2020-05-26 05:48:10 +02:00
|
|
|
// Package artifact provides the core artifact storage for goreleaser.
|
2017-12-17 19:14:21 +02:00
|
|
|
package artifact
|
|
|
|
|
2019-02-04 21:27:51 +02:00
|
|
|
// nolint: gosec
|
2017-12-17 20:31:06 +02:00
|
|
|
import (
|
2019-02-04 21:27:51 +02:00
|
|
|
"crypto/md5"
|
|
|
|
"crypto/sha1"
|
2018-08-21 20:55:35 +02:00
|
|
|
"crypto/sha256"
|
2019-02-04 21:27:51 +02:00
|
|
|
"crypto/sha512"
|
2018-08-21 20:55:35 +02:00
|
|
|
"encoding/hex"
|
2019-02-04 21:27:51 +02:00
|
|
|
"fmt"
|
|
|
|
"hash"
|
|
|
|
"hash/crc32"
|
2018-08-21 20:55:35 +02:00
|
|
|
"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
|
|
|
)
|
2017-12-17 19:14:21 +02:00
|
|
|
|
2020-05-26 05:48:10 +02:00
|
|
|
// Type defines the type of an artifact.
|
2017-12-17 19:14:21 +02:00
|
|
|
type Type int
|
|
|
|
|
|
|
|
const (
|
2020-08-04 05:21:26 +02:00
|
|
|
// UploadableArchive a tar.gz/zip archive to be uploaded.
|
2017-12-17 20:10:40 +02:00
|
|
|
UploadableArchive Type = iota
|
2020-08-04 05:21:26 +02:00
|
|
|
// UploadableBinary is a binary file to be uploaded.
|
2017-12-17 20:10:40 +02:00
|
|
|
UploadableBinary
|
2020-08-04 05:21:26 +02:00
|
|
|
// UploadableFile is any file that can be uploaded.
|
2020-02-11 21:10:41 +02:00
|
|
|
UploadableFile
|
2020-08-04 05:21:26 +02:00
|
|
|
// Binary is a binary (output of a gobuild).
|
2017-12-17 19:14:21 +02:00
|
|
|
Binary
|
2021-10-12 19:55:43 +02:00
|
|
|
// UniversalBinary is a binary that contains multiple binaries within.
|
|
|
|
UniversalBinary
|
2020-08-04 05:21:26 +02:00
|
|
|
// LinuxPackage is a linux package generated by nfpm.
|
2017-12-17 21:11:08 +02:00
|
|
|
LinuxPackage
|
2020-08-04 05:21:26 +02:00
|
|
|
// PublishableSnapcraft is a snap package yet to be published.
|
2018-10-20 19:25:46 +02:00
|
|
|
PublishableSnapcraft
|
2020-08-04 05:21:26 +02:00
|
|
|
// Snapcraft is a published snap package.
|
2018-10-20 19:25:46 +02:00
|
|
|
Snapcraft
|
2020-08-04 05:21:26 +02:00
|
|
|
// PublishableDockerImage is a Docker image yet to be published.
|
2018-10-20 18:45:31 +02:00
|
|
|
PublishableDockerImage
|
2020-08-04 05:21:26 +02:00
|
|
|
// DockerImage is a published Docker image.
|
2017-12-17 19:14:21 +02:00
|
|
|
DockerImage
|
2020-11-29 19:33:31 +02:00
|
|
|
// DockerManifest is a published Docker manifest.
|
|
|
|
DockerManifest
|
2020-08-04 05:21:26 +02:00
|
|
|
// Checksum is a checksums file.
|
2017-12-17 19:14:21 +02:00
|
|
|
Checksum
|
2020-08-04 05:21:26 +02:00
|
|
|
// Signature is a signature file.
|
2017-12-17 21:25:04 +02:00
|
|
|
Signature
|
2020-08-04 05:21:26 +02:00
|
|
|
// UploadableSourceArchive is the archive with the current commit source code.
|
2020-04-12 16:47:46 +02:00
|
|
|
UploadableSourceArchive
|
2021-09-18 15:21:29 +02:00
|
|
|
// BrewTap is an uploadable homebrew tap recipe file.
|
|
|
|
BrewTap
|
2021-09-29 01:16:39 +02:00
|
|
|
// GoFishRig is an uploadable Rigs rig food file.
|
|
|
|
GoFishRig
|
2021-09-18 15:21:29 +02:00
|
|
|
// ScoopManifest is an uploadable scoop manifest file.
|
|
|
|
ScoopManifest
|
2017-12-17 19:14:21 +02:00
|
|
|
)
|
|
|
|
|
2018-09-15 23:53:59 +02:00
|
|
|
func (t Type) String() string {
|
|
|
|
switch t {
|
|
|
|
case UploadableArchive:
|
|
|
|
return "Archive"
|
2020-04-12 16:47:46 +02:00
|
|
|
case UploadableFile:
|
|
|
|
return "File"
|
2021-10-12 19:55:43 +02:00
|
|
|
case UploadableBinary, Binary, UniversalBinary:
|
2018-09-15 23:53:59 +02:00
|
|
|
return "Binary"
|
|
|
|
case LinuxPackage:
|
2018-10-20 18:45:31 +02:00
|
|
|
return "Linux Package"
|
2020-09-02 21:44:02 +02:00
|
|
|
case PublishableDockerImage, DockerImage:
|
2018-10-20 18:45:31 +02:00
|
|
|
return "Docker Image"
|
2020-11-29 19:33:31 +02:00
|
|
|
case DockerManifest:
|
|
|
|
return "Docker Manifest"
|
2020-09-02 21:44:02 +02:00
|
|
|
case PublishableSnapcraft, Snapcraft:
|
2020-04-12 16:47:46 +02:00
|
|
|
return "Snap"
|
2018-09-15 23:53:59 +02:00
|
|
|
case Checksum:
|
|
|
|
return "Checksum"
|
|
|
|
case Signature:
|
|
|
|
return "Signature"
|
2020-04-12 16:47:46 +02:00
|
|
|
case UploadableSourceArchive:
|
|
|
|
return "Source"
|
2021-09-18 15:21:29 +02:00
|
|
|
case BrewTap:
|
|
|
|
return "Brew Tap"
|
2021-09-29 01:16:39 +02:00
|
|
|
case GoFishRig:
|
|
|
|
return "GoFish Rig"
|
2021-09-18 15:21:29 +02:00
|
|
|
case ScoopManifest:
|
|
|
|
return "Scoop Manifest"
|
2020-09-02 21:44:02 +02:00
|
|
|
default:
|
|
|
|
return "unknown"
|
2018-09-15 23:53:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-17 03:46:11 +02:00
|
|
|
const (
|
2021-10-17 04:03:06 +02:00
|
|
|
ExtraID = "ID"
|
|
|
|
ExtraBinary = "Binary"
|
|
|
|
ExtraExt = "Ext"
|
|
|
|
ExtraBuilds = "Builds"
|
|
|
|
ExtraFormat = "Format"
|
|
|
|
ExtraWrappedIn = "WrappedIn"
|
|
|
|
ExtraBinaries = "Binaries"
|
|
|
|
ExtraBinariesClean = "BinariesClean"
|
2021-10-17 03:46:11 +02:00
|
|
|
)
|
|
|
|
|
2020-05-26 05:48:10 +02:00
|
|
|
// Artifact represents an artifact and its relevant info.
|
2017-12-17 19:14:21 +02:00
|
|
|
type Artifact struct {
|
|
|
|
Name string
|
2017-12-17 19:24:49 +02:00
|
|
|
Path string
|
2017-12-17 19:14:21 +02:00
|
|
|
Goos string
|
|
|
|
Goarch string
|
|
|
|
Goarm string
|
2020-01-26 19:36:00 +02:00
|
|
|
Gomips string
|
2017-12-17 19:14:21 +02:00
|
|
|
Type Type
|
2019-01-01 18:40:17 +02:00
|
|
|
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]
|
2017-12-17 19:14:21 +02:00
|
|
|
}
|
|
|
|
|
2019-02-04 21:27:51 +02:00
|
|
|
// 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 20:55:35 +02:00
|
|
|
file, err := os.Open(a.Path)
|
|
|
|
if err != nil {
|
2020-09-21 19:47:51 +02:00
|
|
|
return "", fmt.Errorf("failed to checksum: %w", err)
|
2018-08-21 20:55:35 +02:00
|
|
|
}
|
2020-05-26 05:48:10 +02:00
|
|
|
defer file.Close()
|
2019-02-04 21:27:51 +02:00
|
|
|
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:
|
2020-09-02 21:44:25 +02:00
|
|
|
return "", fmt.Errorf("invalid algorithm: %s", algorithm)
|
2019-02-04 21:27:51 +02:00
|
|
|
}
|
2021-09-18 15:21:29 +02:00
|
|
|
|
|
|
|
if _, err := io.Copy(h, file); err != nil {
|
2020-09-21 19:47:51 +02:00
|
|
|
return "", fmt.Errorf("failed to checksum: %w", err)
|
2018-08-21 20:55:35 +02:00
|
|
|
}
|
2019-02-04 21:27:51 +02:00
|
|
|
return hex.EncodeToString(h.Sum(nil)), nil
|
2018-08-21 20:55:35 +02:00
|
|
|
}
|
|
|
|
|
2021-10-17 03:46:11 +02:00
|
|
|
// ID returns the artifact ID if it exists, empty otherwise.
|
|
|
|
func (a Artifact) ID() string {
|
|
|
|
return a.ExtraOr(ExtraID, "").(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Format returns the artifact Format if it exists, empty otherwise.
|
|
|
|
func (a Artifact) Format() string {
|
|
|
|
return a.ExtraOr(ExtraFormat, "").(string)
|
|
|
|
}
|
|
|
|
|
2020-05-26 05:48:10 +02:00
|
|
|
// Artifacts is a list of artifacts.
|
2017-12-17 19:14:21 +02:00
|
|
|
type Artifacts struct {
|
2019-08-12 22:44:48 +02:00
|
|
|
items []*Artifact
|
2017-12-17 19:14:21 +02:00
|
|
|
lock *sync.Mutex
|
|
|
|
}
|
|
|
|
|
2020-05-26 05:48:10 +02:00
|
|
|
// New return a new list of artifacts.
|
2017-12-17 19:14:21 +02:00
|
|
|
func New() Artifacts {
|
|
|
|
return Artifacts{
|
2019-08-12 22:44:48 +02:00
|
|
|
items: []*Artifact{},
|
2017-12-17 19:14:21 +02:00
|
|
|
lock: &sync.Mutex{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-26 05:48:10 +02:00
|
|
|
// List return the actual list of artifacts.
|
2019-08-12 22:44:48 +02:00
|
|
|
func (artifacts Artifacts) List() []*Artifact {
|
2017-12-17 19:50:09 +02:00
|
|
|
return artifacts.items
|
|
|
|
}
|
|
|
|
|
2020-05-26 05:48:10 +02:00
|
|
|
// GroupByPlatform groups the artifacts by their platform.
|
2019-08-12 22:44:48 +02:00
|
|
|
func (artifacts Artifacts) GroupByPlatform() map[string][]*Artifact {
|
2021-04-25 19:20:49 +02:00
|
|
|
result := map[string][]*Artifact{}
|
2017-12-17 19:50:09 +02:00
|
|
|
for _, a := range artifacts.items {
|
2020-02-06 03:08:18 +02:00
|
|
|
plat := a.Goos + a.Goarch + a.Goarm + a.Gomips
|
2017-12-18 13:19:02 +02:00
|
|
|
result[plat] = append(result[plat], a)
|
2017-12-17 19:50:09 +02:00
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-05-26 05:48:10 +02:00
|
|
|
// Add safely adds a new artifact to an artifact list.
|
2019-08-12 22:44:48 +02:00
|
|
|
func (artifacts *Artifacts) Add(a *Artifact) {
|
2017-12-17 19:14:21 +02:00
|
|
|
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,
|
2018-10-05 14:48:00 +02:00
|
|
|
}).Debug("added new artifact")
|
2017-12-17 19:14:21 +02:00
|
|
|
artifacts.items = append(artifacts.items, a)
|
|
|
|
}
|
|
|
|
|
2021-10-12 19:55:43 +02:00
|
|
|
// Remove removes artifacts that match the given filter from the original artifact list.
|
|
|
|
func (artifacts *Artifacts) Remove(filter Filter) error {
|
|
|
|
if filter == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
artifacts.lock.Lock()
|
|
|
|
defer artifacts.lock.Unlock()
|
|
|
|
|
|
|
|
result := New()
|
|
|
|
for _, a := range artifacts.items {
|
|
|
|
if filter(a) {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"name": a.Name,
|
|
|
|
"path": a.Path,
|
|
|
|
"type": a.Type,
|
|
|
|
}).Debug("removing")
|
|
|
|
} else {
|
|
|
|
result.items = append(result.items, a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
artifacts.items = result.items
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-17 19:14:21 +02:00
|
|
|
// Filter defines an artifact filter which can be used within the Filter
|
2020-05-26 05:48:10 +02:00
|
|
|
// function.
|
2019-08-12 22:44:48 +02:00
|
|
|
type Filter func(a *Artifact) bool
|
2017-12-17 19:14:21 +02:00
|
|
|
|
2020-05-26 05:48:10 +02:00
|
|
|
// ByGoos is a predefined filter that filters by the given goos.
|
2017-12-17 19:14:21 +02:00
|
|
|
func ByGoos(s string) Filter {
|
2019-08-12 22:44:48 +02:00
|
|
|
return func(a *Artifact) bool {
|
2017-12-17 19:14:21 +02:00
|
|
|
return a.Goos == s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-26 05:48:10 +02:00
|
|
|
// ByGoarch is a predefined filter that filters by the given goarch.
|
2017-12-17 19:14:21 +02:00
|
|
|
func ByGoarch(s string) Filter {
|
2019-08-12 22:44:48 +02:00
|
|
|
return func(a *Artifact) bool {
|
2017-12-17 19:14:21 +02:00
|
|
|
return a.Goarch == s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-26 05:48:10 +02:00
|
|
|
// ByGoarm is a predefined filter that filters by the given goarm.
|
2017-12-17 19:14:21 +02:00
|
|
|
func ByGoarm(s string) Filter {
|
2019-08-12 22:44:48 +02:00
|
|
|
return func(a *Artifact) bool {
|
2017-12-17 19:14:21 +02:00
|
|
|
return a.Goarm == s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-26 05:48:10 +02:00
|
|
|
// ByType is a predefined filter that filters by the given type.
|
2017-12-17 19:14:21 +02:00
|
|
|
func ByType(t Type) Filter {
|
2019-08-12 22:44:48 +02:00
|
|
|
return func(a *Artifact) bool {
|
2017-12-17 19:14:21 +02:00
|
|
|
return a.Type == t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-10 15:35:19 +02:00
|
|
|
// ByFormats filters artifacts by a `Format` extra field.
|
|
|
|
func ByFormats(formats ...string) Filter {
|
2021-04-25 19:20:49 +02:00
|
|
|
filters := make([]Filter, 0, len(formats))
|
2019-06-10 15:35:19 +02:00
|
|
|
for _, format := range formats {
|
|
|
|
format := format
|
2019-08-12 22:44:48 +02:00
|
|
|
filters = append(filters, func(a *Artifact) bool {
|
2021-10-17 03:46:11 +02:00
|
|
|
return a.Format() == format
|
2019-06-10 15:35:19 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return Or(filters...)
|
|
|
|
}
|
|
|
|
|
2019-04-16 15:19:15 +02:00
|
|
|
// ByIDs filter artifacts by an `ID` extra field.
|
|
|
|
func ByIDs(ids ...string) Filter {
|
2021-04-25 19:20:49 +02:00
|
|
|
filters := make([]Filter, 0, len(ids))
|
2019-04-16 15:19:15 +02:00
|
|
|
for _, id := range ids {
|
|
|
|
id := id
|
2019-08-12 22:44:48 +02:00
|
|
|
filters = append(filters, func(a *Artifact) bool {
|
2020-04-12 16:47:46 +02:00
|
|
|
// checksum and source archive are always for all artifacts, so return always true.
|
|
|
|
return a.Type == Checksum ||
|
|
|
|
a.Type == UploadableSourceArchive ||
|
2021-10-17 03:46:11 +02:00
|
|
|
a.ID() == id
|
2019-04-16 15:19:15 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return Or(filters...)
|
|
|
|
}
|
|
|
|
|
2020-05-26 05:48:10 +02:00
|
|
|
// Or performs an OR between all given filters.
|
2017-12-17 20:59:54 +02:00
|
|
|
func Or(filters ...Filter) Filter {
|
2019-08-12 22:44:48 +02:00
|
|
|
return func(a *Artifact) bool {
|
2017-12-17 20:59:54 +02:00
|
|
|
for _, f := range filters {
|
|
|
|
if f(a) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-26 05:48:10 +02:00
|
|
|
// And performs an AND between all given filters.
|
2017-12-17 20:59:54 +02:00
|
|
|
func And(filters ...Filter) Filter {
|
2019-08-12 22:44:48 +02:00
|
|
|
return func(a *Artifact) bool {
|
2017-12-17 20:59:54 +02:00
|
|
|
for _, f := range filters {
|
|
|
|
if !f(a) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-17 19:14:21 +02:00
|
|
|
// 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 {
|
2020-05-10 18:03:49 +02:00
|
|
|
if filter == nil {
|
|
|
|
return *artifacts
|
|
|
|
}
|
|
|
|
|
2021-04-25 19:20:49 +02:00
|
|
|
result := New()
|
2017-12-17 19:14:21 +02:00
|
|
|
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)
|
2017-12-17 19:14:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
2021-01-07 21:21:12 +02:00
|
|
|
|
|
|
|
// Paths returns the artifact.Path of the current artifact list.
|
|
|
|
func (artifacts Artifacts) Paths() []string {
|
|
|
|
var result []string
|
|
|
|
for _, artifact := range artifacts.List() {
|
|
|
|
result = append(result, artifact.Path)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|