-
Notifications
You must be signed in to change notification settings - Fork 263
/
control_exporter.go
53 lines (41 loc) · 1.38 KB
/
control_exporter.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
package controldisplay
import (
"context"
"fmt"
"github.com/turbot/steampipe/pkg/contexthelpers"
"github.com/turbot/steampipe/pkg/control/controlexecute"
"github.com/turbot/steampipe/pkg/export"
)
var contextKeyFormatterPurpose = contexthelpers.ContextKey("formatter_purpose")
const formatterPurposeExport = "export"
type ControlExporter struct {
formatter Formatter
}
func NewControlExporter(formatter Formatter) *ControlExporter {
return &ControlExporter{formatter}
}
func (e *ControlExporter) Export(ctx context.Context, input export.ExportSourceData, destPath string) error {
// tell the formatter it is being used for export
// this is a tactical mechanism used to ensure that exported snapshots are unindented
// whereas display snapshots are indented
exportCtx := context.WithValue(ctx, contextKeyFormatterPurpose, formatterPurposeExport)
// input must be control execution tree
tree, ok := input.(*controlexecute.ExecutionTree)
if !ok {
return fmt.Errorf("ControlExporter input must be *controlexecute.ExecutionTree")
}
res, err := e.formatter.Format(exportCtx, tree)
if err != nil {
return err
}
return export.Write(destPath, res)
}
func (e *ControlExporter) FileExtension() string {
return e.formatter.FileExtension()
}
func (e *ControlExporter) Name() string {
return e.formatter.Name()
}
func (e *ControlExporter) Alias() string {
return e.formatter.Alias()
}