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

     1  package exec
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/pf-qiu/concourse/v6/atc"
     7  	"github.com/pf-qiu/concourse/v6/atc/runtime"
     8  )
     9  
    10  func NewVersionSourceFromPlan(getPlan *atc.GetPlan) VersionSource {
    11  	if getPlan.Version != nil {
    12  		return &StaticVersionSource{
    13  			version: *getPlan.Version,
    14  		}
    15  	} else if getPlan.VersionFrom != nil {
    16  		return &PutStepVersionSource{
    17  			planID: *getPlan.VersionFrom,
    18  		}
    19  	} else {
    20  		return &EmptyVersionSource{}
    21  	}
    22  }
    23  
    24  type VersionSource interface {
    25  	Version(RunState) (atc.Version, error)
    26  }
    27  
    28  type StaticVersionSource struct {
    29  	version atc.Version
    30  }
    31  
    32  func (p *StaticVersionSource) Version(RunState) (atc.Version, error) {
    33  	return p.version, nil
    34  }
    35  
    36  var ErrPutStepVersionMissing = errors.New("version is missing from put step")
    37  
    38  type PutStepVersionSource struct {
    39  	planID atc.PlanID
    40  }
    41  
    42  func (p *PutStepVersionSource) Version(state RunState) (atc.Version, error) {
    43  	var info runtime.VersionResult
    44  	if !state.Result(p.planID, &info) {
    45  		return atc.Version{}, ErrPutStepVersionMissing
    46  	}
    47  
    48  	return info.Version, nil
    49  }
    50  
    51  type EmptyVersionSource struct{}
    52  
    53  func (p *EmptyVersionSource) Version(RunState) (atc.Version, error) {
    54  	return atc.Version{}, nil
    55  }