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

     1  package exec
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/pf-qiu/concourse/v6/atc"
     8  	"github.com/pf-qiu/concourse/v6/atc/exec/build"
     9  	"github.com/pf-qiu/concourse/v6/atc/resource"
    10  	"github.com/pf-qiu/concourse/v6/atc/runtime"
    11  )
    12  
    13  type PutInputNotFoundError struct {
    14  	Input string
    15  }
    16  
    17  func (e PutInputNotFoundError) Error() string {
    18  	return fmt.Sprintf("input not found: %s", e.Input)
    19  }
    20  
    21  type PutInputs interface {
    22  	FindAll(*build.Repository) (map[string]runtime.Artifact, error)
    23  }
    24  
    25  type allInputs struct{}
    26  
    27  func NewAllInputs() PutInputs {
    28  	return &allInputs{}
    29  }
    30  
    31  func (i allInputs) FindAll(artifacts *build.Repository) (map[string]runtime.Artifact, error) {
    32  	inputs := map[string]runtime.Artifact{}
    33  
    34  	for name, artifact := range artifacts.AsMap() {
    35  		pi := putInput{
    36  			name:     name,
    37  			artifact: artifact,
    38  		}
    39  
    40  		inputs[pi.DestinationPath()] = pi.Artifact()
    41  	}
    42  
    43  	return inputs, nil
    44  }
    45  
    46  type specificInputs struct {
    47  	inputs []string
    48  }
    49  
    50  func NewSpecificInputs(inputs []string) PutInputs {
    51  	return &specificInputs{
    52  		inputs: inputs,
    53  	}
    54  }
    55  
    56  func (i specificInputs) FindAll(artifacts *build.Repository) (map[string]runtime.Artifact, error) {
    57  	artifactsMap := artifacts.AsMap()
    58  
    59  	inputs := map[string]runtime.Artifact{}
    60  
    61  	for _, i := range i.inputs {
    62  		artifact, found := artifactsMap[build.ArtifactName(i)]
    63  		if !found {
    64  			return nil, PutInputNotFoundError{Input: i}
    65  		}
    66  
    67  		pi := putInput{
    68  			name:     build.ArtifactName(i),
    69  			artifact: artifact,
    70  		}
    71  
    72  		inputs[pi.DestinationPath()] = pi.Artifact()
    73  	}
    74  
    75  	return inputs, nil
    76  }
    77  
    78  type detectInputs struct {
    79  	guessedNames []build.ArtifactName
    80  }
    81  
    82  func detectInputsFromParam(value interface{}) []build.ArtifactName {
    83  	switch actual := value.(type) {
    84  	case string:
    85  		input := actual
    86  		if idx := strings.IndexByte(actual, '/'); idx >= 0 {
    87  			input = actual[:idx]
    88  		}
    89  		return []build.ArtifactName{build.ArtifactName(input)}
    90  	case map[string]interface{}:
    91  		var inputs []build.ArtifactName
    92  		for _, value := range actual {
    93  			inputs = append(inputs, detectInputsFromParam(value)...)
    94  		}
    95  		return inputs
    96  	case []interface{}:
    97  		var inputs []build.ArtifactName
    98  		for _, value := range actual {
    99  			inputs = append(inputs, detectInputsFromParam(value)...)
   100  		}
   101  		return inputs
   102  	default:
   103  		return []build.ArtifactName{}
   104  	}
   105  }
   106  
   107  func NewDetectInputs(params atc.Params) PutInputs {
   108  	return &detectInputs{
   109  		guessedNames: detectInputsFromParam(map[string]interface{}(params)),
   110  	}
   111  }
   112  
   113  func (i detectInputs) FindAll(artifacts *build.Repository) (map[string]runtime.Artifact, error) {
   114  	artifactsMap := artifacts.AsMap()
   115  
   116  	inputs := map[string]runtime.Artifact{}
   117  	for _, name := range i.guessedNames {
   118  		artifact, found := artifactsMap[name]
   119  		if !found {
   120  			// false positive; not an artifact
   121  			continue
   122  		}
   123  
   124  		pi := putInput{
   125  			name:     name,
   126  			artifact: artifact,
   127  		}
   128  
   129  		inputs[pi.DestinationPath()] = pi.Artifact()
   130  	}
   131  
   132  	return inputs, nil
   133  }
   134  
   135  type putInput struct {
   136  	name     build.ArtifactName
   137  	artifact runtime.Artifact
   138  }
   139  
   140  func (input putInput) Artifact() runtime.Artifact { return input.artifact }
   141  
   142  func (input putInput) DestinationPath() string {
   143  	return resource.ResourcesDir("put/" + string(input.name))
   144  }