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 option to download sliced file from staging storage without decompression #2137

Merged
merged 5 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
16 changes: 9 additions & 7 deletions internal/pkg/service/cli/cmd/remote/file/download/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ import (
)

type Flags struct {
StorageAPIHost configmap.Value[string] `configKey:"storage-api-host" configShorthand:"H" configUsage:"storage API host, eg. \"connection.keboola.com\""`
StorageAPIToken configmap.Value[string] `configKey:"storage-api-token" configShorthand:"t" configUsage:"storage API token from your project"`
Output configmap.Value[string] `configKey:"output" configShorthand:"o" configUsage:"path to the destination file or directory"`
AllowSliced configmap.Value[bool] `configKey:"allow-sliced" configUsage:"output sliced files as a directory containing slices as individual files"`
StorageAPIHost configmap.Value[string] `configKey:"storage-api-host" configShorthand:"H" configUsage:"storage API host, eg. \"connection.keboola.com\""`
StorageAPIToken configmap.Value[string] `configKey:"storage-api-token" configShorthand:"t" configUsage:"storage API token from your project"`
Output configmap.Value[string] `configKey:"output" configShorthand:"o" configUsage:"path to the destination file or directory"`
AllowSliced configmap.Value[bool] `configKey:"allow-sliced" configUsage:"output sliced files as a directory containing slices as individual files"`
WithOutDecompress configmap.Value[bool] `configKey:"without-decompress" configUsage:"treat the sliced files as individual files, without decompressing them."`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
WithOutDecompress configmap.Value[bool] `configKey:"without-decompress" configUsage:"treat the sliced files as individual files, without decompressing them."`
WithoutDecompress configmap.Value[bool] `configKey:"without-decompress" configUsage:"do not decompress the downloaded files or sliced files."`

Copy link
Contributor Author

@hosekpeter hosekpeter Nov 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

func DefaultFlags() Flags {
Expand Down Expand Up @@ -83,9 +84,10 @@ func Command(p dependencies.Provider) *cobra.Command {
defer d.EventSender().SendCmdEvent(cmd.Context(), time.Now(), &cmdErr, "remote-file-download")

opts := download.Options{
File: file,
Output: output,
AllowSliced: f.AllowSliced.Value,
File: file,
Output: output,
AllowSliced: f.AllowSliced.Value,
WithOutDecompress: f.WithOutDecompress,
}

return download.Run(cmd.Context(), opts, d)
Expand Down
22 changes: 12 additions & 10 deletions pkg/lib/operation/project/remote/file/download/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,16 +168,18 @@ func (d *downloader) readSliceTo(ctx context.Context, slice string, writer io.Wr
}

// Add decompression reader
if strings.HasSuffix(slice, GZIPFileExt) || (slice == "" && strings.HasSuffix(d.options.File.Name, GZIPFileExt)) {
if gzipReader, err := pgzip.NewReader(reader); err == nil {
defer func() {
if closeErr := gzipReader.Close(); returnErr == nil && closeErr != nil {
returnErr = closeErr
}
}()
reader = gzipReader
} else {
return errors.Errorf(`cannot create gzip reader: %w`, err)
if !d.options.WithOutDecompress.IsSet() {
if strings.HasSuffix(slice, GZIPFileExt) || (slice == "" && strings.HasSuffix(d.options.File.Name, GZIPFileExt)) {
if gzipReader, err := pgzip.NewReader(reader); err == nil {
defer func() {
if closeErr := gzipReader.Close(); returnErr == nil && closeErr != nil {
returnErr = closeErr
}
}()
reader = gzipReader
} else {
return errors.Errorf(`cannot create gzip reader: %w`, err)
}
}
}

Expand Down
11 changes: 6 additions & 5 deletions pkg/lib/operation/project/remote/file/download/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import (
)

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

func (o *Options) ToStdout() bool {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
remote file download %%TEST_FILE_TEST1_ID%% --output file.csv.gz --without-decompress --storage-api-host %%TEST_KBC_STORAGE_API_HOST%% --storage-api-token %%TEST_KBC_STORAGE_API_TOKEN%%
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
%ADownloading%A
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
File "%%TEST_FILE_TEST1_ID%%" downloaded to "file.csv.gz".
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"branches": [
{
"branch": {
"name": "Main",
"description": "",
"isDefault": true
},
"configs": []
}
],
"files": [
{
"name": "test1",
"content": "H4sIAAAAAAAAA0vOzzHUSc7PMeIqSwSygASYZQximQAAVQ0b1R0AAAA="
hosekpeter marked this conversation as resolved.
Show resolved Hide resolved
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
H4sIAAAAAAAAA0vOzzHUSc7PMeIqSwSygASYZQximQAAVQ0b1R0AAAA=