Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add flag header for download table command #1892

Merged
merged 4 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
22 changes: 22 additions & 0 deletions internal/pkg/service/cli/cmd/remote/table/download/cmd.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package download

import (
"context"
"time"

"github.com/keboola/go-client/pkg/keboola"
Expand All @@ -22,6 +23,7 @@ type Flags struct {
ChangeSince configmap.Value[string] `configKey:"changed-since" configUsage:"only export rows imported after this date"`
ChangedUntil configmap.Value[string] `configKey:"changed-until" configUsage:"only export rows imported before this date"`
Columns configmap.Value[[]string] `configKey:"columns" configUsage:"comma-separated list of columns to export"`
Header configmap.Value[bool] `configKey:"header" configUsage:"first line of the csv file contains the column names"`
Limit configmap.Value[uint] `configKey:"limit" configUsage:"limit the number of exported rows"`
Where configmap.Value[string] `configKey:"where" configUsage:"filter columns by value"`
Order configmap.Value[string] `configKey:"order" configUsage:"order by one or more columns"`
Expand Down Expand Up @@ -108,10 +110,17 @@ func Command(p dependencies.Provider) *cobra.Command {
return err
}

columns, err := getColumns(cmd.Context(), d, f, tableKey)
if err != nil {
return err
}

downloadOpts := download.Options{
File: fileWithCredentials,
Output: fileOutput,
AllowSliced: f.AllowSliced.Value,
Header: f.Header,
Columns: columns,
}

// Send cmd successful/failed event
Expand All @@ -125,3 +134,16 @@ func Command(p dependencies.Provider) *cobra.Command {

return cmd
}

func getColumns(ctx context.Context, d dependencies.RemoteCommandScope, f Flags, key keboola.TableKey) ([]string, error) {
if f.Header.IsSet() && len(f.Columns.Value) == 0 {
table, err := d.KeboolaProjectAPI().GetTableRequest(key).Send(ctx)
if err != nil {
return nil, err
}

return table.Columns, nil
}

return f.Columns.Value, nil
hosekpeter marked this conversation as resolved.
Show resolved Hide resolved
}
15 changes: 15 additions & 0 deletions pkg/lib/operation/project/remote/file/download/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package download

import (
"context"
"encoding/csv"
"fmt"
"io"
"os"
Expand All @@ -23,6 +24,7 @@ import (
const (
StdoutOutput = "-"
GZIPFileExt = ".gz"
CSVFileExt = ".csv"
GetFileSizeParallelism = 100
)

Expand Down Expand Up @@ -179,11 +181,24 @@ func (d *downloader) readSliceTo(ctx context.Context, slice string, writer io.Wr
}
}

if strings.HasSuffix(d.options.Output, CSVFileExt) && d.options.Header.IsSet() {
if err := d.addHeaderToCSV(writer); err != nil {
return err
}
}

// Copy all
_, err := io.Copy(writer, reader)
return err
}

func (d *downloader) addHeaderToCSV(writer io.Writer) error {
w := csv.NewWriter(writer)
defer w.Flush()

return w.Write(d.options.Columns)
}

// getSlices from the file manifest.
func (d *downloader) getSlices(ctx context.Context) ([]string, error) {
if d.options.File.IsSliced {
Expand Down
4 changes: 4 additions & 0 deletions pkg/lib/operation/project/remote/file/download/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ import (
"path/filepath"

"github.com/keboola/go-client/pkg/keboola"

"github.com/keboola/keboola-as-code/internal/pkg/service/common/configmap"
)

type Options struct {
File *keboola.FileDownloadCredentials
Output string
AllowSliced bool
Columns []string
Header configmap.Value[bool]
}

func (o *Options) ToStdout() bool {
Expand Down
1 change: 1 addition & 0 deletions test/cli/help/remote-table-download/expected-stdout
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Flags:
--changed-until string only export rows imported before this date
--columns strings comma-separated list of columns to export
--format string output format (json/csv) (default "csv")
--header first line of the csv file contains the column names
--limit uint limit the number of exported rows
--order string order by one or more columns
-o, --output string path to the destination file or directory
Expand Down
1 change: 1 addition & 0 deletions test/cli/remote-table-download/run-ok-header/args
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
remote table download --storage-api-host %%TEST_KBC_STORAGE_API_HOST%% --storage-api-token %%TEST_KBC_STORAGE_API_TOKEN%% in.c-test.accounts -o data.csv --header
1 change: 1 addition & 0 deletions test/cli/remote-table-download/run-ok-header/expected-code
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
58 changes: 58 additions & 0 deletions test/cli/remote-table-download/run-ok-header/expected-state.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"branches": [
{
"branch": {
"name": "Main",
"description": "",
"isDefault": true
},
"configs": []
}
],
"buckets": [
{
"id": "in.c-test",
"uri": "%s/v2/storage/buckets/in.c-test",
"displayName": "test",
"description": "",
"tables": [
{
"id": "in.c-test.accounts",
"uri": "%s/v2/storage/tables/in.c-test.accounts",
"name": "accounts",
"displayName": "accounts",
"primaryKey": [
"Id",
"Name"
],
"columns": [
"Id",
"Name",
"Region",
"Status",
"First_Order"
],
"rowsCount": 4
}
]
}
],
"files": [
{
"name": "in.c-test.accounts.csv",
"tags": [
"table-export"
],
"isSliced": true,
"isEncrypted": true,
"isPermanent": false
},
{
"name": "in.c_test.accounts.data",
"tags": [],
"isSliced": false,
"isEncrypted": true,
"isPermanent": false
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
%ADownloading%A
3 changes: 3 additions & 0 deletions test/cli/remote-table-download/run-ok-header/expected-stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Unloading table, please wait.
Table "in.c-test.accounts" unloaded to file "%d".
File "%s" downloaded to "data.csv".
Empty file.
37 changes: 37 additions & 0 deletions test/cli/remote-table-download/run-ok-header/initial-state.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"backend": {
"type": "snowflake"
},
"branches": [
{
"branch": {
"name": "Main",
"description": "",
"isDefault": true
},
"configs": []
}
],
"buckets": [
{
"id": "in.c-test",
"name": "test",
"stage": "in",
"tables": [
{
"id": "in.c-test.accounts",
"name": "accounts",
"primaryKey": ["Id", "Name"],
"columns": ["Id", "Name", "Region", "Status", "First_Order"],
"rows": [
["f030ed64cbc8babbe50901a26675a2ee", "CSK Auto", "US West", "Active", "2015-01-23"],
["06c0b954b0d2088e3da2132d1ba96f31", "AM/PM Camp", "Global", "Active", "2015-02-04"],
["fffe0e30b4a34f01063330a4b908fde5", "Super Foods", "Global", "Active", "2015-02-06"],
["33025ad4a425b6ee832e76beb250ae1c", "Netcore", "Global", "Inactive", "2015-03-02"]
]
}
]
}
]
}

Empty file.
5 changes: 5 additions & 0 deletions test/cli/remote-table-download/run-ok-header/out/data.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Id,Name,Region,Status,First_Order
"fffe0e30b4a34f01063330a4b908fde5","Super Foods","Global","Active","2015-02-06"
"06c0b954b0d2088e3da2132d1ba96f31","AM/PM Camp","Global","Active","2015-02-04"
"f030ed64cbc8babbe50901a26675a2ee","CSK Auto","US West","Active","2015-01-23"
"33025ad4a425b6ee832e76beb250ae1c","Netcore","Global","Inactive","2015-03-02"
1 change: 1 addition & 0 deletions test/cli/remote-table-download/with-columns/args
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
remote table download --storage-api-host %%TEST_KBC_STORAGE_API_HOST%% --storage-api-token %%TEST_KBC_STORAGE_API_TOKEN%% in.c-test.accounts -o data.csv --columns Status,Id,First_Order
1 change: 1 addition & 0 deletions test/cli/remote-table-download/with-columns/expected-code
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
58 changes: 58 additions & 0 deletions test/cli/remote-table-download/with-columns/expected-state.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"branches": [
{
"branch": {
"name": "Main",
"description": "",
"isDefault": true
},
"configs": []
}
],
"buckets": [
{
"id": "in.c-test",
"uri": "%s/v2/storage/buckets/in.c-test",
"displayName": "test",
"description": "",
"tables": [
{
"id": "in.c-test.accounts",
"uri": "%s/v2/storage/tables/in.c-test.accounts",
"name": "accounts",
"displayName": "accounts",
"primaryKey": [
"Id",
"Name"
],
"columns": [
"Id",
"Name",
"Region",
"Status",
"First_Order"
],
"rowsCount": 4
}
]
}
],
"files": [
{
"name": "in.c-test.accounts.csv",
"tags": [
"table-export"
],
"isSliced": true,
"isEncrypted": true,
"isPermanent": false
},
{
"name": "in.c_test.accounts.data",
"tags": [],
"isSliced": false,
"isEncrypted": true,
"isPermanent": false
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
%ADownloading%A
3 changes: 3 additions & 0 deletions test/cli/remote-table-download/with-columns/expected-stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Unloading table, please wait.
Table "in.c-test.accounts" unloaded to file "%d".
File "%s" downloaded to "data.csv".
Empty file.
37 changes: 37 additions & 0 deletions test/cli/remote-table-download/with-columns/initial-state.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"backend": {
"type": "snowflake"
},
"branches": [
{
"branch": {
"name": "Main",
"description": "",
"isDefault": true
},
"configs": []
}
],
"buckets": [
{
"id": "in.c-test",
"name": "test",
"stage": "in",
"tables": [
{
"id": "in.c-test.accounts",
"name": "accounts",
"primaryKey": ["Id", "Name"],
"columns": ["Id", "Name", "Region", "Status", "First_Order"],
"rows": [
["f030ed64cbc8babbe50901a26675a2ee", "CSK Auto", "US West", "Active", "2015-01-23"],
["06c0b954b0d2088e3da2132d1ba96f31", "AM/PM Camp", "Global", "Active", "2015-02-04"],
["fffe0e30b4a34f01063330a4b908fde5", "Super Foods", "Global", "Active", "2015-02-06"],
["33025ad4a425b6ee832e76beb250ae1c", "Netcore", "Global", "Inactive", "2015-03-02"]
]
}
]
}
]
}

Empty file.
6 changes: 6 additions & 0 deletions test/cli/remote-table-download/with-columns/out/data.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"Active","fffe0e30b4a34f01063330a4b908fde5","2015-02-06"
"Active","06c0b954b0d2088e3da2132d1ba96f31","2015-02-04"
"Active","f030ed64cbc8babbe50901a26675a2ee","2015-01-23"
"Inactive","33025ad4a425b6ee832e76beb250ae1c","2015-03-02"


Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
remote table download --storage-api-host %%TEST_KBC_STORAGE_API_HOST%% --storage-api-token %%TEST_KBC_STORAGE_API_TOKEN%% in.c-test.accounts -o data.csv --header --order Name --columns Name,Id,Region
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
Loading
Loading