github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/atc/exec/on_success.go (about)

     1  package exec
     2  
     3  import (
     4  	"context"
     5  )
     6  
     7  // OnSuccessStep will run one step, and then a second step if the first step
     8  // succeeds.
     9  type OnSuccessStep struct {
    10  	step Step
    11  	hook Step
    12  }
    13  
    14  // OnSuccess constructs an OnSuccessStep factory.
    15  func OnSuccess(firstStep Step, secondStep Step) Step {
    16  	return OnSuccessStep{
    17  		step: firstStep,
    18  		hook: secondStep,
    19  	}
    20  }
    21  
    22  // Run will call Run on the first step and wait for it to complete. If the
    23  // first step errors, Run returns the error. OnSuccessStep is ready as soon as
    24  // the first step is ready.
    25  //
    26  // If the first step succeeds (that is, its Success result is true), the second
    27  // step is executed. If the second step errors, its error is returned.
    28  func (o OnSuccessStep) Run(ctx context.Context, state RunState) (bool, error) {
    29  	ok, err := o.step.Run(ctx, state)
    30  	if err != nil {
    31  		return false, err
    32  	}
    33  
    34  	if !ok {
    35  		return false, nil
    36  	}
    37  
    38  	return o.hook.Run(ctx, state)
    39  }