github.com/goreleaser/goreleaser@v1.25.1/internal/skips/skips.go (about)

     1  package skips
     2  
     3  import (
     4  	"fmt"
     5  	"sort"
     6  	"strings"
     7  
     8  	"github.com/goreleaser/goreleaser/pkg/context"
     9  	"golang.org/x/exp/maps"
    10  	"golang.org/x/exp/slices"
    11  )
    12  
    13  type Key string
    14  
    15  const (
    16  	PreBuildHooks  Key = "pre-hooks"
    17  	PostBuildHooks Key = "post-hooks"
    18  	Publish        Key = "publish"
    19  	Announce       Key = "announce"
    20  	Sign           Key = "sign"
    21  	Validate       Key = "validate"
    22  	SBOM           Key = "sbom"
    23  	Ko             Key = "ko"
    24  	Docker         Key = "docker"
    25  	Before         Key = "before"
    26  	Winget         Key = "winget"
    27  	Snapcraft      Key = "snapcraft"
    28  	Scoop          Key = "scoop"
    29  	Homebrew       Key = "homebrew"
    30  	Nix            Key = "nix"
    31  	AUR            Key = "aur"
    32  	NFPM           Key = "nfpm"
    33  	Chocolatey     Key = "chocolatey"
    34  )
    35  
    36  func String(ctx *context.Context) string {
    37  	keys := maps.Keys(ctx.Skips)
    38  	sort.Strings(keys)
    39  	str := strings.Join(keys, ", ")
    40  	if idx := strings.LastIndex(str, ","); idx > -1 {
    41  		str = str[:idx] + " and" + str[idx+1:]
    42  	}
    43  	return str
    44  }
    45  
    46  func Any(ctx *context.Context, keys ...Key) bool {
    47  	for _, key := range keys {
    48  		if ctx.Skips[string(key)] {
    49  			return true
    50  		}
    51  	}
    52  	return false
    53  }
    54  
    55  func Set(ctx *context.Context, keys ...Key) {
    56  	for _, key := range keys {
    57  		ctx.Skips[string(key)] = true
    58  	}
    59  }
    60  
    61  var (
    62  	SetRelease = set(Release)
    63  	SetBuild   = set(Build)
    64  )
    65  
    66  func set(allowed Keys) func(ctx *context.Context, keys ...string) error {
    67  	return func(ctx *context.Context, keys ...string) error {
    68  		for _, key := range keys {
    69  			if !slices.Contains(allowed, Key(key)) {
    70  				return fmt.Errorf("--skip=%s is not allowed. Valid options for skip are [%s]", key, allowed)
    71  			}
    72  			ctx.Skips[key] = true
    73  		}
    74  		return nil
    75  	}
    76  }
    77  
    78  type Keys []Key
    79  
    80  func (keys Keys) String() string {
    81  	ss := make([]string, len(keys))
    82  	for i, key := range keys {
    83  		ss[i] = string(key)
    84  	}
    85  	slices.Sort(ss)
    86  	return strings.Join(ss, ", ")
    87  }
    88  
    89  func (keys Keys) Complete(prefix string) []string {
    90  	var result []string
    91  	for _, k := range keys {
    92  		if strings.HasPrefix(string(k), strings.ToLower(prefix)) {
    93  			result = append(result, string(k))
    94  		}
    95  	}
    96  	sort.Strings(result)
    97  	return result
    98  }
    99  
   100  var Release = Keys{
   101  	Publish,
   102  	Announce,
   103  	Sign,
   104  	Validate,
   105  	SBOM,
   106  	Ko,
   107  	Docker,
   108  	Winget,
   109  	Chocolatey,
   110  	Snapcraft,
   111  	Scoop,
   112  	Homebrew,
   113  	Nix,
   114  	AUR,
   115  	NFPM,
   116  	Before,
   117  }
   118  
   119  var Build = Keys{
   120  	PreBuildHooks,
   121  	PostBuildHooks,
   122  	Validate,
   123  	Before,
   124  }