-
Notifications
You must be signed in to change notification settings - Fork 214
/
Copy pathaction.go
167 lines (139 loc) · 3.65 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
166
167
package exec
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:
// exec:
// ...
// helm:
// ...
func (a Action) MarshalYAML() (interface{}, error) {
return map[string]interface{}{a.Name: a.Steps}, nil
}
// MakeSteps builds a slice of Steps for data to be unmarshaled into.
func (a Action) MakeSteps() interface{} {
return &[]Step{}
}
// UnmarshalYAML takes any yaml in this form
// ACTION:
// - exec: ...
// 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 {
steps := make([]builder.ExecutableStep, len(a.Steps))
for i := range a.Steps {
steps[i] = a.Steps[i]
}
return steps
}
// 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:
// exec:
// ...
// exec:
// ...
// upgrade:
// exec:
// ...
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 = Step{}
var _ builder.ExecutableStep = Step{}
var _ builder.StepWithOutputs = Step{}
type Step struct {
Instruction `yaml:"exec"`
}
type Instruction struct {
Description string `yaml:"description"`
Command string `yaml:"command"`
WorkingDir string `yaml:"dir,omitempty"`
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 Step) GetCommand() string {
return s.Command
}
func (s Step) GetArguments() []string {
return s.Arguments
}
func (s Step) GetSuffixArguments() []string {
return s.SuffixArguments
}
func (s Step) GetFlags() builder.Flags {
return s.Flags
}
func (s Step) SuppressesOutput() bool {
return s.SuppressOutput
}
func (s Step) GetOutputs() []builder.Output {
outputs := make([]builder.Output, len(s.Outputs))
for i := range s.Outputs {
outputs[i] = s.Outputs[i]
}
return outputs
}
func (s Step) GetWorkingDir() string {
return s.WorkingDir
}
var _ builder.OutputRegex = Output{}
var _ builder.OutputFile = Output{}
var _ builder.OutputJsonPath = Output{}
type Output struct {
Name string `yaml:"name"`
FilePath string `yaml:"path,omitempty"`
JsonPath string `yaml:"jsonPath,omitempty"`
Regex string `yaml:"regex,omitempty"`
}
func (o Output) GetName() string {
return o.Name
}
func (o Output) GetFilePath() string {
return o.FilePath
}
func (o Output) GetJsonPath() string {
return o.JsonPath
}
func (o Output) GetRegex() string {
return o.Regex
}