@@ -97,7 +97,8 @@ func downloadFunc(
97
97
}
98
98
99
99
var req * http.Request
100
- if volumeMode == "Filesystem" {
100
+ switch volumeMode {
101
+ case "Filesystem" :
101
102
if srcPath == "" || srcPath [len (srcPath )- 1 :] != "/" {
102
103
return fmt .Errorf ("invalid source path: '%s'" , srcPath )
103
104
}
@@ -108,24 +109,33 @@ func downloadFunc(
108
109
109
110
log .Info ("Start listing" , slog .String ("url" , dataURL ), slog .String ("srcPath" , srcPath ))
110
111
req , _ = http .NewRequest ("GET" , dataURL , nil )
111
- } else if volumeMode == "Block" {
112
+ case "Block" :
112
113
log .Info ("Start listing" , slog .String ("url" , url ))
113
114
req , _ = http .NewRequest ("HEAD" , url , nil )
114
115
}
115
116
116
- resp , err := subClient .HttpDo (req )
117
+ resp , err := subClient .HttpDo (req . WithContext ( ctx ) )
117
118
if err != nil {
118
119
return fmt .Errorf ("HttpDo: %s\n " , err .Error ())
119
120
}
120
121
defer resp .Body .Close ()
121
122
122
123
if resp .StatusCode != 200 {
123
- msg , err := io .ReadAll (io .LimitReader (resp .Body , 1000 ))
124
+ const maxLen = 4096
125
+ msg , err := io .ReadAll (io .LimitReader (resp .Body , maxLen ))
124
126
if err == nil {
125
127
return fmt .Errorf ("Backend response \" %s\" Msg: %s" , resp .Status , string (msg ))
126
128
}
127
129
}
128
130
131
+ if volumeMode == "Block" {
132
+ body := ""
133
+ if contLen := resp .Header .Get ("Content-Length" ); contLen != "" {
134
+ body = fmt .Sprintf ("Content-Length: %s" , contLen )
135
+ }
136
+ return foo (strings .NewReader (body ))
137
+ }
138
+
129
139
return foo (resp .Body )
130
140
}
131
141
@@ -185,8 +195,12 @@ func Run(ctx context.Context, log *slog.Logger, cmd *cobra.Command, args []strin
185
195
186
196
err = downloadFunc (ctx , log , namespace , deName , srcPath , publish , sClient , func (body io.Reader ) error {
187
197
_ , err := io .Copy (os .Stdout , body )
198
+ if err == io .EOF {
199
+ err = nil
200
+ }
188
201
return err
189
202
})
203
+
190
204
if err != nil {
191
205
return err
192
206
}
0 commit comments