github.com/turbot/steampipe@v1.7.0-rc.0.0.20240517123944-7cef272d4458/pkg/control/controldisplay/snapshot.go (about)

     1  package controldisplay
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/spf13/viper"
     8  	"github.com/turbot/steampipe/pkg/cloud"
     9  	"github.com/turbot/steampipe/pkg/constants"
    10  	"github.com/turbot/steampipe/pkg/control/controlexecute"
    11  	"github.com/turbot/steampipe/pkg/dashboard/dashboardexecute"
    12  	"github.com/turbot/steampipe/pkg/dashboard/dashboardtypes"
    13  	"github.com/turbot/steampipe/pkg/statushooks"
    14  	"github.com/turbot/steampipe/pkg/steampipeconfig/modconfig"
    15  )
    16  
    17  func executionTreeToSnapshot(e *controlexecute.ExecutionTree) (*dashboardtypes.SteampipeSnapshot, error) {
    18  	var dashboardNode modconfig.DashboardLeafNode
    19  	var panels map[string]dashboardtypes.SnapshotPanel
    20  	var checkRun *dashboardexecute.CheckRun
    21  
    22  	// get root benchmark/control
    23  	switch root := e.Root.Children[0].(type) {
    24  	case *controlexecute.ResultGroup:
    25  		var ok bool
    26  		dashboardNode, ok = root.GroupItem.(modconfig.DashboardLeafNode)
    27  		if !ok {
    28  			return nil, fmt.Errorf("invalid node found in control execution tree - cannot cast '%s' to a DashboardLeafNode", root.GroupItem.Name())
    29  		}
    30  	case *controlexecute.ControlRun:
    31  		dashboardNode = root.Control
    32  	}
    33  
    34  	// TACTICAL create a check run to wrap the execution tree
    35  	checkRun = &dashboardexecute.CheckRun{Root: e.Root.Children[0]}
    36  	checkRun.DashboardTreeRunImpl = dashboardexecute.NewDashboardTreeRunImpl(dashboardNode, nil, checkRun, nil)
    37  
    38  	// populate the panels
    39  	panels = checkRun.BuildSnapshotPanels(make(map[string]dashboardtypes.SnapshotPanel))
    40  
    41  	// create the snapshot
    42  	res := &dashboardtypes.SteampipeSnapshot{
    43  		SchemaVersion: fmt.Sprintf("%d", dashboardtypes.SteampipeSnapshotSchemaVersion),
    44  		Panels:        panels,
    45  		Layout:        checkRun.Root.AsTreeNode(),
    46  		Inputs:        map[string]interface{}{},
    47  		Variables:     dashboardexecute.GetReferencedVariables(checkRun, e.Workspace),
    48  		SearchPath:    e.SearchPath,
    49  		StartTime:     e.StartTime,
    50  		EndTime:       e.EndTime,
    51  		Title:         dashboardNode.GetTitle(),
    52  		FileNameRoot:  dashboardNode.Name(),
    53  	}
    54  	return res, nil
    55  }
    56  
    57  func PublishSnapshot(ctx context.Context, e *controlexecute.ExecutionTree, shouldShare bool) error {
    58  	statushooks.Show(ctx)
    59  	defer statushooks.Done(ctx)
    60  
    61  	snapshot, err := executionTreeToSnapshot(e)
    62  	if err != nil {
    63  		return err
    64  	}
    65  
    66  	message, err := cloud.PublishSnapshot(ctx, snapshot, shouldShare)
    67  	if err != nil {
    68  		return err
    69  	}
    70  	if viper.GetBool(constants.ArgProgress) {
    71  		statushooks.Done(ctx)
    72  		fmt.Println(message)
    73  	}
    74  	return nil
    75  
    76  }