get.porter.sh/porter@v1.3.0/pkg/storage/output.go (about)

     1  package storage
     2  
     3  import (
     4  	"sort"
     5  
     6  	"get.porter.sh/porter/pkg/cnab"
     7  	"github.com/cnabio/cnab-go/bundle/definition"
     8  )
     9  
    10  var _ Document = Output{}
    11  
    12  type Output struct {
    13  	SchemaVersion cnab.SchemaVersion `json:"schemaVersion"`
    14  	Name          string             `json:"name"`
    15  	Namespace     string             `json:"namespace"`
    16  	Installation  string             `json:"installation"`
    17  	RunID         string             `json:"runId"`
    18  	ResultID      string             `json:"resultId"`
    19  
    20  	// Key holds the secret key to retrieve a sensitive output value
    21  	Key   string `json:"key"`
    22  	Value []byte `json:"value"`
    23  }
    24  
    25  func (o Output) DefaultDocumentFilter() map[string]interface{} {
    26  	return map[string]interface{}{"resultId": o.ResultID, "name": o.Name}
    27  }
    28  
    29  // GetSchema returns the schema for the output from the specified bundle, or
    30  // false if the schema is not defined.
    31  func (o Output) GetSchema(b cnab.ExtendedBundle) (definition.Schema, bool) {
    32  	if def, ok := b.Outputs[o.Name]; ok {
    33  		if schema, ok := b.Definitions[def.Definition]; ok {
    34  			return *schema, ok
    35  		}
    36  	}
    37  
    38  	return definition.Schema{}, false
    39  }
    40  
    41  type Outputs struct {
    42  	// Sorted list of outputs
    43  	vals []Output
    44  	// output name -> index of the output in vals
    45  	keys map[string]int
    46  }
    47  
    48  func NewOutputs(outputs []Output) Outputs {
    49  	o := Outputs{
    50  		vals: make([]Output, len(outputs)),
    51  		keys: make(map[string]int, len(outputs)),
    52  	}
    53  
    54  	copy(o.vals, outputs)
    55  	for i, output := range outputs {
    56  		o.keys[output.Name] = i
    57  	}
    58  
    59  	sort.Sort(o)
    60  	return o
    61  }
    62  
    63  func (o Outputs) GetByName(name string) (Output, bool) {
    64  	i, ok := o.keys[name]
    65  	if !ok || i >= len(o.vals) {
    66  		return Output{}, false
    67  	}
    68  
    69  	return o.vals[i], true
    70  }
    71  
    72  func (o Outputs) GetByIndex(i int) (Output, bool) {
    73  	if i < 0 || i >= len(o.vals) {
    74  		return Output{}, false
    75  	}
    76  
    77  	return o.vals[i], true
    78  }
    79  
    80  // Value returns a list of outputs.
    81  func (o Outputs) Value() []Output {
    82  	return o.vals
    83  }
    84  
    85  func (o Outputs) Len() int {
    86  	return len(o.vals)
    87  }
    88  
    89  func (o Outputs) Less(i, j int) bool {
    90  	return o.vals[i].Name < o.vals[j].Name
    91  }
    92  
    93  func (o Outputs) Swap(i, j int) {
    94  	o.keys[o.vals[i].Name] = j
    95  	o.keys[o.vals[j].Name] = i
    96  	o.vals[i], o.vals[j] = o.vals[j], o.vals[i]
    97  }