1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-10 03:47:03 +02:00
goreleaser/internal/middleware/skip/skip.go
Carlos Alexandro Becker 90a82157ca
refactor: improve tmpls that eval to a bool (#3726)
- added a tmpl.Bool that checks if the result of a template is "true"
- added an ErrSkipper interface so Skip() methods can return errors as well

---------

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
2023-01-28 23:21:43 -03:00

63 lines
1.5 KiB
Go

// Package skip can skip an entire Action.
package skip
import (
"fmt"
"github.com/caarlos0/log"
"github.com/goreleaser/goreleaser/internal/middleware"
"github.com/goreleaser/goreleaser/pkg/context"
)
// Skipper defines a method to skip an entire Piper.
type Skipper interface {
// Skip returns true if the Piper should be skipped.
Skip(ctx *context.Context) bool
fmt.Stringer
}
// Skipper defines a method to skip an entire Piper.
type ErrSkipper interface {
// Skip returns true if the Piper should be skipped.
Skip(ctx *context.Context) (bool, error)
fmt.Stringer
}
// Maybe returns an action that skips immediately if the given p is a Skipper
// and its Skip method returns true.
func Maybe(skipper interface{}, next middleware.Action) middleware.Action {
if skipper, ok := skipper.(Skipper); ok {
return Maybe(wrapper{skipper}, next)
}
if skipper, ok := skipper.(ErrSkipper); ok {
return func(ctx *context.Context) error {
skip, err := skipper.Skip(ctx)
if err != nil {
return fmt.Errorf("skip %s: %w", skipper.String(), err)
}
if skip {
log.Debugf("skipped %s", skipper.String())
return nil
}
return next(ctx)
}
}
return next
}
var _ ErrSkipper = wrapper{}
type wrapper struct {
skipper Skipper
}
// String implements SkipperErr
func (w wrapper) String() string {
return w.skipper.String()
}
// Skip implements SkipperErr
func (w wrapper) Skip(ctx *context.Context) (bool, error) {
return w.skipper.Skip(ctx), nil
}