@@ -23,6 +23,7 @@ import (
23
23
"log/slog"
24
24
"net/http"
25
25
neturl "net/url"
26
+ "os"
26
27
"strings"
27
28
"time"
28
29
@@ -82,20 +83,27 @@ func parseArgs(args []string) (deName, srcPath string, err error) {
82
83
return
83
84
}
84
85
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 {
86
94
url , volumeMode , subClient , err := util .PrepareDownload (ctx , log , deName , namespace , publish , sClient )
87
95
if err != nil {
88
- return nil , err
96
+ return err
89
97
}
90
98
91
99
var req * http.Request
92
100
if volumeMode == "Filesystem" {
93
101
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 )
95
103
}
96
104
dataURL , err := neturl .JoinPath (url , srcPath )
97
105
if err != nil {
98
- return nil , err
106
+ return err
99
107
}
100
108
101
109
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
107
115
108
116
resp , err := subClient .HttpDo (req )
109
117
if err != nil {
110
- return nil , fmt .Errorf ("HttpDo: %s\n " , err .Error ())
118
+ return fmt .Errorf ("HttpDo: %s\n " , err .Error ())
111
119
}
112
120
defer resp .Body .Close ()
113
121
114
122
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 ))
136
126
}
137
127
}
138
128
139
- return bodyRaw , nil
129
+ return foo ( resp . Body )
140
130
}
141
131
142
132
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
193
183
194
184
log .Info ("DataExport created" , slog .String ("name" , deName ), slog .String ("namespace" , namespace ))
195
185
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
+ })
197
190
if err != nil {
198
191
return err
199
192
}
200
- fmt .Println (string (data ))
201
193
202
194
if deName != dataName { // DataExport created in download process
203
195
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