-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathengine.go
204 lines (170 loc) · 5.85 KB
/
engine.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package engine
import (
"errors"
"fmt"
"os"
"strings"
"github.com/Azure/InnovationEngine/internal/az"
"github.com/Azure/InnovationEngine/internal/engine/common"
"github.com/Azure/InnovationEngine/internal/engine/environments"
"github.com/Azure/InnovationEngine/internal/engine/interactive"
"github.com/Azure/InnovationEngine/internal/engine/test"
"github.com/Azure/InnovationEngine/internal/lib"
"github.com/Azure/InnovationEngine/internal/lib/fs"
"github.com/Azure/InnovationEngine/internal/logging"
"github.com/Azure/InnovationEngine/internal/ui"
tea "github.com/charmbracelet/bubbletea"
)
// Configuration for the engine.
type EngineConfiguration struct {
Verbose bool
DoNotDelete bool
CorrelationId string
Subscription string
Environment string
WorkingDirectory string
RenderValues bool
ReportFile string
}
type Engine struct {
Configuration EngineConfiguration
}
// / Create a new engine instance.
func NewEngine(configuration EngineConfiguration) (*Engine, error) {
return &Engine{
Configuration: configuration,
}, nil
}
// Executes a markdown scenario.
func (e *Engine) ExecuteScenario(scenario *common.Scenario) error {
return fs.UsingDirectory(e.Configuration.WorkingDirectory, func() error {
az.SetCorrelationId(e.Configuration.CorrelationId, scenario.Environment)
// Execute the steps
fmt.Println(ui.ScenarioTitleStyle.Render(scenario.Name))
err := e.ExecuteAndRenderSteps(scenario.Steps, lib.CopyMap(scenario.Environment))
return err
})
}
// Executes a scenario in testing moe. This mode goes over each code block
// and executes it without user interaction.
func (e *Engine) TestScenario(scenario *common.Scenario) error {
return fs.UsingDirectory(e.Configuration.WorkingDirectory, func() error {
az.SetCorrelationId(e.Configuration.CorrelationId, scenario.Environment)
stepsToExecute := filterDeletionCommands(scenario.Steps, e.Configuration.DoNotDelete)
initialEnvironmentVariables := lib.GetEnvironmentVariables()
model, err := test.NewTestModeModel(
scenario.Name,
e.Configuration.Subscription,
e.Configuration.Environment,
stepsToExecute,
lib.CopyMap(scenario.Environment),
)
if err != nil {
return err
}
var flags []tea.ProgramOption
if environments.EnvironmentsGithubAction == e.Configuration.Environment {
flags = append(
flags,
tea.WithoutRenderer(),
tea.WithOutput(os.Stdout),
tea.WithInput(os.Stdin),
)
} else {
flags = append(flags, tea.WithAltScreen(), tea.WithMouseCellMotion())
}
common.Program = tea.NewProgram(model, flags...)
var finalModel tea.Model
finalModel, err = common.Program.Run()
// TODO(vmarcella): After testing is complete, we should generate a report.
model, ok := finalModel.(test.TestModeModel)
if !ok {
err = errors.Join(err, fmt.Errorf("failed to cast tea.Model to TestModeModel"))
return err
}
if e.Configuration.ReportFile != "" {
allEnvironmentVariables, envErr := lib.LoadEnvironmentStateFile(
lib.DefaultEnvironmentStateFile,
)
if envErr != nil {
logging.GlobalLogger.Errorf("Failed to load environment state file: %s", err)
err = errors.Join(err, fmt.Errorf("failed to load environment state file: %s", err))
return err
}
variablesDeclaredByScenario := lib.DiffMapsByKey(
allEnvironmentVariables,
initialEnvironmentVariables,
)
report := common.BuildReport(scenario.Name)
err = report.
WithProperties(scenario.Properties).
WithEnvironmentVariables(variablesDeclaredByScenario).
WithError(model.GetFailure()).
WithCodeBlocks(model.GetCodeBlocks()).
WriteToJSONFile(e.Configuration.ReportFile)
if err != nil {
err = errors.Join(err, fmt.Errorf("failed to write report to file: %s", err))
return err
}
model.CommandLines = append(
model.CommandLines,
"Report written to "+e.Configuration.ReportFile,
)
}
fmt.Println(strings.Join(model.CommandLines, "\n"))
err = errors.Join(err, model.GetFailure())
if err != nil {
logging.GlobalLogger.Errorf("Failed to run ie test %s", err)
return err
}
return nil
})
}
// Executes a Scenario in interactive mode. This mode goes over each codeblock
// step by step and allows the user to interact with the codeblock.
func (e *Engine) InteractWithScenario(scenario *common.Scenario) error {
return fs.UsingDirectory(e.Configuration.WorkingDirectory, func() error {
az.SetCorrelationId(e.Configuration.CorrelationId, scenario.Environment)
stepsToExecute := filterDeletionCommands(scenario.Steps, e.Configuration.DoNotDelete)
model, err := interactive.NewInteractiveModeModel(
scenario.Name,
e.Configuration.Subscription,
e.Configuration.Environment,
stepsToExecute,
lib.CopyMap(scenario.Environment),
)
if err != nil {
return err
}
common.Program = tea.NewProgram(model, tea.WithAltScreen(), tea.WithMouseCellMotion())
var finalModel tea.Model
var ok bool
finalModel, err = common.Program.Run()
model, ok = finalModel.(interactive.InteractiveModeModel)
if environments.EnvironmentsAzure == e.Configuration.Environment {
if !ok {
return fmt.Errorf("failed to cast tea.Model to InteractiveModeModel")
}
logging.GlobalLogger.Info("Writing session output to stdout")
fmt.Println(strings.Join(model.CommandLines, "\n"))
}
switch e.Configuration.Environment {
case environments.EnvironmentsAzure, environments.EnvironmentsOCD:
logging.GlobalLogger.Info(
"Cleaning environment variable file located at /tmp/env-vars",
)
err := lib.CleanEnvironmentStateFile(lib.DefaultEnvironmentStateFile)
if err != nil {
logging.GlobalLogger.Errorf("Error cleaning environment variables: %s", err.Error())
return err
}
default:
lib.DeleteEnvironmentStateFile(lib.DefaultEnvironmentStateFile)
}
if err != nil {
logging.GlobalLogger.Errorf("Failed to run program %s", err)
return err
}
return nil
})
}