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

     1  package exec
     2  
     3  import (
     4  	"context"
     5  	"reflect"
     6  	"sync"
     7  
     8  	"github.com/pf-qiu/concourse/v6/atc"
     9  	"github.com/pf-qiu/concourse/v6/atc/exec/build"
    10  	"github.com/pf-qiu/concourse/v6/vars"
    11  )
    12  
    13  type runState struct {
    14  	stepper Stepper
    15  
    16  	vars *buildVariables
    17  
    18  	artifacts *build.Repository
    19  	results   *sync.Map
    20  
    21  	parent RunState
    22  }
    23  
    24  type Stepper func(atc.Plan) Step
    25  
    26  func NewRunState(
    27  	stepper Stepper,
    28  	credVars vars.Variables,
    29  	enableRedaction bool,
    30  ) RunState {
    31  	return &runState{
    32  		stepper: stepper,
    33  
    34  		vars: newBuildVariables(credVars, enableRedaction),
    35  
    36  		artifacts: build.NewRepository(),
    37  		results:   &sync.Map{},
    38  	}
    39  }
    40  
    41  func (state *runState) ArtifactRepository() *build.Repository {
    42  	return state.artifacts
    43  }
    44  
    45  func (state *runState) Result(id atc.PlanID, to interface{}) bool {
    46  	val, ok := state.results.Load(id)
    47  	if !ok {
    48  		return false
    49  	}
    50  
    51  	if reflect.TypeOf(val).AssignableTo(reflect.TypeOf(to).Elem()) {
    52  		reflect.ValueOf(to).Elem().Set(reflect.ValueOf(val))
    53  		return true
    54  	}
    55  
    56  	return false
    57  }
    58  
    59  func (state *runState) StoreResult(id atc.PlanID, val interface{}) {
    60  	state.results.Store(id, val)
    61  }
    62  
    63  func (state *runState) Get(ref vars.Reference) (interface{}, bool, error) {
    64  	return state.vars.Get(ref)
    65  }
    66  
    67  func (state *runState) List() ([]vars.Reference, error) {
    68  	return state.vars.List()
    69  }
    70  
    71  func (state *runState) IterateInterpolatedCreds(iter vars.TrackedVarsIterator) {
    72  	state.vars.IterateInterpolatedCreds(iter)
    73  }
    74  
    75  func (state *runState) NewLocalScope() RunState {
    76  	clone := *state
    77  	clone.vars = state.vars.NewLocalScope()
    78  	clone.artifacts = state.artifacts.NewLocalScope()
    79  	clone.parent = state
    80  	return &clone
    81  }
    82  
    83  func (state *runState) Parent() RunState {
    84  	return state.parent
    85  }
    86  
    87  func (state *runState) AddLocalVar(name string, val interface{}, redact bool) {
    88  	state.vars.AddLocalVar(name, val, redact)
    89  }
    90  
    91  func (state *runState) RedactionEnabled() bool {
    92  	return state.vars.RedactionEnabled()
    93  }
    94  
    95  func (state *runState) Run(ctx context.Context, plan atc.Plan) (bool, error) {
    96  	return state.stepper(plan).Run(ctx, state)
    97  }