Files
osmedeus/internal/executor/state_export.go
T

62 lines
1.7 KiB
Go

package executor
import (
"github.com/j3ssie/osmedeus/v5/internal/core"
"github.com/j3ssie/osmedeus/v5/internal/state"
)
// ExportState exports the current run state to a JSON file
// It uses database data if available, otherwise falls back to in-memory data from result and execCtx
func ExportState(stateFile string, result *core.WorkflowResult, execCtx *core.ExecutionContext) error {
ctx := buildExportContext(result, execCtx)
return state.Export(stateFile, ctx)
}
func buildExportContext(result *core.WorkflowResult, execCtx *core.ExecutionContext) *state.ExportContext {
ctx := &state.ExportContext{}
// Populate from execCtx
if execCtx != nil {
ctx.RunID = execCtx.RunID
ctx.WorkflowName = execCtx.WorkflowName
ctx.WorkflowKind = string(execCtx.WorkflowKind)
ctx.Target = execCtx.Target
ctx.WorkspacePath = execCtx.WorkspacePath
ctx.WorkspaceName = execCtx.WorkspaceName
ctx.Params = execCtx.Params
}
// Populate/override from result
if result != nil {
if ctx.RunID == "" {
ctx.RunID = result.RunID
}
if ctx.WorkflowName == "" {
ctx.WorkflowName = result.WorkflowName
}
ctx.WorkflowKind = string(result.WorkflowKind)
ctx.Target = result.Target
ctx.Status = string(result.Status)
startTime := result.StartTime
endTime := result.EndTime
ctx.StartedAt = &startTime
ctx.CompletedAt = &endTime
ctx.TotalSteps = len(result.Steps)
completedSteps := 0
for _, step := range result.Steps {
if step.Status == core.StepStatusSuccess {
completedSteps++
}
}
ctx.CompletedSteps = completedSteps
if result.Error != nil {
ctx.ErrorMessage = result.Error.Error()
}
ctx.Artifacts = result.Artifacts
}
return ctx
}