diff --git a/cmd/query.go b/cmd/query.go index 70b110beb..8886ff386 100644 --- a/cmd/query.go +++ b/cmd/query.go @@ -21,10 +21,12 @@ import ( "github.com/turbot/steampipe/pkg/constants" "github.com/turbot/steampipe/pkg/contexthelpers" "github.com/turbot/steampipe/pkg/dashboard/dashboardtypes" + "github.com/turbot/steampipe/pkg/display" "github.com/turbot/steampipe/pkg/error_helpers" "github.com/turbot/steampipe/pkg/query" "github.com/turbot/steampipe/pkg/query/queryexecute" - "github.com/turbot/steampipe/pkg/snapshot2" + "github.com/turbot/steampipe/pkg/query/queryresult" + "github.com/turbot/steampipe/pkg/snapshot" "github.com/turbot/steampipe/pkg/statushooks" "github.com/turbot/steampipe/pkg/steampipeconfig/modconfig" "github.com/turbot/steampipe/pkg/utils" @@ -241,7 +243,7 @@ func executeSnapshotQuery(initData *query.InitData, ctx context.Context) int { baseInitData := &initData.InitData // so a dashboard name was specified - just call GenerateSnapshot - snap, err := snapshot2.GenerateSnapshot(ctx, queryProvider.Name(), baseInitData, nil) + snap, err := snapshot.GenerateSnapshot(ctx, queryProvider.Name(), baseInitData, nil) if err != nil { exitCode = constants.ExitCodeSnapshotCreationFailed error_helpers.FailOnError(err) @@ -265,71 +267,70 @@ func executeSnapshotQuery(initData *query.InitData, ctx context.Context) int { fmt.Println(string(jsonOutput)) default: // otherwise convert the snapshot into a query result - // result, err := snapshotToQueryResult(snap) - // error_helpers.FailOnErrorWithMessage(err, "failed to display result as snapshot") - fmt.Println() - // display.ShowOutput(ctx, result, display.WithTimingDisabled()) + result, err := snapshotToQueryResult(snap) + error_helpers.FailOnErrorWithMessage(err, "failed to display result as snapshot") + display.ShowOutput(ctx, result, display.WithTimingDisabled()) } // share the snapshot if necessary - // err = publishSnapshotIfNeeded(ctx, snap) - // if err != nil { - // exitCode = constants.ExitCodeSnapshotUploadFailed - // error_helpers.FailOnErrorWithMessage(err, fmt.Sprintf("failed to publish snapshot to %s", viper.GetString(constants.ArgSnapshotLocation))) - // } + err = publishSnapshotIfNeeded(ctx, snap) + if err != nil { + exitCode = constants.ExitCodeSnapshotUploadFailed + error_helpers.FailOnErrorWithMessage(err, fmt.Sprintf("failed to publish snapshot to %s", viper.GetString(constants.ArgSnapshotLocation))) + } // export the result if necessary - // exportArgs := viper.GetStringSlice(constants.ArgExport) - // exportMsg, err := initData.ExportManager.DoExport(ctx, snap.FileNameRoot, snap, exportArgs) - // if err != nil { - // exitCode = constants.ExitCodeSnapshotCreationFailed - // error_helpers.FailOnErrorWithMessage(err, "failed to export snapshot") - // } - // // print the location where the file is exported - // if len(exportMsg) > 0 && viper.GetBool(constants.ArgProgress) { - // fmt.Printf("\n") - // fmt.Println(strings.Join(exportMsg, "\n")) - // fmt.Printf("\n") - // } + exportArgs := viper.GetStringSlice(constants.ArgExport) + exportMsg, err := initData.ExportManager.DoExport(ctx, snap.FileNameRoot, snap, exportArgs) + if err != nil { + exitCode = constants.ExitCodeSnapshotCreationFailed + error_helpers.FailOnErrorWithMessage(err, "failed to export snapshot") + } + // print the location where the file is exported + if len(exportMsg) > 0 && viper.GetBool(constants.ArgProgress) { + fmt.Printf("\n") + fmt.Println(strings.Join(exportMsg, "\n")) + fmt.Printf("\n") + } } return 0 } -// func snapshotToQueryResult(snap *snapshot2.SteampipeSnapshot) (*queryresult.Result, error) { -// // the table of a snapshot query has a fixed name -// tablePanel, ok := snap.Panels[modconfig.SnapshotQueryTableName] -// if !ok { -// return nil, sperr.New("dashboard does not contain table result for query") -// } -// chartRun := tablePanel.(*snapshot.LeafRun) -// if !ok { -// return nil, sperr.New("failed to read query result from snapshot") -// } -// // check for error -// if err := chartRun.GetError(); err != nil { -// return nil, error_helpers.DecodePgError(err) -// } +func snapshotToQueryResult(snap *dashboardtypes.SteampipeSnapshot) (*queryresult.Result, error) { + // the table of a snapshot query has a fixed name + tablePanel, ok := snap.Panels[modconfig.SnapshotQueryTableName] + if !ok { + return nil, sperr.New("dashboard does not contain table result for query") + } + chartRun := tablePanel.(*snapshot.LeafRun) + if !ok { + return nil, sperr.New("failed to read query result from snapshot") + } + // check for error + if err := chartRun.GetError(); err != nil { + return nil, error_helpers.DecodePgError(err) + } -// res := queryresult.NewResult(chartRun.Data.Columns) + res := queryresult.NewResult(chartRun.Data.Columns) -// // start a goroutine to stream the results as rows -// go func() { -// for _, d := range chartRun.Data.Rows { -// // we need to allocate a new slice everytime, since this gets read -// // asynchronously on the other end and we need to make sure that we don't overwrite -// // data already sent -// rowVals := make([]interface{}, len(chartRun.Data.Columns)) -// for i, c := range chartRun.Data.Columns { -// rowVals[i] = d[c.Name] -// } -// res.StreamRow(rowVals) -// } -// res.TimingResult <- chartRun.TimingResult -// res.Close() -// }() + // start a goroutine to stream the results as rows + go func() { + for _, d := range chartRun.Data.Rows { + // we need to allocate a new slice everytime, since this gets read + // asynchronously on the other end and we need to make sure that we don't overwrite + // data already sent + rowVals := make([]interface{}, len(chartRun.Data.Columns)) + for i, c := range chartRun.Data.Columns { + rowVals[i] = d[c.Name] + } + res.StreamRow(rowVals) + } + res.TimingResult <- chartRun.TimingResult + res.Close() + }() -// return res, nil -// } + return res, nil +} // convert the given command line query into a query resource and add to workspace // this is to allow us to use existing dashboard execution code diff --git a/pkg/display/display.go b/pkg/display/display.go index d631b7f3c..21fe7a3b8 100644 --- a/pkg/display/display.go +++ b/pkg/display/display.go @@ -22,13 +22,10 @@ import ( "github.com/spf13/viper" "github.com/turbot/go-kit/helpers" pfq "github.com/turbot/pipe-fittings/queryresult" - pqueryresult "github.com/turbot/pipe-fittings/queryresult" - pfq "github.com/turbot/pipe-fittings/queryresult" "github.com/turbot/steampipe/pkg/cmdconfig" "github.com/turbot/steampipe/pkg/constants" "github.com/turbot/steampipe/pkg/error_helpers" "github.com/turbot/steampipe/pkg/query/queryresult" - "github.com/turbot/steampipe/pkg/snapshot2" "golang.org/x/text/language" "golang.org/x/text/message" ) @@ -45,8 +42,6 @@ func ShowOutput(ctx context.Context, result *queryresult.Result, opts ...Display outputFormat := cmdconfig.Viper().GetString(constants.ArgOutput) switch outputFormat { - case constants.OutputFormatSnapshotShort: - rowErrors, timingResult = displaySnapshot(ctx, result) case constants.OutputFormatJSON: rowErrors, timingResult = displayJSON(ctx, result) case constants.OutputFormatCSV: @@ -202,56 +197,6 @@ func newJSONOutput() *jsonOutput { } -func displaySnapshot(ctx context.Context, result *queryresult.Result) (int, *queryresult.TimingResult) { - rowErrors := 0 - snapshotOutput := snapshot2.NewEmptySnapshot() - - // add column defs to the JSON output - for _, col := range result.Cols { - // create a new column def, converting the data type to lowercase - c := pfq.ColumnDef{ - Name: col.Name, - DataType: strings.ToLower(col.DataType), - } - // add to the column def array - snapshotOutput.Panels["abcd"].Columns = append(jsonOutput.Columns, c) - } - - // define function to add each row to the JSON output - rowFunc := func(row []interface{}, result *queryresult.Result) { - record := map[string]interface{}{} - for idx, col := range result.Cols { - value, _ := ParseJSONOutputColumnValue(row[idx], col) - // get the column def - c := jsonOutput.Columns[idx] - // add the value under the unique column name - record[c.Name] = value - } - snapshotOutput.Panels.Rows = append(jsonOutput.Rows, record) - } - - // call this function for each row - count, err := iterateResults(result, rowFunc) - if err != nil { - error_helpers.ShowError(ctx, err) - rowErrors++ - return rowErrors, nil - } - - // now we have iterated the rows, get the timing - snapshotOutput.Metadata = getTiming(result, count) - - // display the JSON - encoder := json.NewEncoder(os.Stdout) - encoder.SetIndent("", " ") - encoder.SetEscapeHTML(false) - if err := encoder.Encode(snapshotOutput); err != nil { - fmt.Print("Error displaying result as JSON", err) - return 0, nil - } - return rowErrors, snapshotOutput.Metadata -} - func displayJSON(ctx context.Context, result *queryresult.Result) (int, *queryresult.TimingResult) { rowErrors := 0 jsonOutput := newJSONOutput() diff --git a/pkg/snapshot2/snapshot.go b/pkg/snapshot2/snapshot.go deleted file mode 100644 index c9315739c..000000000 --- a/pkg/snapshot2/snapshot.go +++ /dev/null @@ -1,22 +0,0 @@ -package snapshot2 - -import ( - "context" - - "github.com/turbot/pipe-fittings/steampipeconfig" - "github.com/turbot/steampipe/pkg/initialisation" -) - -func GenerateSnapshot(ctx context.Context, target string, initData *initialisation.InitData, inputs map[string]any) (snapshot steampipeconfig.SteampipeSnapshot, err error) { - snapshot = NewEmptySnapshot() - return snapshot, nil -} - -func NewEmptySnapshot() steampipeconfig.SteampipeSnapshot { - return steampipeconfig.SteampipeSnapshot{ - SchemaVersion: "20221222", - Inputs: make(map[string]interface{}), - Panels: make(map[string]steampipeconfig.SnapshotPanel), - Variables: make(map[string]string), - } -}