2021-09-18 15:21:29 +02:00
|
|
|
// Package skip can skip an entire Action.
|
|
|
|
package skip
|
|
|
|
|
|
|
|
import (
|
2021-09-18 15:25:28 +02:00
|
|
|
"fmt"
|
|
|
|
|
2022-06-22 02:11:15 +02:00
|
|
|
"github.com/caarlos0/log"
|
2021-09-18 15:21:29 +02:00
|
|
|
"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 15:25:28 +02:00
|
|
|
fmt.Stringer
|
2021-09-18 15:21:29 +02:00
|
|
|
}
|
|
|
|
|
2023-01-29 04:21:43 +02:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2021-09-18 15:21:29 +02:00
|
|
|
// 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 {
|
2023-01-29 04:21:43 +02:00
|
|
|
return Maybe(wrapper{skipper}, next)
|
|
|
|
}
|
|
|
|
if skipper, ok := skipper.(ErrSkipper); ok {
|
2021-09-18 15:21:29 +02:00
|
|
|
return func(ctx *context.Context) error {
|
2023-01-29 04:21:43 +02:00
|
|
|
skip, err := skipper.Skip(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("skip %s: %w", skipper.String(), err)
|
|
|
|
}
|
|
|
|
if skip {
|
2021-09-18 15:25:28 +02:00
|
|
|
log.Debugf("skipped %s", skipper.String())
|
2021-09-18 15:21:29 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return next(ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return next
|
|
|
|
}
|
2023-01-29 04:21:43 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|