-
Notifications
You must be signed in to change notification settings - Fork 263
/
snapshot.go
76 lines (65 loc) · 2.38 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
76
package controldisplay
import (
"context"
"fmt"
"github.com/spf13/viper"
"github.com/turbot/steampipe/pkg/cloud"
"github.com/turbot/steampipe/pkg/constants"
"github.com/turbot/steampipe/pkg/control/controlexecute"
"github.com/turbot/steampipe/pkg/dashboard/dashboardexecute"
"github.com/turbot/steampipe/pkg/dashboard/dashboardtypes"
"github.com/turbot/steampipe/pkg/statushooks"
"github.com/turbot/steampipe/pkg/steampipeconfig/modconfig"
)
func executionTreeToSnapshot(e *controlexecute.ExecutionTree) (*dashboardtypes.SteampipeSnapshot, error) {
var dashboardNode modconfig.DashboardLeafNode
var panels map[string]dashboardtypes.SnapshotPanel
var checkRun *dashboardexecute.CheckRun
// get root benchmark/control
switch root := e.Root.Children[0].(type) {
case *controlexecute.ResultGroup:
var ok bool
dashboardNode, ok = root.GroupItem.(modconfig.DashboardLeafNode)
if !ok {
return nil, fmt.Errorf("invalid node found in control execution tree - cannot cast '%s' to a DashboardLeafNode", root.GroupItem.Name())
}
case *controlexecute.ControlRun:
dashboardNode = root.Control
}
// TACTICAL create a check run to wrap the execution tree
checkRun = &dashboardexecute.CheckRun{Root: e.Root.Children[0]}
checkRun.DashboardTreeRunImpl = dashboardexecute.NewDashboardTreeRunImpl(dashboardNode, nil, checkRun, nil)
// populate the panels
panels = checkRun.BuildSnapshotPanels(make(map[string]dashboardtypes.SnapshotPanel))
// create the snapshot
res := &dashboardtypes.SteampipeSnapshot{
SchemaVersion: fmt.Sprintf("%d", dashboardtypes.SteampipeSnapshotSchemaVersion),
Panels: panels,
Layout: checkRun.Root.AsTreeNode(),
Inputs: map[string]interface{}{},
Variables: dashboardexecute.GetReferencedVariables(checkRun, e.Workspace),
SearchPath: e.SearchPath,
StartTime: e.StartTime,
EndTime: e.EndTime,
Title: dashboardNode.GetTitle(),
FileNameRoot: dashboardNode.Name(),
}
return res, nil
}
func PublishSnapshot(ctx context.Context, e *controlexecute.ExecutionTree, shouldShare bool) error {
statushooks.Show(ctx)
defer statushooks.Done(ctx)
snapshot, err := executionTreeToSnapshot(e)
if err != nil {
return err
}
message, err := cloud.PublishSnapshot(ctx, snapshot, shouldShare)
if err != nil {
return err
}
if viper.GetBool(constants.ArgProgress) {
statushooks.Done(ctx)
fmt.Println(message)
}
return nil
}