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