Files
goreleaser/internal/middleware/skip/skip.go
T

63 lines
1.5 KiB
Go
Raw Normal View History

// Package skip can skip an entire Action.
package skip
import (
2021-09-18 10:25:28 -03:00
"fmt"
2022-06-21 21:11:15 -03:00
"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
2021-09-18 10:25:28 -03:00
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 {
2021-09-18 10:25:28 -03:00
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
}