Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ require (
github.com/spf13/viper v1.19.0
github.com/stevenle/topsort v0.2.0
github.com/turbot/go-kit v1.1.0
github.com/turbot/pipe-fittings/v2 v2.1.1
github.com/turbot/pipe-fittings/v2 v2.3.0-rc.3
github.com/turbot/steampipe-plugin-sdk/v5 v5.11.3
github.com/turbot/terraform-components v0.0.0-20231213122222-1f3526cab7a7 // indirect
github.com/xlab/treeprint v1.2.0 // indirect
Expand All @@ -42,7 +42,6 @@ require (
github.com/gin-contrib/size v1.0.1
github.com/go-sql-driver/mysql v1.8.1
github.com/jackc/pgx/v5 v5.6.0
github.com/jedib0t/go-pretty/v6 v6.5.9
github.com/logrusorgru/aurora v2.0.3+incompatible
github.com/marcboeker/go-duckdb v1.8.3
github.com/mattn/go-sqlite3 v1.14.22
Expand Down Expand Up @@ -158,6 +157,7 @@ require (
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/jedib0t/go-pretty/v6 v6.5.9 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
Expand Down Expand Up @@ -208,7 +208,6 @@ require (
github.com/tklauser/numcpus v0.3.0 // indirect
github.com/tkrajina/go-reflector v0.5.6 // indirect
github.com/turbot/pipes-sdk-go v0.12.0 // indirect
github.com/turbot/steampipe-plugin-code v1.0.1-alpha.1 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
github.com/ulikunitz/xz v0.5.10 // indirect
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -828,12 +828,10 @@ github.com/tkrajina/go-reflector v0.5.6 h1:hKQ0gyocG7vgMD2M3dRlYN6WBBOmdoOzJ6njQ
github.com/tkrajina/go-reflector v0.5.6/go.mod h1:ECbqLgccecY5kPmPmXg1MrHW585yMcDkVl6IvJe64T4=
github.com/turbot/go-kit v1.1.0 h1:2gW+MFDJD+mN41GcvhAajTrwR8HgN9KKJ8HnYwPGTV0=
github.com/turbot/go-kit v1.1.0/go.mod h1:1xmRuQ0cn/10QUMNLNOAFIqN8P6Rz5s3VLT8mkN3nF8=
github.com/turbot/pipe-fittings/v2 v2.1.1 h1:sV6bviX7WH3zivi45n29+ui+I9tJLlFNCNA2rOpw6/U=
github.com/turbot/pipe-fittings/v2 v2.1.1/go.mod h1:mGFH8dfDQOdv+d1fNL2r3ex+qlnVrTi3xGKZRVxoCEU=
github.com/turbot/pipe-fittings/v2 v2.3.0-rc.3 h1:9ZI021HWFrjiL2RplyYcSs7wZjUzwTy4voRYHkpvAXE=
github.com/turbot/pipe-fittings/v2 v2.3.0-rc.3/go.mod h1:wEoN4EseMTXophNlpOe740rAC9Jg0JhGRt5QM5R2ss8=
github.com/turbot/pipes-sdk-go v0.12.0 h1:esbbR7bALa5L8n/hqroMPaQSSo3gNM/4X0iTmHa3D6U=
github.com/turbot/pipes-sdk-go v0.12.0/go.mod h1:Mb+KhvqqEdRbz/6TSZc2QWDrMa5BN3E4Xw+gPt2TRkc=
github.com/turbot/steampipe-plugin-code v1.0.1-alpha.1 h1:mN0k0SGAN0pqPh92QZfJIzFzXuz6TiMALnLLLgCqnTI=
github.com/turbot/steampipe-plugin-code v1.0.1-alpha.1/go.mod h1:Dhkl99FVa9eHbBbHgyy0Zf6jj7eVjZZujyqore+RHmQ=
github.com/turbot/steampipe-plugin-sdk/v5 v5.11.3 h1:/b+ZUVydvkvjtNB0LbzVkDoWy/GB0qrucAxiUg4yznM=
github.com/turbot/steampipe-plugin-sdk/v5 v5.11.3/go.mod h1:zI1JuJjVV+tiqK1MbOfEfeyHJzONRQh/NEYVXEOyd4o=
github.com/turbot/terraform-components v0.0.0-20231213122222-1f3526cab7a7 h1:qDMxFVd8Zo0rIhnEBdCIbR+T6WgjwkxpFZMN8zZmmjg=
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func runCheckCmd[T controlinit.CheckTarget](cmd *cobra.Command, args []string) {
return
}
if shouldPrintCheckTiming() {
display.PrintTiming(&localqueryresult.TimingMetadata{
display.PrintTiming(&localqueryresult.CheckTimingMetadata{
Duration: time.Since(startTime),
})
}
Expand Down
76 changes: 61 additions & 15 deletions internal/cmd/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ import (
"github.com/turbot/pipe-fittings/v2/constants"
"github.com/turbot/pipe-fittings/v2/error_helpers"
"github.com/turbot/pipe-fittings/v2/export"
pquerydisplay "github.com/turbot/pipe-fittings/v2/querydisplay"
pqueryresult "github.com/turbot/pipe-fittings/v2/queryresult"
"github.com/turbot/pipe-fittings/v2/steampipeconfig"
"github.com/turbot/pipe-fittings/v2/workspace"
localcmdconfig "github.com/turbot/powerpipe/internal/cmdconfig"
localconstants "github.com/turbot/powerpipe/internal/constants"
"github.com/turbot/powerpipe/internal/dashboardexecute"
"github.com/turbot/powerpipe/internal/display"
"github.com/turbot/powerpipe/internal/initialisation"
"github.com/turbot/powerpipe/internal/queryresult"
"github.com/turbot/powerpipe/internal/resources"
Expand Down Expand Up @@ -53,7 +54,7 @@ The current mod is the working directory, or the directory specified by the --mo
AddStringArrayFlag(constants.ArgArg, nil, "Specify the value of a query argument").
AddStringFlag(constants.ArgDatabase, "", "Turbot Pipes workspace database", localcmdconfig.Deprecated("see https://powerpipe.io/docs/run#selecting-a-database for the new syntax")).
AddIntFlag(constants.ArgDatabaseQueryTimeout, localconstants.DatabaseDefaultQueryTimeout, "The query timeout").
AddStringSliceFlag(constants.ArgExport, nil, "Export output to file, supported formats: csv, html, json, md, nunit3, pps (snapshot), asff").
AddStringSliceFlag(constants.ArgExport, nil, "Export output to file, supported formats: csv, json, pps (snapshot)").
AddBoolFlag(constants.ArgHeader, true, "Include column headers for csv and table output").
AddBoolFlag(constants.ArgHelp, false, "Help for query", cmdconfig.FlagOptions.WithShortHand("h")).
AddBoolFlag(constants.ArgInput, true, "Enable interactive prompts").
Expand Down Expand Up @@ -154,10 +155,10 @@ func queryRun(cmd *cobra.Command, args []string) {
}
fmt.Println(string(jsonOutput)) //nolint:forbidigo // intentional use of fmt
default:
// otherwise convert the snapshot into a query result
// convert the snapshot into a query result
result, err := snapshotToQueryResult(snap, startTime)
error_helpers.FailOnError(err)
display.ShowQueryOutput(ctx, result)
pquerydisplay.ShowOutput(ctx, result)
}

// share the snapshot if necessary
Expand All @@ -168,14 +169,48 @@ func queryRun(cmd *cobra.Command, args []string) {
}

// export the result if necessary
exportArgs := viper.GetStringSlice(constants.ArgExport)
exportMsg, err := initData.ExportManager.DoExport(ctx, snap.FileNameRoot, snap, exportArgs)
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") //nolint:forbidigo // intentional use of fmt
fmt.Println(strings.Join(exportMsg, "\n")) //nolint:forbidigo // intentional use of fmt
fmt.Printf("\n") //nolint:forbidigo // intentional use of fmt
if viper.IsSet(constants.ArgExport) {
exportArgs := viper.GetStringSlice(constants.ArgExport)
var exportMsg []string

// check if export format is csv or json
isCsvOrJson := false
isSnapshot := false
for _, arg := range exportArgs {
argLower := strings.ToLower(arg)
if strings.Contains(argLower, "csv") || strings.Contains(argLower, "json") {
isCsvOrJson = true
}
if strings.Contains(argLower, "pps") {
isSnapshot = true
}
}

switch {
case isCsvOrJson:
// export csv/json with the query result
// convert the snapshot into a query result (this is needed again since the rowChan has been already closed)
result, err := snapshotToQueryResult(snap, startTime)
error_helpers.FailOnError(err)
exportMsg, err = initData.ExportManager.DoExport(ctx, "query", result, exportArgs)
error_helpers.FailOnErrorWithMessage(err, "failed to export")

case isSnapshot:
// export snapshot with the snapshot result
exportMsg, err = initData.ExportManager.DoExport(ctx, snap.FileNameRoot, snap, exportArgs)
error_helpers.FailOnErrorWithMessage(err, "failed to export snapshot")

default:
// not a recognised export format
error_helpers.FailOnError(fmt.Errorf("unsupported export format: %s", exportArgs))
}

// print the location where the file is exported
if len(exportMsg) > 0 && viper.GetBool(constants.ArgProgress) {
fmt.Printf("\n") //nolint:forbidigo // intentional use of fmt
fmt.Println(strings.Join(exportMsg, "\n")) //nolint:forbidigo // intentional use of fmt
fmt.Printf("\n") //nolint:forbidigo // intentional use of fmt
}
}

}
Expand All @@ -202,7 +237,7 @@ func validateQueryArgs(ctx context.Context) error {
}

func queryExporters() []export.Exporter {
return []export.Exporter{&export.SnapshotExporter{}}
return []export.Exporter{&export.SnapshotExporter{}, &export.JsonExporter{}, &export.CsvExporter{}}
}

func setExitCodeForQueryError(err error) {
Expand Down Expand Up @@ -250,8 +285,19 @@ func snapshotToQueryResult(snap *steampipeconfig.SteampipeSnapshot, startTime ti
res.Close()
}()

res.Timing = &queryresult.TimingMetadata{
Duration: time.Since(startTime),
res.Timing = &pqueryresult.QueryTimingMetadata{
RowsReturned: len(chartRun.Data.Rows),
Duration: getDurationString(time.Since(startTime)),
}
return res, nil
}

func getDurationString(duration time.Duration) string {
// Calculate duration since startTime and round down to the nearest millisecond
durationInMS := duration / time.Millisecond
//nolint:durationcheck // we want to print the duration in milliseconds
duration = durationInMS * time.Millisecond

durationString := duration.String()
return durationString
}
15 changes: 0 additions & 15 deletions internal/display/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,6 @@ import (
"github.com/turbot/pipe-fittings/v2/queryresult"
)

// columnNames builds a list of name from a slice of column defs - respecting the original name if present
func columnNames(columns []*queryresult.ColumnDef) []string {
var colNames = make([]string, len(columns))
for i, c := range columns {
// respect original name
if c.OriginalName != "" {
colNames[i] = c.OriginalName
} else {
colNames[i] = c.Name
}
}

return colNames
}

type columnValueSettings struct{ nullString string }

type ColumnValueOption func(opt *columnValueSettings)
Expand Down
Loading