generated from getporter/skeletor
/
action.go
165 lines (137 loc) · 3.89 KB
/
action.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package kfctl
import (
"get.porter.sh/porter/pkg/exec/builder"
)
var _ builder.ExecutableAction = Action{}
var _ builder.BuildableAction = Action{}
type Action struct {
Name string
Steps []Step // using UnmarshalYAML so that we don't need a custom type per action
}
// MarshalYAML converts the action back to a YAML representation
// install:
// kfctl:
// ...
func (a Action) MarshalYAML() (interface{}, error) {
return map[string]interface{}{a.Name: a.Steps}, nil
}
// MakeSteps builds a slice of Step for data to be unmarshaled into.
func (a Action) MakeSteps() interface{} {
return &[]Step{}
}
// UnmarshalYAML takes any yaml in this form
// ACTION:
// - kfctl: ...
// and puts the steps into the Action.Steps field
func (a *Action) UnmarshalYAML(unmarshal func(interface{}) error) error {
results, err := builder.UnmarshalAction(unmarshal, a)
if err != nil {
return err
}
for actionName, action := range results {
a.Name = actionName
for _, result := range action {
step := result.(*[]Step)
a.Steps = append(a.Steps, *step...)
}
break // There is only 1 action
}
return nil
}
func (a Action) GetSteps() []builder.ExecutableStep {
// Go doesn't have generics, nothing to see here...
steps := make([]builder.ExecutableStep, len(a.Steps))
for i := range a.Steps {
steps[i] = a.Steps[i]
}
return steps
}
type Step struct {
Instruction `yaml:"kfctl"`
}
// Actions is a set of actions, and the steps, passed from Porter.
type Actions []Action
// UnmarshalYAML takes chunks of a porter.yaml file associated with this mixin
// and populates it on the current action set.
// install:
// kfctl:
// ...
// kfctl:
// ...
// upgrade:
// kfctl:
// ...
func (a *Actions) UnmarshalYAML(unmarshal func(interface{}) error) error {
results, err := builder.UnmarshalAction(unmarshal, Action{})
if err != nil {
return err
}
for actionName, action := range results {
for _, result := range action {
s := result.(*[]Step)
*a = append(*a, Action{
Name: actionName,
Steps: *s,
})
}
}
return nil
}
var _ builder.HasOrderedArguments = Instruction{}
var _ builder.ExecutableStep = Instruction{}
var _ builder.StepWithOutputs = Instruction{}
type Instruction struct {
Name string `yaml:"name"`
Description string `yaml:"description"`
Arguments []string `yaml:"arguments,omitempty"`
SuffixArguments []string `yaml:"suffix-arguments,omitempty"`
Flags builder.Flags `yaml:"flags,omitempty"`
Outputs []Output `yaml:"outputs,omitempty"`
SuppressOutput bool `yaml:"suppress-output,omitempty"`
}
func (s Instruction) GetCommand() string {
return "kfctl"
}
func (s Instruction) GetArguments() []string {
return s.Arguments
}
func (s Instruction) GetSuffixArguments() []string {
return s.SuffixArguments
}
func (s Instruction) GetFlags() builder.Flags {
return s.Flags
}
func (s Instruction) SuppressesOutput() bool {
return s.SuppressOutput
}
func (s Instruction) GetOutputs() []builder.Output {
// Go doesn't have generics, nothing to see here...
outputs := make([]builder.Output, len(s.Outputs))
for i := range s.Outputs {
outputs[i] = s.Outputs[i]
}
return outputs
}
var _ builder.OutputJsonPath = Output{}
var _ builder.OutputFile = Output{}
var _ builder.OutputRegex = Output{}
type Output struct {
Name string `yaml:"name"`
// See https://porter.sh/mixins/exec/#outputs
// TODO: If your mixin doesn't support these output types, you can remove these and the interface assertions above, and from #/definitions/outputs in schema.json
JsonPath string `yaml:"jsonPath,omitempty"`
FilePath string `yaml:"path,omitempty"`
Regex string `yaml:"regex,omitempty"`
}
func (o Output) GetName() string {
return o.Name
}
func (o Output) GetJsonPath() string {
return o.JsonPath
}
func (o Output) GetFilePath() string {
return o.FilePath
}
func (o Output) GetRegex() string {
return o.Regex
}