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

     1  package controldisplay
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"io"
     7  	"strings"
     8  
     9  	"github.com/turbot/steampipe/pkg/constants"
    10  	"github.com/turbot/steampipe/pkg/control/controlexecute"
    11  )
    12  
    13  type SnapshotFormatter struct {
    14  	FormatterBase
    15  }
    16  
    17  func (f *SnapshotFormatter) Format(ctx context.Context, tree *controlexecute.ExecutionTree) (io.Reader, error) {
    18  	snapshot, err := executionTreeToSnapshot(tree)
    19  	if err != nil {
    20  		return nil, err
    21  	}
    22  
    23  	// determine whether to indent the snapshot
    24  	// TACTICAL: check in the context for contextKeyFormatterUse - if this is "export" then DO NOT indent
    25  	var indent = true
    26  	if formatterPurpose, ok := ctx.Value(contextKeyFormatterPurpose).(string); ok && formatterPurpose == formatterPurposeExport {
    27  		indent = false
    28  	}
    29  	// strip unwanted fields from the snapshot
    30  	snapshotStr, err := snapshot.AsStrippedJson(indent)
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  
    35  	res := strings.NewReader(fmt.Sprintf("%s\n", string(snapshotStr)))
    36  
    37  	return res, nil
    38  }
    39  
    40  func (f *SnapshotFormatter) FileExtension() string {
    41  	return constants.SnapshotExtension
    42  }
    43  
    44  func (f SnapshotFormatter) Name() string {
    45  	return constants.OutputFormatSnapshot
    46  }
    47  
    48  func (f *SnapshotFormatter) Alias() string {
    49  	return "sps"
    50  }