github.com/joselitofilho/goreleaser@v0.155.1-0.20210123221854-e4891856c593/internal/pipe/pipe.go (about)

     1  // Package pipe provides generic erros for pipes to use.
     2  package pipe
     3  
     4  import (
     5  	"errors"
     6  	"strings"
     7  )
     8  
     9  // ErrSnapshotEnabled happens when goreleaser is running in snapshot mode.
    10  // It usually means that publishing and maybe some validations were skipped.
    11  var ErrSnapshotEnabled = Skip("disabled during snapshot mode")
    12  
    13  // ErrSkipPublishEnabled happens if --skip-publish is set.
    14  // It means that the part of a Piper that publishes its artifacts was not run.
    15  var ErrSkipPublishEnabled = Skip("publishing is disabled")
    16  
    17  // ErrSkipSignEnabled happens if --skip-sign is set.
    18  // It means that the part of a Piper that signs some things was not run.
    19  var ErrSkipSignEnabled = Skip("artifact signing is disabled")
    20  
    21  // ErrSkipValidateEnabled happens if --skip-validate is set.
    22  // It means that the part of a Piper that validates some things was not run.
    23  var ErrSkipValidateEnabled = Skip("validation is disabled")
    24  
    25  // IsSkip returns true if the error is an ErrSkip.
    26  func IsSkip(err error) bool {
    27  	return errors.As(err, &ErrSkip{})
    28  }
    29  
    30  // ErrSkip occurs when a pipe is skipped for some reason.
    31  type ErrSkip struct {
    32  	reason string
    33  }
    34  
    35  // Error implements the error interface. returns the reason the pipe was skipped.
    36  func (e ErrSkip) Error() string {
    37  	return e.reason
    38  }
    39  
    40  // Skip skips this pipe with the given reason.
    41  func Skip(reason string) ErrSkip {
    42  	return ErrSkip{reason: reason}
    43  }
    44  
    45  // SkipMemento remembers previous skip errors so you can return them all at once later.
    46  type SkipMemento struct {
    47  	skips []string
    48  }
    49  
    50  // Remember a skip.
    51  func (e *SkipMemento) Remember(err error) {
    52  	for _, skip := range e.skips {
    53  		if skip == err.Error() {
    54  			return
    55  		}
    56  	}
    57  	e.skips = append(e.skips, err.Error())
    58  }
    59  
    60  // Evaluate return a skip error with all previous skips, or nil if none happened.
    61  func (e *SkipMemento) Evaluate() error {
    62  	if len(e.skips) == 0 {
    63  		return nil
    64  	}
    65  	return Skip(strings.Join(e.skips, ", "))
    66  }