gitee.com/mirrors_opencollective/goreleaser@v0.45.0/internal/artifact/artifact.go (about)

     1  // Package artifact provides the core artifact storage for goreleaser
     2  package artifact
     3  
     4  import (
     5  	"sync"
     6  
     7  	"github.com/apex/log"
     8  )
     9  
    10  // Type defines the type of an artifact
    11  //go:generate stringer -type=Type
    12  type Type int
    13  
    14  const (
    15  	// UploadableArchive a tar.gz/zip archive to be uploaded
    16  	UploadableArchive Type = iota
    17  	// UploadableBinary is a binary file to be uploaded
    18  	UploadableBinary
    19  	// Binary is a binary (output of a gobuild)
    20  	Binary
    21  	// LinuxPackage is a linux package generated by fpm or snapcraft
    22  	LinuxPackage
    23  	// DockerImage is a docker image
    24  	DockerImage
    25  	// Checksum is a checksums file
    26  	Checksum
    27  	// Signature is a signature file
    28  	Signature
    29  )
    30  
    31  // Artifact represents an artifact and its relevant info
    32  type Artifact struct {
    33  	Name   string
    34  	Path   string
    35  	Goos   string
    36  	Goarch string
    37  	Goarm  string
    38  	Type   Type
    39  	Extra  map[string]string
    40  }
    41  
    42  // Artifacts is a list of artifacts
    43  type Artifacts struct {
    44  	items []Artifact
    45  	lock  *sync.Mutex
    46  }
    47  
    48  // New return a new list of artifacts
    49  func New() Artifacts {
    50  	return Artifacts{
    51  		items: []Artifact{},
    52  		lock:  &sync.Mutex{},
    53  	}
    54  }
    55  
    56  // List return the actual list of artifacts
    57  func (artifacts Artifacts) List() []Artifact {
    58  	return artifacts.items
    59  }
    60  
    61  // GroupByPlatform groups the artifacts by their platform
    62  func (artifacts Artifacts) GroupByPlatform() map[string][]Artifact {
    63  	var result = map[string][]Artifact{}
    64  	for _, a := range artifacts.items {
    65  		plat := a.Goos + a.Goarch + a.Goarm
    66  		result[plat] = append(result[plat], a)
    67  	}
    68  	return result
    69  }
    70  
    71  // Add safely adds a new artifact to an artifact list
    72  func (artifacts *Artifacts) Add(a Artifact) {
    73  	artifacts.lock.Lock()
    74  	defer artifacts.lock.Unlock()
    75  	log.WithFields(log.Fields{
    76  		"name": a.Name,
    77  		"path": a.Path,
    78  		"type": a.Type,
    79  	}).Info("added new artifact")
    80  	artifacts.items = append(artifacts.items, a)
    81  }
    82  
    83  // Filter defines an artifact filter which can be used within the Filter
    84  // function
    85  type Filter func(a Artifact) bool
    86  
    87  // ByGoos is a predefined filter that filters by the given goos
    88  func ByGoos(s string) Filter {
    89  	return func(a Artifact) bool {
    90  		return a.Goos == s
    91  	}
    92  }
    93  
    94  // ByGoarch is a predefined filter that filters by the given goarch
    95  func ByGoarch(s string) Filter {
    96  	return func(a Artifact) bool {
    97  		return a.Goarch == s
    98  	}
    99  }
   100  
   101  // ByGoarm is a predefined filter that filters by the given goarm
   102  func ByGoarm(s string) Filter {
   103  	return func(a Artifact) bool {
   104  		return a.Goarm == s
   105  	}
   106  }
   107  
   108  // ByType is a predefined filter that filters by the given type
   109  func ByType(t Type) Filter {
   110  	return func(a Artifact) bool {
   111  		return a.Type == t
   112  	}
   113  }
   114  
   115  // Or performs an OR between all given filters
   116  func Or(filters ...Filter) Filter {
   117  	return func(a Artifact) bool {
   118  		for _, f := range filters {
   119  			if f(a) {
   120  				return true
   121  			}
   122  		}
   123  		return false
   124  	}
   125  }
   126  
   127  // And performs an AND between all given filters
   128  func And(filters ...Filter) Filter {
   129  	return func(a Artifact) bool {
   130  		for _, f := range filters {
   131  			if !f(a) {
   132  				return false
   133  			}
   134  		}
   135  		return true
   136  	}
   137  }
   138  
   139  // Filter filters the artifact list, returning a new instance.
   140  // There are some pre-defined filters but anything of the Type Filter
   141  // is accepted.
   142  // You can compose filters by using the And and Or filters.
   143  func (artifacts *Artifacts) Filter(filter Filter) Artifacts {
   144  	var result = New()
   145  	for _, a := range artifacts.items {
   146  		if filter(a) {
   147  			result.items = append(result.items, a)
   148  		}
   149  	}
   150  	return result
   151  }