MCPcopy Index your code
hub / github.com/wavetermdev/waveterm / fileAppendRun

Function fileAppendRun

cmd/wsh/cmd/wshcmd-file.go:267–323  ·  view source on GitHub ↗
(cmd *cobra.Command, args []string)

Source from the content-addressed store, hash-verified

265}
266
267func fileAppendRun(cmd *cobra.Command, args []string) error {
268 path, err := fixRelativePaths(args[0])
269 if err != nil {
270 return err
271 }
272 fileData := wshrpc.FileData{
273 Info: &wshrpc.FileInfo{
274 Path: path}}
275
276 info, err := ensureFile(fileData)
277 if err != nil {
278 return err
279 }
280 if info.Size >= MaxFileSize {
281 return fmt.Errorf("file already at maximum size (%d bytes)", MaxFileSize)
282 }
283
284 reader := bufio.NewReader(WrappedStdin)
285 var buf bytes.Buffer
286 remainingSpace := MaxFileSize - info.Size
287 for {
288 chunk := make([]byte, 8192)
289 n, err := reader.Read(chunk)
290 if err == io.EOF {
291 break
292 }
293 if err != nil {
294 return fmt.Errorf("reading input: %w", err)
295 }
296
297 if int64(buf.Len()+n) > remainingSpace {
298 return fmt.Errorf("append would exceed maximum file size of %d bytes", MaxFileSize)
299 }
300
301 buf.Write(chunk[:n])
302
303 if buf.Len() >= 8192 { // 8KB batch size
304 fileData.Data64 = base64.StdEncoding.EncodeToString(buf.Bytes())
305 err = wshclient.FileAppendCommand(RpcClient, fileData, &wshrpc.RpcOpts{Timeout: fileTimeout})
306 if err != nil {
307 return fmt.Errorf("appending to file: %w", err)
308 }
309 remainingSpace -= int64(buf.Len())
310 buf.Reset()
311 }
312 }
313
314 if buf.Len() > 0 {
315 fileData.Data64 = base64.StdEncoding.EncodeToString(buf.Bytes())
316 err = wshclient.FileAppendCommand(RpcClient, fileData, &wshrpc.RpcOpts{Timeout: fileTimeout})
317 if err != nil {
318 return fmt.Errorf("appending to file: %w", err)
319 }
320 }
321
322 return nil
323}
324

Callers

nothing calls this directly

Calls 7

ReadMethod · 0.95
FileAppendCommandFunction · 0.92
fixRelativePathsFunction · 0.85
ensureFileFunction · 0.85
LenMethod · 0.80
BytesMethod · 0.80
WriteMethod · 0.65

Tested by

no test coverage detected