Skip to content

Commit 79246f4

Browse files
committed
Use io.Copy for list
Signed-off-by: Anton Sergunov <[email protected]>
1 parent 0e290aa commit 79246f4

File tree

1 file changed

+21
-29
lines changed
  • internal/dataexport/cmd/list

1 file changed

+21
-29
lines changed

internal/dataexport/cmd/list/list.go

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"log/slog"
2424
"net/http"
2525
neturl "net/url"
26+
"os"
2627
"strings"
2728
"time"
2829

@@ -82,20 +83,27 @@ func parseArgs(args []string) (deName, srcPath string, err error) {
8283
return
8384
}
8485

85-
func downloadRaw(ctx context.Context, log *slog.Logger, namespace, deName, srcPath string, publish bool, sClient *safeClient.SafeClient) ([]byte, error) {
86+
func downloadFunc(
87+
ctx context.Context,
88+
log *slog.Logger,
89+
namespace, deName, srcPath string,
90+
publish bool,
91+
sClient *safeClient.SafeClient,
92+
foo func(body io.Reader) error,
93+
) error {
8694
url, volumeMode, subClient, err := util.PrepareDownload(ctx, log, deName, namespace, publish, sClient)
8795
if err != nil {
88-
return nil, err
96+
return err
8997
}
9098

9199
var req *http.Request
92100
if volumeMode == "Filesystem" {
93101
if srcPath == "" || srcPath[len(srcPath)-1:] != "/" {
94-
return nil, fmt.Errorf("invalid source path: '%s'", srcPath)
102+
return fmt.Errorf("invalid source path: '%s'", srcPath)
95103
}
96104
dataURL, err := neturl.JoinPath(url, srcPath)
97105
if err != nil {
98-
return nil, err
106+
return err
99107
}
100108

101109
log.Info("Start listing", slog.String("url", dataURL), slog.String("srcPath", srcPath))
@@ -107,36 +115,18 @@ func downloadRaw(ctx context.Context, log *slog.Logger, namespace, deName, srcPa
107115

108116
resp, err := subClient.HttpDo(req)
109117
if err != nil {
110-
return nil, fmt.Errorf("HttpDo: %s\n", err.Error())
118+
return fmt.Errorf("HttpDo: %s\n", err.Error())
111119
}
112120
defer resp.Body.Close()
113121

114122
if resp.StatusCode != 200 {
115-
if resp.ContentLength > 0 {
116-
msg, err := io.ReadAll(io.LimitReader(resp.Body, 1000))
117-
if err == nil {
118-
return nil, fmt.Errorf("Backend response \"%s\" Msg: %s", resp.Status, string(msg))
119-
}
120-
}
121-
122-
return nil, fmt.Errorf("Backend response \"%s\"", resp.Status)
123-
}
124-
125-
bodyRaw := []byte{}
126-
if volumeMode == "Block" {
127-
contLen := resp.Header.Get("Content-Length")
128-
if len(contLen) == 0 {
129-
contLen = "0"
130-
}
131-
bodyRaw = append(bodyRaw, []byte("Content-Length: "+contLen+"\n")...)
132-
} else {
133-
bodyRaw, err = io.ReadAll(resp.Body)
134-
if err != nil {
135-
return nil, fmt.Errorf("Response body (%s) error: %s", srcPath, err.Error())
123+
msg, err := io.ReadAll(io.LimitReader(resp.Body, 1000))
124+
if err == nil {
125+
return fmt.Errorf("Backend response \"%s\" Msg: %s", resp.Status, string(msg))
136126
}
137127
}
138128

139-
return bodyRaw, nil
129+
return foo(resp.Body)
140130
}
141131

142132
func createDataExporterIfNeeded(ctx context.Context, log *slog.Logger, deName, namespace string, publish bool, rtClient ctrlrtclient.Client) (string, error) {
@@ -193,11 +183,13 @@ func Run(ctx context.Context, log *slog.Logger, cmd *cobra.Command, args []strin
193183

194184
log.Info("DataExport created", slog.String("name", deName), slog.String("namespace", namespace))
195185

196-
data, err := downloadRaw(ctx, log, namespace, deName, srcPath, publish, sClient)
186+
err = downloadFunc(ctx, log, namespace, deName, srcPath, publish, sClient, func(body io.Reader) error {
187+
_, err := io.Copy(os.Stdout, body)
188+
return err
189+
})
197190
if err != nil {
198191
return err
199192
}
200-
fmt.Println(string(data))
201193

202194
if deName != dataName { // DataExport created in download process
203195
if util.AskYesNoWithTimeout("DataExport will auto-delete in 30 sec [press y+Enter to delete now, n+Enter to cancel]", time.Second*30) {

0 commit comments

Comments
 (0)