-
Notifications
You must be signed in to change notification settings - Fork 263
/
snapshot.go
75 lines (67 loc) · 2.66 KB
/
snapshot.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
package dashboardexecute
import (
"context"
"fmt"
"log"
"github.com/turbot/steampipe/pkg/dashboard/dashboardevents"
"github.com/turbot/steampipe/pkg/dashboard/dashboardtypes"
"github.com/turbot/steampipe/pkg/initialisation"
"github.com/turbot/steampipe/pkg/steampipeconfig/modconfig"
)
func GenerateSnapshot(ctx context.Context, target string, initData *initialisation.InitData, inputs map[string]any) (snapshot *dashboardtypes.SteampipeSnapshot, err error) {
w := initData.Workspace
parsedName, err := modconfig.ParseResourceName(target)
if err != nil {
return nil, err
}
// no session for manual execution
sessionId := ""
errorChannel := make(chan error)
resultChannel := make(chan *dashboardtypes.SteampipeSnapshot)
dashboardEventHandler := func(ctx context.Context, event dashboardevents.DashboardEvent) {
handleDashboardEvent(ctx, event, resultChannel, errorChannel)
}
w.RegisterDashboardEventHandler(ctx, dashboardEventHandler)
// clear event handlers again in case another snapshot will be generated in this run
defer w.UnregisterDashboardEventHandlers()
// all runtime dependencies must be resolved before execution (i.e. inputs must be passed in)
Executor.interactive = false
Executor.ExecuteDashboard(ctx, sessionId, target, inputs, w, initData.Client)
select {
case err = <-errorChannel:
return nil, err
case snapshot = <-resultChannel:
// set the filename root of the snapshot
fileRootName, err := parsedName.ToFullNameWithMod(w.Mod.ShortName)
if err != nil {
return nil, err
}
snapshot.FileNameRoot = fileRootName
// return the context error (if any) to ensure we respect cancellation
return snapshot, ctx.Err()
}
}
func handleDashboardEvent(_ context.Context, event dashboardevents.DashboardEvent, resultChannel chan *dashboardtypes.SteampipeSnapshot, errorChannel chan error) {
switch e := event.(type) {
case *dashboardevents.ExecutionError:
errorChannel <- e.Error
case *dashboardevents.ExecutionComplete:
log.Println("[TRACE] execution complete event", *e)
snap := ExecutionCompleteToSnapshot(e)
resultChannel <- snap
}
}
// ExecutionCompleteToSnapshot transforms the ExecutionComplete event into a SteampipeSnapshot
func ExecutionCompleteToSnapshot(event *dashboardevents.ExecutionComplete) *dashboardtypes.SteampipeSnapshot {
return &dashboardtypes.SteampipeSnapshot{
SchemaVersion: fmt.Sprintf("%d", dashboardtypes.SteampipeSnapshotSchemaVersion),
Panels: event.Panels,
Layout: event.Root.AsTreeNode(),
Inputs: event.Inputs,
Variables: event.Variables,
SearchPath: event.SearchPath,
StartTime: event.StartTime,
EndTime: event.EndTime,
Title: event.Root.GetTitle(),
}
}